index.html :: rss :: github :: telegram :: email

go: compiler flags

22 Aug 2024

enable escape analysis

-gcflags="-m -m"

GC reports

GODEBUG=gctrace=1 ./app

Set the garbage collection target percentage, 150% in that case

GOGC=150 GODEBUG=gctrace=1,schedtrace=1000 ./app

report scheduler stats every 1000ms

GODEBUG=schedtrace=1000 ./app

Disable compiler optimizations and inlining

-gcflags"-N"
-dwarf = generate dwarf symbols
-l = disable inlining
-j = debug runtime-initialized variables

build modes

-buildmode=archive statically linked .a

-buildmode=c-shared .so, exports only symbols annotated with "//export" comment

strip binaries

GOOS=linux GOARCH=amd64 CGO_ENABLED=0 go build -x -v -a -ldflags="-w -s" \
	-gcflags=-trimpath=/Users/alex/go/path-to-project \
	-asmflags=-trimpath=/Users/alex/go/path-to-project \
	-o ./myapp

/Users/alex/go/path-to-project may be replaced with $(pwd).


more: go tool compile -help, also https://pkg.go.dev/cmd/compile