How to cross-compile golang
How to carry out golang cross-compilation, I believe that many inexperienced people do not know what to do, so this paper summarizes the causes of the problem and solutions, through this article I hope you can solve this problem.
1. Comparison table of environmental variables
Golang cross-compilation environment variables comparison table
GOOS | GOARCH | OS VERSION-- |-- linux | 386 / amd64 / arm | > = Linux 2.6darwin | 386 / amd64 | OX X (Snow Leopard + Lion) freebsd | 386 / amd64 | > = FreeBSD 7 windows | 386 / amd64 | > = Windows 20002, using 2.1Windows
Compiling Mac 64-bit executable programs under Windows
Set CGO_ENABLED=0set GOOS=darwinset GOARCH=amd64go build main.go
Compiling Linux 64-bit executable programs under Windows
Set CGO_ENABLED=0set GOOS=linuxset GOARCH=amd64go build main.go2.2 linux
Compiling Mac 64-bit executable programs under Linux
Export CGO_ENABLED=0export GOOS=darwinexport GOARCH=amd64go build main.go
Compiling Windows 64-bit executable programs under Linux
Export CGO_ENABLED=0export GOOS=windowsexport GOARCH=amd64go build main.go2.3 Mac OS
Compiling Windows 64-bit executable programs under Mac
Export CGO_ENABLED=0export GOOS=windowsexport GOARCH=amd64go build main.go
Compiling Linux 64-bit executable programs under Mac
Export CGO_ENABLED=0export GOOS=linuxexport GOARCH=amd64go build main.go3, Note 3.1 CGO_ENABLED explanation
By default, Go's runtime environment variable CGO_ENABLED=1, which starts cgo by default, allows you to call C code in Go code
If the standard library is compiled in CGO_ENABLED=1, the final binaries may be dynamically linked, so it is recommended that you set CGO_ENABLED=0 to avoid unnecessary problems in the migration process.
After reading the above, have you mastered how to cross-compile golang? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!