Home >Backend Development >Golang >Golang cannot 'sqlc generate'

Golang cannot 'sqlc generate'

王林
王林forward
2024-02-09 11:09:181121browse

Golang 无法“sqlc 生成”

php Editor Banana brings you an important issue about Golang: the problem of being unable to use sqlc to generate code. Golang is a powerful programming language that is often used to develop efficient and reliable back-end applications. However, some developers have encountered problems using sqlc and are unable to generate the required code. This problem causes confusion for developers, so in this article we will explore the possible causes and provide solutions to help you solve this confusion. Let’s take a look!

Question content

I'm using Windows and Ubuntu WSL but I get this error when I try to generate sqlc.

sqlc generate
# package database
sql/schema/001_users.sql:1:1: the PostgreSQL engine requires cgo. Please set CGO_ENABLED=1.
export CGO_ENABLED=1 && sqlc generate
# package database
sql/schema/001_users.sql:1:1: the PostgreSQL engine requires cgo. Please set CGO_ENABLED=1.

When I try the go env command it is in my environment but still cannot generate sqlc

go env

GO111MODULE=''
GOARCH='amd64'
GOBIN=''
GOCACHE='/home/user/.cache/go-build'
GOENV='/home/user/.config/go/env'
.
.
.
GOHOSTARCH='amd64'
GOHOSTOS='linux'
GOINSECURE=''
GOMODCACHE='/home/user/go/pkg/mod'
GONOPROXY=''
GONOSUMDB=''
GOOS='linux'
GOPATH='/home/user/go'
GOPRIVATE=''
GOPROXY='https://proxy.golang.org,direct'
GOROOT='/usr/local/go'
GOSUMDB='sum.golang.org'
.
.
.
CGO_ENABLED='1'
GOMOD='/mnt/c/Users/User/Desktop/Golang/main/go.mod'

Workaround

I was able to replicate this on a fresh install of WSL2 Ubuntu 22.04 (running on Win 11). Using the new image, I installed Go (1.21.4) and sqlc (v1.23.0) and then ran sqlcgenerate (using the example store from the tutorial mentioned in the question library) and received the error:

# package database
sql/schema/001_users.sql:1:1: the PostgreSQL engine requires cgo. Please set CGO_ENABLED=1.
sql/schema/002_users_apikey.sql:1:1: the PostgreSQL engine requires cgo. Please set CGO_ENABLED=1.
sql/schema/003_feeds.sql:1:1: the PostgreSQL engine requires cgo. Please set CGO_ENABLED=1.

Interestingly, everything works fine under Ubuntu 20.04 (also on WSL2).

There is an issue with compatibility of Ubuntu 20.04 and CGO-enabled binaries for 22.04 (didn't really look into this in detail!); so it seems worth trying to reinstall sqlc and enable CGO

go env -w CGO_ENABLED=1
sudo apt update
sudo apt install gcc
go install github.com/sqlc-dev/sqlc/cmd/sqlc@latest

After doing this, sqlc worked (successfully generated the go file for the 6-createfeed example, no issues).

NOTE: Since this works, I thought I'd add a comment for any similar a>sqlc questions and come across this comment suggesting the same basic solution (I looked at this last night This was not discovered at the time).

The above is the detailed content of Golang cannot 'sqlc generate'. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:stackoverflow.com. If there is any infringement, please contact admin@php.cn delete