Go 程序未使用 1.4.2 静态链接

Go program not statically linked using 1.4.2

尝试构建运行 http 服务器的 go 程序的静态链接版本,并使用 net 包来确定和解析传入请求的 IP 地址。使用此构建语句:

CGO_ENABLED=0 go install -a -ldflags '-s' .

我程序中的序言:

package main

import (
    "encoding/json"
    "errors"
    "fmt"
    "log"
    "net"
    "net/http"
    "path/filepath"
    "strings"

    "golang.org/x/blog/content/context/userip"

    "github.com/oschwald/maxminddb-golang"
)

这适用于 go 1.3 构建,生成静态链接程序,但不适用于 go 1.4.2。构建成功,但是程序没有静态链接

有人知道这是怎么回事吗?

更多搜索发现 this thread in the golang issue tracker 由于 1.3 和 1.4 之间的变化,无法静态 link 导入 net 包的程序。

向下阅读我发现 this workaround suggested by ianlancetaylor 使用 -installsuffix 开关指定 cgo。这使我的构建声明:

CGO_ENABLED=0 go install -a -installsuffix cgo -ldflags '-s' .

这对我有用。构建成功并生成了静态 linked 程序。