目录
1、创建一个go demo
package main
import "fmt"
func main() {
fmt.Println("当当响,测试golang交叉编译")
}
2、查看自己的go env
C:\work\[1]src\5_GO_code\pro02>go env
set GO111MODULE=on
set GOARCH=amd64
set GOBIN=
set GOCACHE=C:\Users\HP\AppData\Local\go-build
set GOENV=C:\Users\HP\AppData\Roaming\go\env
set GOEXE=.exe
set GOEXPERIMENT=
set GOFLAGS=
set GOHOSTARCH=amd64
set GOHOSTOS=windows
set GOINSECURE=
set GOMODCACHE=C:\work\[1]src\[19]GoPath\pkg\mod
set GONOPROXY=
set GONOSUMDB=
set GOOS=windows
set GOPATH=C:\work\[1]src\[19]GoPath
set GOPRIVATE=
set GOPROXY=http://goproxy.cn
set GOROOT=C:\software\Go
set GOSUMDB=sum.golang.org
set GOTMPDIR=
set GOTOOLDIR=C:\software\Go\pkg\tool\windows_amd64
set GOVCS=
set GOVERSION=go1.18.3
set GCCGO=gccgo
set GOAMD64=v1
set AR=ar
set CC=gcc
set CXX=g++
set CGO_ENABLED=1
set GOMOD=C:\work\[1]src\5_GO_code\pro02\go.mod
set GOWORK=
set CGO_CFLAGS=-g -O2
set CGO_CPPFLAGS=
set CGO_CXXFLAGS=-g -O2
set CGO_FFLAGS=-g -O2
set CGO_LDFLAGS=-g -O2
set PKG_CONFIG=pkg-config
set GOGCCFLAGS=-m64 -mthreads -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=C:\Users\HP\AppData\Local\Temp\go-build1888514496=/tmp/go-build -gno-record-gcc-switches
C:\work\[1]src\5_GO_code\pro02>
3、修改环境变量
$env:GOOS = "linux"
$env:GOARCH = "amd64"
4、使用go build 编译demo.go
go build demo.go
会生成一个demo可执行文件。
5、将demo文件上传到linux环境下
6、修改demo的执行权限
chmod +x ./demo
7、运行程序
./demo
8、运行结果
当当响,测试golang交叉编译