Go language learning - compilation

Golang supports cross-compilation, which generates executable programs for another platform on one platform.

Taking the executable program of compiling linux on windows as an example, online tutorials are generally as follows:

SET CGO_ENABLED=0
SET GOOS=linux
SET GOARCH=amd64

go build main.go  
go build -o main main.go  //指定可执行文件的命名

For some reason, on the author's win11, the set command did not take effect. So I added a command:

go env -w GOOS=linux

command interpretation

  • GOOS: The operating system of the target platform (darwin, freedsd, linux, windows)
  • GOARCH: The architecture of the target platform (386, amd64, arm)
  • CGO: Cross-compilation does not support CGO, so disable it

run

Transfer the generated executable file to the target platform, and it can be run directly. Remember to grant the executable permission to the file.

./main

Guess you like

Origin blog.csdn.net/chenxy02/article/details/125736783