Article Directory
1. Go Bulid command
How to package Go project it?
Basic usage we use go build command, take a look go build command
usage:
$ go build [-o output] [-i] [build flags] [packages]
-o parameter to specify the output directory and file name of the package
such as:
$ go build -o gotest main.go
Files will main.go involved are compiled package, placed in the current directory, the file name gotest.exe (windows platform by default compiled to exe files, modify, described below)
2. The two most common examples of packaging
Here we introduce the two most common ways of packaging
2.1 packaged into exe file can run in windows
Packaged into exe file extension, can be run directly on the windows
First, go to the next main.go file directory, execute the following command
$ go build main.go
Go then generates packaged project files in this directory, and is main.exe windows executable files can be run directly.
2.2 can be packaged into a file that runs linux
Packaged into a binary file that can run on linux platform
First, go to the next main.go file directory, execute the following command
$ set GOARCH=amd64
$ set GOOS=linux
GOOS refers to the target operating system supports the following operating systems
darwin freebsd linux windows android dragonfly netbsd openbsd plan9 solaris
GOARCH refers to the target processor architecture to support what processor architecture
arm arm64 386 amd64 ppc64 ppc64le mips64 mips64le s390x
After setting up the architecture of the target operating system and the target processor, we execute the command to go build main.go files, you can get what we want the target file.
$ go build main.go
go after packaged Go bulid will generate the project files in this directory, and linux platform is executable binary file.
A file and place this file under linux system folder, chmod 773 [filename] give the file permissions,. / Xx command to execute the file, do not need to go any dependencies, you can run directly.