- Go Systems Programming
- Mihalis Tsoukalos
- 332字
- 2021-07-02 18:07:56
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
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.
- Mastering JavaScript Object-Oriented Programming
- Visual FoxPro程序設(shè)計教程(第3版)
- 密碼學(xué)原理與Java實現(xiàn)
- Pandas Cookbook
- Python程序設(shè)計(第3版)
- Learning AWS Lumberyard Game Development
- AngularJS深度剖析與最佳實踐
- 新手學(xué)Visual C# 2008程序設(shè)計
- 量化金融R語言高級教程
- C# 8.0核心技術(shù)指南(原書第8版)
- Visual C#通用范例開發(fā)金典
- Machine Learning in Java
- 深入剖析Java虛擬機:源碼剖析與實例詳解(基礎(chǔ)卷)
- Spring Boot+Vue全棧開發(fā)實戰(zhàn)
- Visual C#.NET Web應(yīng)用程序設(shè)計