1 现象
在mac上编写的go 代码,远程发布到linux 上执行,会报如下错误:
GOROOT=/opt/homebrew/Cellar/go/1.19.5/libexec #gosetup
GOPATH= #gosetup
/opt/homebrew/Cellar/go/1.19.5/libexec/bin/go build -o /private/var/folders/rg/7cl0y8717_j0hks264rdl47h0000gn/T/GoLand/___go_run_drz_go_linux /Users/david/Documents/DaveDocs/WorkSpace/Go/drz.go #gosetup
/data/dave/Go/executables-GJ4q2mbJKi/___go_run_drz_go_linux
/data/dave/Go/executables-GJ4q2mbJKi/___go_run_drz_go_linux: /data/dave/Go/executables-GJ4q2mbJKi/___go_run_drz_go_linux: cannot execute binary file
Process finished with the exit code 126
2 原因分析
Go是一门编译型语言,所以在不同平台上,需要编译生成不同格式的二进制包。编译时候只需要指定两个参数:GOOS和GOARCH即可。
编译到 linux 64bit
$ GOOS=linux GOARCH=amd64 go build
或者可以使用 -o 选项指定生成二进制文件名字
$ GOOS=linux GOARCH=amd64 go build -o app.linux
编译到 linux 32bit
$ GOOS=linux GOARCH=386 go build
编译到 windows 64bit
$ GOOS=windows GOARCH=amd64 go build
编译到 windows 32bit
$ GOOS=windows GOARCH=386 go build
编译到 Mac OS X 64bit
$ GOOS=darwin GOARCH=amd64 go build
在不同的平台编写快捷生成脚本的时候也是不一样的
linux sh
GOARCH=amd64 GOOS=linux go build -o myweb main.go
windows bat
set GOARCH=amd64
set GOOS=linux
go build -o myweb main.go
3 解决方法
我们这里只在GoLand上写的,直接修改配置,勾选Build on remote target即可。
版权声明:本文为博主原创文章,未经博主允许不得转载。