补充 编译标签 和 文件后缀.
Signed-off-by: chen.yang <chen.yang@yuzhen-iot.com>
This commit is contained in:
parent
2e41712714
commit
2a0a3856fe
@ -1,5 +1,7 @@
|
||||
# Go 交叉编译
|
||||
|
||||
## 交叉编译宏
|
||||
|
||||
Golang 的交叉编译是通过设置编译宏变量来实现的,主要是 GOOS 和 GOARCH,列表如下:
|
||||
|
||||
| GOOS - Target Operating System | GOARCH - Target Platform |
|
||||
@ -55,3 +57,39 @@ CGO_ENABLED=1 GOOS=linux GOARCH=arm CC=arm-linux-gnueabi-gcc go build
|
||||
# Build and Stripped
|
||||
GOOS=linux GOARCH=arm go build -ldflags "-s"
|
||||
```
|
||||
|
||||
## 编译标签
|
||||
|
||||
有时针对不同的平台、架构,在代码中需要进行额外的处理,因此需要在代码中区分平台和架构。
|
||||
|
||||
Go语言通过关键字+build在编译时对平台和就够进行区分。
|
||||
|
||||
例如:在 Linux 平台,386 架构或者 darwin 平台,非 CGo 时编译
|
||||
|
||||
```go
|
||||
// +build linux,386 darwin,!cgo
|
||||
|
||||
package mypackage
|
||||
```
|
||||
|
||||
还可以采用并列的写法。例如:
|
||||
|
||||
```go
|
||||
// +build linux darwin
|
||||
// +build amd64
|
||||
|
||||
package mypackage
|
||||
```
|
||||
|
||||
***注意,在标签和 package 声明之间必须有一个空行。***
|
||||
|
||||
## 文件后缀
|
||||
|
||||
也可以通过向文件添加后缀来区分不同平台的代码:
|
||||
|
||||
```bash
|
||||
myfile_freebsd_arm.go
|
||||
<file name>_<os>_<arch>.go
|
||||
```
|
||||
|
||||
***注意,源文件不能只提供编译后缀。***
|
||||
|
Loading…
x
Reference in New Issue
Block a user