This should work:

Copygo env -w CGO_ENABLED=1

But if you don't have a C compiler installed on your machine, you will get another error message after setting this variable and trying to use go run -race .:

cgo: C compiler "gcc" not found: exec: "gcc": executable file not found in %PATH%

If so, here are VS Code's instructions on how to install it

  • Windows: https://code.visualstudio.com/docs/cpp/config-mingw
  • Linux: https://code.visualstudio.com/docs/cpp/config-linux
Answer from Krista Korund on Stack Overflow
🌐
Go Packages
pkg.go.dev › cmd › cgo
cgo command - cmd/cgo - Go Packages
April 7, 2026 - It is disabled by default when ... 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....
Discussions

Getting CGO_ENABLED=0 when running a go project that uses github.com/mattn/go-sqlite3 library?
No, CGO is not enabled by default; you'll need to provide a C compiler for your target architecture. Alternatively, if you don't care that much about performance, there's a pure-Go version: https://gitlab.com/cznic/sqlite More on reddit.com
🌐 r/golang
20
8
June 3, 2023
`CGO_ENABLED` cannot be set to `1`
When Task is invoked with CGO_ENABLED=0, the environment variable cannot be set to 1 in the env section of a task declaration. This is reproducible using the following Taskfile. version: 3 tasks: d... More on github.com
🌐 github.com
7
July 21, 2023
What is the consequence of using CGO_ENABLED=0?

