尝试构建一个运行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 之间的更改,无法静态链接导入 net
包的程序。
往下读我发现this workaround suggested by ianlancetaylor使用 -installsuffix
开关指定 cgo
。这就是我的构建声明:
CGO_ENABLED=0 go install -a -installsuffix cgo -ldflags '-s' .
这对我有用。构建成功并生成静态链接程序。
关于Go程序未使用1.4.2静态链接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30954159/