- 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.
- Oracle從入門到精通(第3版)
- ExtGWT Rich Internet Application Cookbook
- Java游戲服務器架構實戰
- 基于免疫進化的算法及應用研究
- Learning Apache Mahout Classification
- R語言與網絡輿情處理
- 從Java到Web程序設計教程
- Mastering Akka
- Oracle GoldenGate 12c Implementer's Guide
- Mastering C++ Multithreading
- JSP程序設計實例教程(第2版)
- 單片機原理及應用技術
- 物聯網系統架構設計與邊緣計算(原書第2版)
- IBM DB2 9.7 Advanced Application Developer Cookbook
- Practical Time Series Analysis