Safeguard
Supply Chain•

Does go get run code from a dependency when you install it?

Fetching and building Go modules runs no install scripts by design, but init functions run in your program, and go generate or cgo need your attention.

Safeguard Security Team
3 min read

No. Fetching a Go module with go get or building it does not execute code from that module. The Go team calls the absence of install-time scripts an explicit security design goal. But once a package is compiled into your program, its init functions run when the program starts.

What the Go team says

The Go supply chain article states that fetching and building do not execute code, and describes it as an explicit security design goal of the toolchain. It also states a limit: within a build, there is no security boundary, because any package that contributes to the build can define an init function.

Where code does and does not run

MomentDependency code runs?
go get or go mod downloadNo
go buildCompiled, not executed, though cgo invokes a C compiler
Your program startsYes, init functions and everything you call
go testYes, test code of your own packages
go generateOnly if you run it, and it runs commands named in source comments

The last two rows are about your own workflow. go generate runs commands listed in //go:generate comments in your source. It is never run by go build, so the risk is a developer running a generator they did not read.

Why this reduces some attacks

Package managers with install scripts let a malicious release run on a developer laptop the moment it is installed. Go removes that step. It also has no package repository accounts, which the Go team notes means there are no registry credentials to steal and publish with.

That is not the same as no risk. A malicious module that you import still executes at runtime with your program's permissions.

Practical steps

  1. Review new imports as code that will run in production, because it will.
  2. Search a new dependency for func init() and for packages with side effects at import, such as network calls.
  3. Be careful with go generate in someone else's repository. Read the directives first.
  4. Be careful with cgo, which compiles C code and runs a compiler as part of the build.
  5. Keep dependency counts low, and prefer the standard library where it covers the need.
  6. Use a reachability aware scanner so you know which vulnerable functions your program can actually call.

Try it yourself

Click through it at your own pace, or open it full screen. Look at how findings change when only reachable code is counted.

Where Safeguard fits

Safeguard offers reachability analysis and dependency scanning, plus policy gates on pull requests. A free scan shows findings for a Go repository.

Quick check

Can a Go dependency run code at install time?

The Go team says no: fetching and building do not execute code, by design.

Can it run code at all?

Yes. Once imported, its init functions and any code you call run inside your program.

Sources

Last reviewed: 19 October 2026.

Never miss an update

Weekly insights on software supply chain security, delivered to your inbox.

Self-healing security runs on Safeguard.

Your first fix PR is minutes away.

No sales call required, even your agent can complete the purchase over MCP.