How to reduce compiled file size?
The 2016 answer:
1. Use Go 1.7
2. Compile with go build -ldflags "-s -w"
➜ ls -lh hello
-rwxr-xr-x 1 oneofone oneofone 976K May 26 20:49 hello*
3. Then use upx
, goupx
is no longer needed since 1.6.
➜ ls -lh hello
-rwxr-xr-x 1 oneofone oneofone 367K May 26 20:49 hello*
Go binaries are large because they are statically linked (except for library bindings using cgo). Try statically linking a C program and you'll see it grow to a comparable size.
If this is really a problem for you (which I have a hard time believing), you can compile with gccgo and dynamically link.
Note: This answer is outdated
Please note that this answer is outdated. Please refer to the other higher-voted answers. I would like to delete this post, accepted answers can't be deleted though.
Is it a problem that the file is larger? I don't know Go but I would assume that it statically links some runtime lib which is not the case for the C program. But probably that is nothing to worry about as soon as your program gets larger.
As described here, statically linking the Go runtime is the default. That page also tells you how to set up for dynamic linking.
If you are using a Unix-based system (e.g. Linux or Mac OSX) you could try removing the debugging information included in the executable by building it with the -w flag:
go build -ldflags "-w" prog.go
The file sizes are reduced dramatically.
For more details visit the GDB's page: http://golang.org/doc/gdb