Go Build
- Go package guidelines
- 常用
-trimpath -ldflags '-s -w -extldflags "-static"'
- 参考
caution
- c-shared
- musl 环境不支持构建 c-shared golang/go#13492 runtime: c-shared builds fail with musllibc
- windows 下构建 c-shared 需要使用 TDM-GCC
- 无法 dlclose, offload c-shared #11100
- 涉及到 VM
env | note | macOS |
---|---|---|
GOENV | ~/Library/Application Support/go/env | |
GOCACHE | ~/Library/Caches/go-build | |
GOMODCACHE | ~/go/pkg/mod | |
GOTOOLDIR | ||
GOMOD | go.mod 位置 | |
GOWORK |
# 所有交叉编译列表
go tool dist list
# 包含 cgo 支持情况
go tool dist list -json
# 移除 mod 缓存
go clean -modcache
buildmode | desc |
---|---|
archive | build non-main, .a |
c-archive | main+imports, cgo //export |
c-shared | main+imports, cgo //export |
default | main+non-main, exec, .a |
exe | main+imports,exec, 忽略非 main 包 |
pie | main+imports, exec, pie |
plugin | main+imports, plugin, 忽略非 main 包 |
non-main, for -linkshared - module 出来后 异常 |
ldflags | desc |
---|---|
-w | disable DWARF generation |
-s | disable symbol table |
-X 'wener.me/gou/build.Version=123' | add definition |
-linkmode=external | |
-extldflags "$LDFLAGS" |
gcflags | desc |
---|---|
-N | Disable optimizations |
-l | Disable inlining |
flags | desc |
---|---|
-modcacherw | 新的 mod 缓存 rw - 可以 rm -rf |
-trimpath | 移除环境相关路径,reproduceable |
-ldflags "$GOLDFLAGS" | |
-mod=readonly | 不动 go.mod |
-buildmode=pie |
# https://pkg.go.dev/cmd/compile
go tool compile -help
env | default | desc |
---|---|---|
GOMAXPROCS | Max Thread | |
GOGC | 100 | off 关闭 GC |
GOOS | ||
GOARCH |