douchen4547 2018-09-09 02:58
浏览 28
已采纳

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

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 2018-09-09 06:22
    关注

    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)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 多址通信方式的抗噪声性能和系统容量对比
  • ¥15 winform的chart曲线生成时有凸起
  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据
  • ¥20 软件测试决策法疑问求解答