Cross-platform
GOOS=linux GOARCH=amd64 go build <packageName>
Conditional compilation
- Conditional compilation c not go as defined by the macro can implement conditional compilation of the code within a file, he only supports file-level
- Method One: Compile tab of the beginning of the file
- Note that the label needs to compile and package declaration (package main) a space, otherwise it will be treated as a comment statement
- All conditions in the row for the relationship or
- Conditions for the relationship between each row and the
// +build dawin freebsd
// +build !linux
-
Cross-platform above is automatically triggered, we can also manually trigger a number of conditional compilation
-
Method Two: file extension
- If your source files contain suffix: $ GOOS.go, then the source file will only be compiled under the platform, $ GOARCH.go as well.
- These two suffixes can be used together, but to pay attention to the order : $ GOOS $ GOARCH.go, in turn, can not be used: $ GOARCH $ GOOS.go
-
References: https: //blog.csdn.net/varding/article/details/12675971