现象

golang:alpineDocker镜像跑gometalinter的时候发现它错误地处理了/usr/local/go/src标准库中的文件

$ gometalinter.v2
../../../usr/local/go/src/net/lookup_unix.go:80:24:warning: undeclared name: cgoLookupHost (maligned)
../../../usr/local/go/src/net/lookup_unix.go:95:24:warning: undeclared name: cgoLookupIP (maligned)
../../../usr/local/go/src/net/lookup_unix.go:80:24:warning: error return value not checked (undeclared name: cgoLookupHost) (errcheck)
../../../usr/local/go/src/net/lookup_unix.go:95:24:warning: error return value not checked (undeclared name: cgoLookupIP) (errcheck)
../../../usr/local/go/src/net/lookup_unix.go:80:24:warning: unused variable or constant undeclared name: cgoLookupHost (varcheck)
../../../usr/local/go/src/net/lookup_unix.go:95:24:warning: unused variable or constant undeclared name: cgoLookupIP (varcheck)
...

原因

许多linter使用了golang.org/x/tools/go/loader这个库而它在默认情况下会调用go tool cgo预处理cgo文件除非设置环境变量CGO_ENABLED=0或者Config.Build.CgoEnabled[1]

golang:alpine因为需要控制镜像体积未安装GCCC标准库

解决方案

安装GCCC标准库

$ apk add --no-cache \
    gcc \
    musl-dev

如果没有用到CGO也可以通过环境变量禁用CGO

$ CGO_ENABLED=0 gometalinter.v2

  1. https://godoc.org/golang.org/x/tools/go/loader#Config ↩︎