Static linking Go binaries

Posted on Fri 24 February 2017 in go

By default, go (<=1.8) compiles a dynamic linked binary if you run go build.

It can be verified with the file command:

$ file go_executable
go_executable: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib/ld-musl-x86_64.so.1, not stripped, with debug_info

Add -ldflags "-linkmode external -extldflags -static" argument to the go build command, to build static linked binaries.

It produces the following executable:

$ file go_executable
go_executable: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), statically linked, not stripped, with debug_info