Getting errors when running go project that uses github.com/mattn/go-sqlite3 library

Whenever I run the code using go run main.go, as soon as the code calls the function for creating a table in the sqlite database,I get this error in the command line.

2023/06/02 22:45:23 Binary was compiled with ‘CGO_ENABLED=0’, go-sqlite3 requires cgo to work. This is a stub

What can I do to manually enable cgo, as I thought cgo was enabled automatically?

Hi @Nucl3arSn3k ,

If you use Bash, try CGO_ENABLED=1 go run main.go. (The syntax might differ for other shells.)

The cgo command documentation says,

The cgo tool is enabled by default for native builds on systems where it is expected to work. It is disabled by default when cross-compiling as well as when the CC environment variable is unset and the default C compiler (typically gcc or clang) cannot be found on the system PATH. You can override the default by setting the CGO_ENABLED environment variable when running the go tool: set it to 1 to enable the use of cgo, and to 0 to disable it.

You can check the value with

go env CGO_ENABLED

and update it wtih

go env -w CGO_ENABLED=<0 or1>

for your local Go toolchain.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.