Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to build a release version binary in Go?

Tags:

c

go

build

release

In C, we can build a debug version or a release version of the binary files (the object files and the executable). How can we do this in Go?

like image 265
catric mia Avatar asked Apr 13 '15 06:04

catric mia


People also ask

Does go compile to binary?

To complete the list, go run compiles your application into a temporary folder, and starts that executable binary. When the app exits, it properly cleans up the temporary files.

Where does go build put binaries?

This command does perform the exact operation as go build but places the binary in $GOPATH/bin` directory alongside the binaries of third-party tools installed via go get now if you run $GOPATH/bin/hello you will see Hello, world!

What are binaries in go?

Go Binaries is an open-source server allowing non-Go users to quickly install tools written in Golang, without installing the Go compiler or a package manager — all you need is curl .


2 Answers

In Go, it isn't typical to have a debug version or a release version.

By default, go build combines symbol and debug info with binary files. However, you can remove the symbol and debug info with go build -ldflags "-s -w".

like image 182
hiwjd0 Avatar answered Sep 23 '22 23:09

hiwjd0


You can instruct the linker to strip debug symbols by using

go install -ldflags '-s' 

I just tried it on a fairly large executable (one of the GXUI samples), and this reduced it from ~16M to ~10M. As always, your mileage may vary...

Here is a full list of all linker options.

like image 26
rob74 Avatar answered Sep 25 '22 23:09

rob74