go build 报错

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版本变化导致编译报错

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值