Home  >  Article  >  Backend Development  >  Golang is cross-compiled under various platforms

Golang is cross-compiled under various platforms

藏色散人
藏色散人forward
2021-02-02 15:50:412348browse

The following column will introduce you to Golang cross-compilation under various platforms from the golang tutorial column. I hope it will be helpful to friends in need!

Golang is cross-compiled under various platforms

#Golang supports cross-compilation, which can generate an executable program for another platform on one platform. I have used it recently and it is very easy to use. Here is a note.

Parameter description

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

Compile Linux and Windows 64-bit executable programs under Mac

CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build main.go
CGO_ENABLED=0 GOOS=windows GOARCH=amd64 go build main.go

Compile Mac and Windows 64-bit executable programs under Linux

CGO_ENABLED=0 GOOS=darwin GOARCH=amd64 go build main.go
CGO_ENABLED=0 GOOS=windows GOARCH=amd64 go build main.go

Compile Mac and Linux 64-bit executable programs under Windows

SET CGO_ENABLED=0
SET GOOS=darwin
SET GOARCH=amd64
go build main.go

SET CGO_ENABLED=0
SET GOOS=linux
SET GOARCH=amd64
go build main.go

The above command compiles a 64-bit executable program. Of course you should also use 386 to compile a 32-bit executable program        

More golang related technologies For articles, please visit the go language column!                                                                    

The above is the detailed content of Golang is cross-compiled under various platforms. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:learnku.com. If there is any infringement, please contact admin@php.cn delete