4

I've searched for recent information on building compact Go executables, without success. Most information appears to be several years old.

My program: the standard hello world as found on Rosettacode.org. With Fortran I get an exe file under 100 KB. With Go the exe file is whopping 2400 KB.

My questions: are there compiler switches or pragmas that I should be using? Are there other tools or tricks I am not aware of (I am a beginner with Go). What are the future plans for Go regarding executable size? What is it like under Linux - same thing? What happens to exe files on large programs?

Background: I'm using go 1.5 installed on Windows 7. My project is writing (actually migrating from other languages) some command line programs. Having 20 or more large Go exe files eats up a lot of space. OK, I know space is cheap so I can live with it. However, I believe clarity on the matter will help many people.

Fred
  • 564
  • 10
  • 21
  • 3
    Not sure if SE is the right place to discuss so many questions. Perhaps the Go mailing list would be more appropriate. – laurent Sep 17 '15 at 09:48
  • I'll be happy to hear about the compiler switches or pragmas, especially if they are not well documented. – Fred Sep 17 '15 at 09:51
  • 3
    Read this: [Reason for huge size of compiled executable of Go](http://stackoverflow.com/questions/28576173/reason-for-huge-size-of-compiled-executable-of-go). The Go executable is big because it contains a runtime with support for gc and run-time reflection. The Fortran exe does not provide that to you (or the level comparable to that of Go). Worry not, the runtime is only added once, if your code grows, it will not be linear to the size of the output. – icza Sep 17 '15 at 09:53
  • @icza, many thanks. Glad to hear it is not linear growth. – Fred Sep 17 '15 at 10:01

2 Answers2

4

A Go program is big because it contains garbage collaction code, goroutine scheduling code and besides that it has nearly all libraries statically linked. It is so by design.

FAQ Why is my trivial program such a large binary

Grzegorz Żur
  • 47,257
  • 14
  • 109
  • 105
3

To reduce the executable size, you can instruct the linker to strip debug symbols by using one of the following:

go install -ldflags '-s'
go install -ldflags '-s -w'

I tried it on a fairly large executable (one of the GXUI samples), and this reduced it from ~16M to ~10M. Adding -w to -s didn't make any difference, but as always, your mileage may vary...

Here is a full list of all linker options.

rob74
  • 4,939
  • 29
  • 31
  • Note that this is the same as running `strip` on a binary. – Martin Tournoij Mar 28 '16 at 22:20
  • 1
    @Carpetsmoker: not at all - in the method recommended by me, the symbols are stripped by the Go linker, which is much more likely to work reliably than a third party utility like `strip`. This is not far fetched, apparently `strip` has produced problems with Go binaries in the past - see [here](https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=717172) (quote: "stripping a go binary will produce anywhere from no, to subtle, to outright execution failure") – rob74 Mar 29 '16 at 21:39
  • Ah; thanks for the clarification; it has the *same effect* as running `strip` on a binary :-) – Martin Tournoij Mar 29 '16 at 21:46
  • @Carpetsmoker: I haven't tried it myself, but if I understood correctly, `strip` will have the same effect _if you are lucky_. If not, your stripped binary might be broken. [The Docker guys are really adamant about this too](https://github.com/docker/docker/blob/master/project/PACKAGERS.md#stripping-binaries): "Please, please, please do not strip any compiled binaries". – rob74 Mar 29 '16 at 22:27