当前位置: 首页 > 工具软件 > gobuild > 使用案例 >

go build 报错

明越
2023-12-01

ps:对go 不太熟悉,只因利用docker镜像golang 编译时报错,所以记录下
dockerfile

FROM golang
WORKDIR /go/src/app
ADD . /go/src/app
RUN go get -u -v github.com/kardianos/govendor    ##安装govendor,govendor用来管理项目的依赖
RUN govendor sync    ##本地存在 vendor.json 时候拉去依赖包,匹配所记录的版本
RUN GOOS=linux GOARCH=386 go build -v -o /go/src/app/app-server   

在doker中测试执行:
docker run --rm -it golang:latest sh

# GOOS=linux GOARCH=386 go build -v -o /go/src/app/app-server
go: cannot find main module, but found vendor/vendor.json in /go/src/app
	to create a module there, run:
	go mod init

解决办法:

go mod init 

接着报错:

# go mod init
go: creating new go.mod: module github.com/cnych/docker-multi-stage-demo

go: copying requirements from vendor/vendor.json
go: to add module requirements and sums:
	go mod tidy

解决办法:

go mod tidy

接着报错:

# GOOS=linux GOARCH=386 go build -v -o /go/src/app/app-server
go: inconsistent vendoring in /go/src/app:
	github.com/gin-contrib/sse@v0.0.0-20170109093832-22d885f9ecc7: is explicitly required in go.mod, but not marked as explicit in vendor/modules.txt
	github.com/gin-gonic/gin@v1.1.5-0.20180126034611-783c7ee9c14e: is explicitly required in go.mod, but not marked as explicit in vendor/modules.txt
	github.com/golang/protobuf@v1.0.1-0.20180202184318-bbd03ef6da3a: is explicitly required in go.mod, but not marked as explicit in vendor/modules.txt
	github.com/google/gofuzz@v1.2.0: is explicitly required in go.mod, but not marked as explicit in vendor/modules.txt
	github.com/json-iterator/go@v0.0.0-20180128142709-bca911dae073: is explicitly required in go.mod, but not marked as explicit in vendor/modules.txt
	github.com/mattn/go-isatty@v0.0.4: is explicitly required in go.mod, but not marked as explicit in vendor/modules.txt
	github.com/stretchr/testify@v1.7.0: is explicitly required in go.mod, but not marked as explicit in vendor/modules.txt
	github.com/ugorji/go@v0.0.0-20180131123904-105e48374b52: is explicitly required in go.mod, but not marked as explicit in vendor/modules.txt
	golang.org/x/net@v0.0.0-20210410081132-afb366fc7cd1: is explicitly required in go.mod, but not marked as explicit in vendor/modules.txt
	golang.org/x/sync@v0.0.0-20210220032951-036812b2e83c: is explicitly required in go.mod, but not marked as explicit in vendor/modules.txt
	gopkg.in/check.v1@v1.0.0-20201130134442-10cb98267c6c: is explicitly required in go.mod, but not marked as explicit in vendor/modules.txt
	gopkg.in/go-playground/assert.v1@v1.2.1: is explicitly required in go.mod, but not marked as explicit in vendor/modules.txt
	gopkg.in/go-playground/validator.v8@v8.18.2: is explicitly required in go.mod, but not marked as explicit in vendor/modules.txt
	gopkg.in/yaml.v2@v2.0.0: is explicitly required in go.mod, but not marked as explicit in vendor/modules.txt

	To ignore the vendor directory, use -mod=readonly or -mod=mod.
	To sync the vendor directory, run:
		go mod vendor

解决办法:

go mod vendor

继续执行不再报错

# GOOS=linux GOARCH=386 go build -v -o /go/src/app/app-server

本地测试:
Linux 中安装 go
yum install go -y 就行,和yum install golang -y 是一个意思
安装完后会在/root/下生成一个go 目录
创建工程文件时需要在/root/go/src/ 创建一个空目录,里面必须有 以.go 结尾的文件
govendor安装完成后在/root/go/bin/govendor
docker 中进行测试:

docker run --rm  -it  golang:latest sh
[root@VM-4-11-centos docker-multi-stage-demo]# docker run --rm  -it  golang:latest sh
# mkdir /go/src/app
# cd /go/src/app
# ls
# cp
cp: missing file operand
Try 'cp --help' for more information.
# ls
# ls
main.go  vendor
# go get -u -v github.com/kardianos/govendor
go: downloading github.com/kardianos/govendor v1.0.9
go: downloading github.com/Bowery/prompt v0.0.0-20190916142128-fa8279994f75
...省略
github.com/google/shlex
github.com/kardianos/govendor/run
github.com/kardianos/govendor
# govendor sync
# GOOS=linux GOARCH=386 go build -v -o /go/src/app/app-server
go: cannot find main module, but found vendor/vendor.json in /go/src/app
	to create a module there, run:
	go mod init
