Golang go语言 在Windows上如何打包成linux上可以执行的文件
在Windows上的cmd命令中执行:
运行环境为arm机器系统为linux:
E:ideaprojectgoprojectsrchell>set GOARCH=arm64 E:ideaprojectgoprojectsrchell>set GOOS=linux E:ideaprojectgoprojectsrchell>go build agent.go
运行在环境为其他机器系统为linux:
E:ideaprojectgoprojectsrchell>set GOARCH=amd64 E:ideaprojectgoprojectsrchell>set GOOS=linux E:ideaprojectgoprojectsrchell>go build agent.go
如果执行报如下错:
E:ideaprojectgoprojectsrchell>go build agentServer.go agentServer.go:5:2: no required module provides package github.com/fasthttp/websocket: go.mod file not found in current directory or any parent directory; see 'go help modules' agentServer.go:6:2: no required module provides package github.com/valyala/fasthttp: go.mod file not found in current directory or any parent directory; see 'go help modules' agentServer.go:7:2: no required module provides package github.com/yeqown/fasthttp-reverse-proxy: go.mod file not found in current directory or any parent directory; see 'go help modules'
则执行命令:
E:ideaprojectgoprojectsrchell>go mod init
再次执行报如下错:
E:ideaprojectgoprojectsrchell>go build agent.go
agent.go:5:2: no required module provides package github.com/fasthttp/websocket; to add it:
go get github.com/fasthttp/websocket
agent.go:6:2: no required module provides package github.com/valyala/fasthttp; to add it:
go get github.com/valyala/fasthttp
agent.go:7:2: no required module provides package github.com/yeqown/fasthttp-reverse-proxy; to add it:
go get github.com/yeqown/fasthttp-reverse-proxy
则执行如下命令:
E:ideaprojectgoprojectsrchell>go get github.com/fasthttp/websocket go get: module github.com/fasthttp/websocket: Get "https://proxy.golang.org/github.com/fasthttp/websocket/@v/list": dial tcp 172.217.27.145:443: connectex: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond.
如果报以上错则执行以下命令:
E:ideaprojectgoprojectsrchell>go env -w GOPROXY=https://goproxy.cn,direct
再次执行下载包命令:
E:ideaprojectgoprojectsrchell>go get github.com/fasthttp/websocket go: downloading github.com/fasthttp/websocket v1.4.3 go: downloading github.com/savsgio/gotils v0.0.0-20200608150037-a5f6f5aef16c go: downloading github.com/valyala/fasthttp v1.14.0 go: downloading github.com/klauspost/compress v1.10.4 go: downloading github.com/andybalholm/brotli v1.0.0 go: downloading github.com/valyala/bytebufferpool v1.0.0 go get: added github.com/fasthttp/websocket v1.4.3 E:ideaprojectgoprojectsrchell>go get github.com/yeqown/fasthttp-reverse-proxy go: downloading github.com/yeqown/fasthttp-reverse-proxy v0.0.0-20200930023507-ed73ac32bc64 go: downloading github.com/yeqown/log v1.0.3 go: downloading github.com/pkg/errors v0.9.1 go get: added github.com/yeqown/fasthttp-reverse-proxy v0.0.0-20200930023507-ed73ac32bc64
出现以上提示则包下载成功,重新生成linux上可执行文件:
E:ideaprojectgoprojectsrchell>set GOARCH=amd64 E:ideaprojectgoprojectsrchell>set GOOS=linux E:ideaprojectgoprojectsrchell>go build agent.go
没有报错则生成成功。
然后拷贝新生成的文件到linux上,给授权:
greatwall@greatwall-os:~/soft/remoteAgent$ chmod 777 agent
启动:
greatwall@greatwall-os:~/soft/remoteAgent$ nohup ./agent & [1] 3524 greatwall@greatwall-os:~/soft/remoteAgent$ nohup: 忽略输入并把输出追加到'nohup.out'
查看启动是否成功:
greatwall@greatwall-os:~/soft/remoteAgent$ netstat -an | grep 8999 tcp 0 0 0.0.0.0:8999 0.0.0.0:* LISTEN
出现以上结果则恭喜你启动成功!
以上8999端口为程序配置的端口,所以查看配置端口就可以检查是否启动成功。