With CGO_ENABLED=0 you got a staticaly-linked binary (see: https://en.wikipedia.org/wiki/Static_build) so it will run without any external dependencies (you can buld your dockers from 'scratch' image) Like that: https://github.com/s0rg/microapp/blob/master/Dockerfile

More on reddit.com
🌐 r/golang
33
41
September 5, 2021
cmd/build: cgo cross build need to explictly set `CGO_ENABLED=1`
Go version go version go1.24.1 linux/arm64 Output of go env in your module/workspace: AR='ar' CC='gcc' CGO_CFLAGS='-O2 -g' CGO_CPPFLAGS='' CGO_CXXFLAGS='-O2 -g&#... More on github.com
🌐 github.com
1
March 21, 2025
Top answer
1 of 2
18

Many things are only available as C libraries, and re-implementing that all in Go would be costly. cgo has its downsides, but it can be a good trade-off. Even the standard library uses it (net for DNS lookups, os/user for user lookups) because it doesn't re-implement 100% of the behaviour in Go.

Cross-compiling C code is still rather hard; you'll need the target architecture's C compiler and toolchain (e.g. CC=aarch64-linux-musl-gccgo build to build an arm64 binary). None of that is installed by default so for most people cgo simply won't work when cross-compiling; they need to take manual steps to set it up first.

cgo often isn't strictly required (like in the net and os/user packages), so disabling it by default seems the most user-friendly option.

But there are no such constraints on the native platform, and it's expected to work by default without any user setup; so why not enable it by default?

2 of 2
7

If you're running on an Alpine image, it is impossible to compile and run Go programs in Alpine images right away. You must disable CGO by setting the environment variable CGO_ENABLED to false (the default value is true).

You can do this either by:

  • Adding go env -w CGO_ENABLED=0 like Robert mentions in his comment or,
  • Setting the env value prefixing it in the go command, e.g.: CGO_ENABLED=0 GOARCH=amd64 GOOS=linux go build -ldflags '-s -w' -tags lambda.norpc -o bin/<YOUR_FUNC>/bootstrap <YOUR_PATH>/main.go

See: https://megamorf.gitlab.io/2019/09/08/alpine-go-builds-with-cgo-enabled/

🌐
Medium
medium.com › @pengcheng1222 › exploring-cgo-enabled-in-go-23cf5cf2fe88
Golang — Exploring CGO_ENABLED in Go | by Allen Ning | Medium
November 20, 2024 - CGO_ENABLED is a pivotal environment variable in Go’s build process, controlling the use of cgo, the tool that facilitates calling C code from Go. By default, its value varies based on the operating system and architecture, typically being set to 1 on standard platforms like Windows, macOS, and Linux (for amd64, 386, and arm architectures), enabling the integration of C libraries in Go applications.
🌐
Boinkor
boinkor.net › 2023 › 05 › building-a-golang-program-with-cgo
Building a golang program with cgo - Andreas Fuchs’ Journal
May 25, 2023 - There are two main things you need to do: First, explicitly opt out of disabling the usage of the cgo compiler by setting CGO_ENABLED=1 on the compiler’s process environment. (This defaults to on, but your environment might have it turned off!
🌐
GoLinuxCloud
golinuxcloud.com › home › programming › getting started with cgo using visual studio code
Getting started with CGO using Visual Studio Code | GoLinuxCloud
January 7, 2024 - It is disabled by default when cross-compiling. You can control this 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.
🌐
Bytebase
bytebase.com › blog › how-to-cross-compile-with-cgo-use-goreleaser-and-github-action
How to cross compile with CGO using GoReleaser and GitHub Actions
August 24, 2022 - To enable CGO in GoReleaser, you only need to add "CGO_ENABLED=1" to the corresponding "env" entry in the GoReleaser configuration file.
Find elsewhere
🌐
GitHub
github.com › orgs › community › discussions › 46166
Compile a Go project with `CGO_ENABLED=1` across Linux/Darwin/Windows · community · Discussion #46166
February 2, 2023 - Or you can just include CGO_ENABLED=1 in the command line, run sections just run your commands in a shell. For example (modified excerpt from that starter workflow): jobs: build: strategy: matrix: os: [ubuntu-latest, windows-latest, macos-latest] ...
🌐
GitHub
github.com › golangci › golangci-lint-action › discussions › 1195
How are we supposed to set CGO_ENABLED=1 with this action? · golangci/golangci-lint-action · Discussion #1195
December 20, 2025 - I have a build that requires CGO_ENABLED to be set to lint properly. On my local exporting this var works just fine with the CLI. I tried setting it with: env: CGO_ENABLED: 1 on the action step but...
Author   golangci
🌐
Matrix
matrix-org.github.io › go-neb › pkg › C › index.html
cgo - The Go Programming Language
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. You can control this 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.
🌐
GitHub
github.com › go-task › task › issues › 1272
`CGO_ENABLED` cannot be set to `1` · Issue #1272 · go-task/task
July 21, 2023 - When Task is invoked with CGO_ENABLED=0, the environment variable cannot be set to 1 in the env section of a task declaration. This is reproducible using the following Taskfile. version: 3 tasks: default: env: CGO_ENABLED: 1 cmds: - go e...
Author   go-task
🌐
Reddit
reddit.com › r/golang › what is the consequence of using cgo_enabled=0?
r/golang on Reddit: What is the consequence of using CGO_ENABLED=0?
September 5, 2021 -

Is it a bad idea to set this environment variable to 0? From what I read, setting it to 1 means there should be a gcc compiler installed in the working system. Is this a correct interpretation? Why is it 1 by default?

I was trying to containerise my go application using the below docker file:

FROM golang:latest as builder

ENV GOOS=linux

COPY ./ /go/src/hello_world

WORKDIR /go/src/hello_world

RUN go build .

FROM alpine:latest

WORKDIR /usr/home

COPY --from=builder /go/src/hello_world/hello_world /usr/home

ENTRYPOINT ["./hello_world"]

This was giving me error but based on my findings I put ENV CGO_ENABLED=0 during build and it started working fine.

When is it required to be set to 1 and why did I have to explicitly set to 0 in my case?

P.S. I am very new to go so any resources on this would be appreciated.

🌐
Patchwork
patchwork.yoctoproject.org › project › oe-core › patch › 20220825133630.1659896-3-ross.burton@arm.com
[3/6] oeqa/gotoolchain: set CGO_ENABLED=1 - Patchwork
diff --git a/meta/lib/oeqa/selftest/cases/gotoolchain.py b/meta/lib/oeqa/selftest/cases/gotoolchain.py index 345f533379c..978898b86f1 100644 --- a/meta/lib/oeqa/selftest/cases/gotoolchain.py +++ b/meta/lib/oeqa/selftest/cases/gotoolchain.py @@ -51,6 +51,7 @@ class oeGoToolchainSelfTest(OESelftestTestCase): cmd = cmd + ". %s; " % self.env_SDK cmd = cmd + "export GOPATH=%s; " % self.go_path cmd = cmd + "export GOFLAGS=-modcacherw; " + cmd = cmd + "export CGO_ENABLED=1; " cmd = cmd + "${CROSS_COMPILE}go %s" % gocmd return runCmd(cmd).status
🌐
GitLab
gitlab.com › gitlab.org › gitlab-runner › merge requests › !3413
Enable CGO_ENABLED by default in golang-fips compiler (!3413) · Merge requests · GitLab.org / gitlab-runner · GitLab
April 28, 2022 - If CGO_ENABLED=0, then the golang-fips compiler will default to this mode by default, which is counter to the default Golang behavior. Any binary that needs FIPS support would need to switch on CGO_ENABLD=1.
🌐
GitHub
github.com › golang › go › issues › 72988
cmd/build: cgo cross build need to explictly set `CGO_ENABLED=1` · Issue #72988 · golang/go
March 21, 2025 - Go version go version go1.24.1 linux/arm64 Output of go env in your module/workspace: AR='ar' CC='gcc' CGO_CFLAGS='-O2 -g' CGO_CPPFLAGS='' CGO_CXXFLAGS='-O2 -g' CGO_ENABLED='1' CGO_FFLAGS='-O2 -g' CGO_LDFLAGS='-O2 -g' CXX='g++' GCCGO='gc...
Author   golang
🌐
Go Forum
forum.golangbridge.org › getting help
Setting up the environment - Getting Help - Go Forum
February 24, 2019 - Hi, I have no experience programming go, however i did some PHP for serveral years. I did some research and Go looks very interesting to learn. As a webdeveloper there was no ‘hard’ setup for any environment, just writ…
🌐
JetBrains
youtrack.jetbrains.com › issue › GO-5016 › GoLand-does-not-pass-CGO-variable-during-build
GoLand does not pass CGO variable during build : GO-5016
{{ (>_<) }} This version of your browser is not supported. Try upgrading to the latest stable version. Something went seriously wrong