Passing an optimization flag to a Go compiler?

Solution 1:

Actually no explicit flags, this Go wiki page lists optimizations done by the Go compiler and there was a discussion around this topic in golang-nuts groups.

You can turn off optimization and inlining in Go gc compilers for debugging.

-gcflags '-N -l'
  • -N : Disable optimizations
  • -l : Disable inlining

Solution 2:

If you're looking to optimize the binary size, you can omit the symbol table, debug information and the DWARF symbol table by passing -s and -w to the Go linker:

$ go build -o mybinary -ldflags="-s -w" src.go

(source blog post which includes some benchmarks)