This is a creation in Article, where the information may have evolved or changed.
Golang Cross-compiling
Project Address: Https://github.com/EDDYCJY/go ... (Get in the car, support a wave)
Original address: Https://segmentfault.com/a/11 ...
Objective
When we build the scratch image in serial nine, we compile the executable with another form of command, do not know if you have any questions?
$ CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o go-gin-example .
Description
We will explain the role of the command parameters, I hope you read, each item in tandem, you will find that this is a cross-compilation of the relevant knowledge
Golangone of the most exciting features. Cross-platform compilation
First, cgo_enabled
Role:
Used to identify (claim) cgo whether the tool is available
Significance:
When there is a cross-compilation situation, cgo the tool is not available. In the context of the standard GO command, cross-compiling means that the identity of the target computing schema of the program-building environment differs from the target computing schema of the program's environment, or the identity of the target operating system of the program-building environment differs from the target operating system's identity for the program's running environment
Summary:
In combination with the case, we are the executable file that is compiled in the host, and the executable that runs in the Scratch mirror; Obviously, the computer architecture and the running environment of the two are not sure whether it is consistent (after all docker , the image built can be used by others), then we need to cross-compile Cross-compilation is not supported cgo , so disable it here
When closed cgo , all dependent libraries are ignored and statically linked during the build process, cgo and when turned on cgo , the mode is changed to dynamic link
Add:
golangIs the default turn cgo on tool, can execute go env command view
$ go envGOARCH="amd64"GOBIN=""GOCACHE="/root/.cache/go-build"GOEXE=""GOHOSTARCH="amd64"GOHOSTOS="linux"GOOS="linux"...GCCGO="gccgo"CC="gcc"CXX="g++"CGO_ENABLED="1"...
Second, GOOS
Target operating system used to identify (declare) the program build environment
Such as:
Third, Goarch
Target computing architecture for identifying (declaring) program-building environments
If not set, the default value is consistent with the target computing schema of the program's operating environment (the case is the default value used)
Such as:
| system |
GOOS |
Goarch |
| Windows 32-bit |
Windows |
386 |
| Windows 64-bit |
Windows |
Amd64 |
| OS X 32-bit |
Darwin |
386 |
| OS X 64-bit |
Darwin |
Amd64 |
| Linux 32-bit |
Linux |
386 |
| Linux 64-bit |
Linux |
Amd64 |
Iv. Gohostos
Target operating system used to identify (declare) the running environment of the program
Wu, Gohostarch
Target computing schema for identifying (declaring) The program's operating environment
VI. Go Build
-A
Forced recompilation, simply put, is not using cached or compiled portions of the file, directly all packages are up-to-date Code recompile and association
-installsuffix
Role:
add suffix identifiers to the directory where the package is installed to keep the output separate from the default version
Add:
If identity is used -race , the suffix is set to identity by default -race and is used for differences race and normal versions
-O
Specify the compiled executable file name
Summary
Most of the parameter directives have some relevance and are related to cross-compiling knowledge points, so you can have a good taste.
Finally, we can look at the go build help deeper understanding
$ go Help Buildusage:go build [-O output] [-i] [build flags] [packages] ...-a force rebuilding of packages that is already up-to-date. -N Print the commands but does not run them. -P n The number of programs, such as build commands or test binaries, that can is run in parallel. The default is the number of CPUs available. -race enable data race detection. Supported only on LINUX/AMD64, Freebsd/amd64, Darwin/amd64 and Windows/amd64. -msan enable interoperation with memory sanitizer. Supported only in Linux/amd64, and only with CLANG/LLVM as the host C compiler. -V Print the names of packages as they is compiled. -work Print the name of the temporary work directory and does not delete it when exiting. -X Print the commands. -asmflags ' [pattern=]arg list ' arguments to pass on each go tool asm invocation. -buildmode mode build mode to use. See ' Go HelP Buildmode ' for more. -compiler name Name of compiler to use, as in Runtime.compiler (GCCGO or GC). -gccgoflags ' [pattern=]arg list ' arguments to pass on each gccgo compiler/linker invocation. -gcflags ' [pattern=]arg list ' arguments to pass on each go tool compile invocation. -installsuffix suffix a suffix to use in the name of the package installation directory, in order to keep OU Tput separate from default builds. If using The-race flag, the install suffix is automatically set to race or, if set explicitly, have _race appended to it. Likewise for The-msan flag. Using a-buildmode option that requires non-default compile the flags has a similar effect. -ldflags ' [pattern=]arg list ' arguments to pass on each Go tool link invocation. -linkshared link against shared libraries previously created with-buildmode=shared. -pkgdir dir Install and load all packages from Dir instead ofThe usual locations. For example, when building with a non-standard configuration, use-pkgdir to keep generated packages in a separate Location. -tags ' tag list ' A space-separated list of build tags to consider satisfied during the build. For more information on build tags, see the description of build constraints in the documentation for the Go/bui LD package. -toolexec ' cmd args ' a program-to-use-invoke toolchain programs like vet and ASM. For example, instead of running ASM, the GO command would run ' cmd args/path/to/asm <arguments for asm> ' ....
Reference
Sample code for this series
Books
- Go concurrent Programming Combat Second Edition