官术网_书友最值得收藏!

Checking the size of the executable file

So, after successfully compiling hw.go, you might want to check the size of the generated executable file:

$ ls -l hw
-rwxr-xr-x  1 mtsouk  staff  1628192 Feb  9 22:29 hw
$ file hw
hw: Mach-O 64-bit executable x86_64  

Compiling the same Go program on a Linux machine will create the following file:

$ go versiongo 
go version go1.3.3 linux/amd64
$ go build hw.go
$ ls -l hw
-rwxr-xr-x 1 mtsouk mtsouk 1823712 Feb 18 17:35 hw
$ file hw
hw: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), statically linked, not stripped
  
To get a better sense of how big the Go executable is, consider that the executable for the same program written in C is about 8432 bytes!

So, you might ask why such a huge executable file for such a small program? The main reason is that Go executable files are statically build, which means that they require no external libraries to run. The use of the strip(1) command can make the generated executable files a little smaller, but do not expect miracles:

$ strip hw
$ ls -l hw
-rwxr-xr-x  1 mtsouk  staff  1540096 Feb 18 17:41 hw

The previous process has nothing to do with Go itself because strip(1) is a Unix command that removes or modifies the symbol table of files and therefore reduces their size. Go can perform the work of the strip(1) command on its own and create smaller executable files, but this method does not always work:

$ ls -l hw
-rwxr-xr-x 1 mtsouk mtsouk 1823712 Feb 18 17:35 hw
$ CGO_ENABLED=0 go build -ldflags "-s" -a hw.go
$ ls -l hw
-rwxr-xr-x 1 mtsouk mtsouk 1328032 Feb 18 17:44 hw
$ file hw
hw: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), statically linked, stripped

The preceding output is from a Linux machine; when the same compilation command is used on a macOS machine, it will make no difference to the size of the executable file.

主站蜘蛛池模板: 北碚区| 邛崃市| 新干县| 保靖县| 杭州市| 诸暨市| 鄯善县| 巧家县| 海宁市| 车险| 亚东县| 尚义县| 蒲江县| 大厂| 兴国县| 桃园市| 阿城市| 马龙县| 祁门县| 晋宁县| 正镶白旗| 谷城县| 南雄市| 甘孜县| 宽甸| 双峰县| 隆昌县| 习水县| 三穗县| 隆子县| 西充县| 古丈县| 余江县| 浮山县| 航空| 扶风县| 榕江县| 信丰县| 贡山| 昌吉市| 奇台县|