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

Golang cannot 'sqlc generate'

王林
Release: 2024-02-09 11:09:18
forward
1120 people have browsed it

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.
Copy after login
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.
Copy after login

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'
Copy after login

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.
Copy after login

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
Copy after login

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!

source:stackoverflow.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template