为什么在使用供应商目录中的依赖关系与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

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)