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

  • 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
  
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.

主站蜘蛛池模板: 芜湖市| 张家港市| 城步| 雷州市| 多伦县| 资阳市| 迁西县| 临武县| 天水市| 新乡市| 临洮县| 沽源县| 保定市| 政和县| 成都市| 昌平区| 沧源| 临洮县| 康马县| 策勒县| 托克托县| 逊克县| 六枝特区| 新河县| 喀什市| 和田县| 桂东县| 丰台区| 潼南县| 宿州市| 大化| 永城市| 桃园县| 龙门县| 宿迁市| 增城市| 泗洪县| 内丘县| 定远县| 九寨沟县| 玉环县|