go buildis a very common command that we can start compiling, compiling our packages and related dependencies into an executable file.
Usage:go build [-O output] [-i] [build flags] [packages]
go buildThe use of a relatively concise, all parameters can be ignored, until only go build , this time means to use the current directory to compile, the following several commands are equivalent:
Go Buildgo build. Go Build Hello.go
The above three kinds of writing, all use the current directory compiled meaning. Because we ignored it packages , we naturally compiled it using the current directory. From here we can also extrapolate that go build essentially a path is needed so that the compiler can find out which go files need to be compiled. packagesis actually a relative path, is relative to what we define GOROOT and GOPATH These two environment variables, so with packages This parameter, go build we can know which need to compile the go file.
Go Build Flysnow.org/tools
This is the way to specify the package, which will explicitly compile our package. Of course we can also use wildcard characters
Go Build flysnow.org/tools/...
3 dots indicate that all strings are matched, so go build all packages under the Tools directory are compiled.
go buildwhen it comes to compiling, it's not a cross-platform compilation, go provides a compile-chain tool that allows us to compile executable files for other platforms on any development platform.
By default, all of our current machine-generated executables, such as your Linux 64-bit, generate executable files under Linux 64-bit, like my Mac, and you can use go env to view the compilation environment, which captures the important parts below.
~Go envgoarch="AMD64"Goexe=""Gohostarch="AMD64"Gohostos="Darwin"GOOS="Darwin"Goroot="/usr/local/go"Gotooldir="/usr/local/go/pkg/tool/darwin_amd64"
Note that there are two important environment variables GOOS and Goarch, where GOOS refers to the target operating system, and its available values are:
Darwin
Freebsd
Linux
Windows
Android
Dragonfly
NetBSD
Openbsd
Plan9
Solaris
There are 10 operating systems supported altogether. Goarch refers to the architecture of the target processor and is currently supported by:
Arm
Arm64
386
Amd64
Ppc64
Ppc64le
Mips64
Mips64le
s390x
With a total of 9 processor architectures, GOOS and Goarch combine to support the generation of a wide variety of executable programs, specific combinations of reference:
Https://golang.org/doc/install/source#environment
If we are going to generate executable programs for different platform architectures, just change these two environment variables, such as to build a Linux 64-bit program, the command is as follows:
Goos=linux goarch=amd64 Go Build Flysnow.org/hello
The first two assignments are changes to the environment variables, so the benefit is only valid for this run and will not change our default configuration.
These usages are almost enough for us to use, and more information about go build the user can be viewed through the following commands:
Go help build