douchen4547
douchen4547
2018-09-09 02:58

为什么在使用供应商目录中的依赖关系与gopath中的依赖关系进行构建时,go应用程序二进制文件的大小不同?

  • IT行业问题
  • 计算机技术
  • it技术
  • 编程语言问答
  • 互联网问答
已采纳

I have a main.go file with a glide.yaml file (see below for source code) in a clean gopath (No other repositories)

Running go get -u ./... then go build main.go generates a binary of size 2377872 bytes.

Cleaning the gopath of any repos that were cloned from go get, and running glide update then go build main.go generates a binary of size 2457328 bytes.

Why are binaries of different sizes, if there was no code changes? What does go build do differently? Why does having a vendor directory (or not having one) affect this size?

Software versions used

glide version 0.13.1
go version go1.10.3 darwin/amd64

main.go

package main

import (
    log "github.com/sirupsen/logrus"
)

func main () {
    log.WithFields(log.Fields{
        "qqs": "q1",
    }).Info("Why are binaries different?")
}

glide.yaml

package: github.com/chuyval/qqs/q1
import:
- package: github.com/sirupsen/logrus
  version: 1.0.6
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

1条回答

  • duangu1878 duangu1878 3年前

    go build by default includes debugging information in the executable, including the path of the source code files.

    When you have a vendor/ directory the path of the source files will be longer than when you the source files lie inside GOPATH. As a result, the debugging information will take up more space.

    Try comparing the size of the built binary by telling go build to exclude the debugging information like this.

    go build -ldflags=-s ./
    

    That reduces the difference in size. See https://golang.org/cmd/link/ for more details about the linker flags.

    (Edit: the source file paths are included when debugging information is not included as well as is evidenced by stack traces from a panic(), so this answer is not complete)

    点赞 评论 复制链接分享