Purpose: This variable allows you to specify additional flags for the go command.
Usage: Set GOFLAGS to include additional command-line flags when running go commands.
-
Enabling Verbose Output:
GOFLAGS=-v go build main.go
This command compiles the
main.go
program with the-v
flag, which stands for “verbose.” It provides detailed information about the build process, including which files are being compiled and linked. -
Setting Output Directory:
GOFLAGS=-o myapp go build main.go
Here, the
-o
flag is used to specify the output file’s name asmyapp
. This can be handy when you want to customize the name of the generated binary. -
Building with Specific Tags:
GOFLAGS=-tags=mytag go build main.go
You can use the
-tags
flag to build your Go program with specific build tags. Build tags are a way to include or exclude certain parts of your code during compilation based on conditions defined in your source code. -
Cross-Compiling with
GOOS
andGOARCH
:GOFLAGS="-v -ldflags '-s -w' -trimpath" GOOS=linux GOARCH=amd64 go build main.go
In this example,
GOFLAGS
is used to pass a combination of flags to customize the build process. It includes the-v
flag for verbose output,-ldflags
to specify linker flags, and-trimpath
to trim the source path. Additionally,GOOS
andGOARCH
are set to cross-compile the program for Linux on an AMD64 architecture. -
Modifying Build Mode:
GOFLAGS="-buildmode=shared" go build main.go
Here,
GOFLAGS
is used to specify the build mode as “shared.” This can be useful when you want to create a shared library or plugin using Go. -
Running Tests with Parallelism:
GOFLAGS="-p 2" go test ./…
You can control the parallelism of your Go test runs using the
-p
flag. In this example,GOFLAGS
is set to run tests with a maximum of 2 parallel processes. -
Running Benchmarks with Custom Timing:
GOFLAGS="-benchtime=2s" go test -bench=. ./…
The
-benchtime
flag allows you to specify the duration for which benchmark tests should run. Here,GOFLAGS
is used to set the benchmark time to 2 seconds. -
Using Custom Environment Variables:
GOFLAGS="GOARCH=arm64 GOOS=linux" go build main.go
You can use
GOFLAGS
to set custom environment variables for the Go build process. In this case, it specifies theGOARCH
andGOOS
variables to cross-compile for ARM64 architecture on Linux.