# ls
main.go  vendor
# go mod init
go: creating new go.mod: module github.com/cnych/docker-multi-stage-demo

go: copying requirements from vendor/vendor.json
go: to add module requirements and sums:
	go mod tidy
# # go mod tidy
go: downloading github.com/gin-gonic/gin v1.1.5-0.20180126034611-783c7ee9c14e
go: downloading github.com/gin-contrib/sse v0.0.0-20170109093832-22d885f9ecc7
...省略
go: downloading github.com/pmezard/go-difflib v1.0.0
go: downloading gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c
go: downloading github.com/kr/pretty v0.2.1
go: downloading github.com/kr/text v0.1.0
# ls      
go.mod	go.sum	main.go  vendor
# GOOS=linux GOARCH=386 go build -v -o /go/src/app/app-server
go: inconsistent vendoring in /go/src/app:
	github.com/gin-contrib/sse@v0.0.0-20170109093832-22d885f9ecc7: is explicitly required in go.mod, but not marked as explicit in vendor/modules.txt
	github.com/gin-gonic/gin@v1.1.5-0.20180126034611-783c7ee9c14e: is explicitly required in go.mod, but not marked as explicit in vendor/modules.txt
	github.com/golang/protobuf@v1.0.1-0.20180202184318-bbd03ef6da3a: is explicitly required in go.mod, but not marked as explicit in vendor/modules.txt
	github.com/google/gofuzz@v1.2.0: is explicitly required in go.mod, but not marked as explicit in vendor/modules.txt
	github.com/json-iterator/go@v0.0.0-20180128142709-bca911dae073: is explicitly required in go.mod, but not marked as explicit in vendor/modules.txt
	github.com/mattn/go-isatty@v0.0.4: is explicitly required in go.mod, but not marked as explicit in vendor/modules.txt
	github.com/stretchr/testify@v1.7.0: is explicitly required in go.mod, but not marked as explicit in vendor/modules.txt
	github.com/ugorji/go@v0.0.0-20180131123904-105e48374b52: is explicitly required in go.mod, but not marked as explicit in vendor/modules.txt
	golang.org/x/net@v0.0.0-20210410081132-afb366fc7cd1: is explicitly required in go.mod, but not marked as explicit in vendor/modules.txt
	golang.org/x/sync@v0.0.0-20210220032951-036812b2e83c: is explicitly required in go.mod, but not marked as explicit in vendor/modules.txt
	gopkg.in/check.v1@v1.0.0-20201130134442-10cb98267c6c: is explicitly required in go.mod, but not marked as explicit in vendor/modules.txt
	gopkg.in/go-playground/assert.v1@v1.2.1: is explicitly required in go.mod, but not marked as explicit in vendor/modules.txt
	gopkg.in/go-playground/validator.v8@v8.18.2: is explicitly required in go.mod, but not marked as explicit in vendor/modules.txt
	gopkg.in/yaml.v2@v2.0.0: is explicitly required in go.mod, but not marked as explicit in vendor/modules.txt

	To ignore the vendor directory, use -mod=readonly or -mod=mod.
	To sync the vendor directory, run:
		go mod vendor
# go mod vendor
# GOOS=linux GOARCH=386 go build -v -o /go/src/app/app-server
internal/unsafeheader
internal/cpu
internal/bytealg
runtime/internal/atomic
runtime/internal/sys
...省略
github.com/gin-gonic/gin/render
github.com/mattn/go-isatty
net/http/httputil
github.com/gin-gonic/gin
github.com/cnych/docker-multi-stage-demo
# ls
app-server  go.mod  go.sum  main.go  vendor
# cd ..
# ls
bin  pkg  src
# cd ..
# cd pkg    
# ls
mod  sumdb
# go version
go version go1.16.3 linux/amd64
# exit 

原因分析:
在本地go环境利用go版本 version go1.15.5 linux/amd64 执行编译并未报错,后来发现是dockerfile 中默认拉取golang 最新镜像,版本go version go1.16.3 linux/amd64 ,是golang版本变化导致编译报错

 类似资料: