当前位置:网站首页>Golang compilation constraint / conditional compilation (/ / +build < tags>)
Golang compilation constraint / conditional compilation (/ / +build < tags>)
2022-07-07 08:27:00 【Code two brother】
Entry to published columns
0 grpc-go、protobuf、multus-cni Technical column The main entrance
1 grpc-go Source code analysis and actual combat List of articles
2 Protobuf Introduction and actual combat Graphic column List of articles
3 multus-cni List of articles (k8s Multi network implementation scheme )
4 grpc、oauth2、openssl、 Two-way authentication 、 One way authentication and other column contents
Usually when compiling a program , There may be some conditions ( Such as different platforms 、 The architecture has different code implementations ),
Let the compiler compile only the code that meets the conditions , Discard code that doesn't meet the criteria , This is conditional compilation .
golang in , Call it compilation constraint , The methods of compiling constraints are 2 Species difference :
- Compile tags (build tag)
- file extension
| 1、 Compile tags (build tag) |
Add comments at the top of the source file , To determine whether the file is involved in compilation
// +build <tags>
explain :
- Separated by spaces AND
- Separated by commas OR
- ! Express NOT
Labels can be specified as the following :
- operating system , In the environment variables GOOS Value , Such as :linux、darwin、windows wait . Can pass go env see GOOS The value of the environment variable .
- The architecture of the operating system , In the environment variables GOARCH Value , Such as :arch64、x86、i386 wait . Can pass go env see GOARCH The value of the environment variable .
- The compiler used ,gc perhaps gccgo.
- Open or not CGO,cgo.
- golang Version number : such as Go Version 1.1 by go1.1,Go Version 1.12 Version is go1.12, And so on .
- Other custom labels , adopt go build -tags The specified value .
for example , The compilation condition is (linux AND 386) OR (darwin AND (NOT cgo))
// +build linux,386 darwin,!cgo
Another file can have multiple compilation constraints , For example, the condition is (linux OR darwin) AND amd64
// +build linux darwin
// +build amd64
You can also exclude a file from compilation , Use ignore label .
// +build ignore
Be careful :// +build The next line of must be empty .
// +build linux
// main package comment
package main
to glance at , The real case 
The following expression will not be recognized as build tag, It will be resolved to package comments :
// +build linux
package main
| 2、 file extension |
In addition to compiling tags , The compiler will also automatically select the compiled file according to the file suffix , The format is as follows :
$filename_$GOOS.go
$filename_$GOARCH.go
$filename_$GOOS_$GOARCH.go
- $filename: Source file name .
- $GOOS: Represents the operating system , Get... From the environment variable .
- $GOARCH: Represents the system architecture , Get... From the environment variable .
If there is in the project tcp.go and tcp_linux_x86.go Two documents , perform :
GOOS=linux GOARCH=x86 go build
Will choose tcp_linux_x86.go Compile , And perform :
GOOS=linux GOARCH=x86 go build
choice tcp.go Compile .
| 3、 utilize ldflags Assign values to variables during compilation |
This section is for additional instructions , It does not belong to the category of conditional compilation .
Sometimes we need to assign values to variables during compilation , You can use ldflags Parameters complete .
ldflags yes go build A parameter of , Use as follows :
go build -ldflags "-w -s -X main.Version=${VERSION} -X github.com/demo/version.BuildNo=${BUILD_NO}"
Parameter description :
-w Delete DWARF Information : The compiled program cannot be used gdb debug .
-s Delete symbol table :panic Of stack trace No file name / Line number information , Equivalent to C/C++ The program is strip.
-X Replace the value of the variable in the package .
add -w -s It can effectively reduce the size of the compiled program , But it is not conducive to debugging and log tracking .

边栏推荐
- 解读创客思维与数学课程的实际运用
- [IELTS speaking] Anna's oral learning records Part3
- Opencv learning notes II - basic image operations
- Domain specific language / DSL in kotlin
- Four items that should be included in the management system of integral mall
- Several ways of lambda used in functions in kotlin (higher-order functions)
- Go语言中,函数是一种类型
- 云原生存储解决方案Rook-Ceph与Rainbond结合的实践
- Openjudge noi 2.1 1752: chicken and rabbit in the same cage
- 【无标题】
猜你喜欢

Improve the delivery efficiency of enterprise products (1) -- one click installation and upgrade of enterprise applications

Coquette data completes the cloud native transformation through rainbow to realize offline continuous delivery to customers

GFS分布式文件系统

Lua 编程学习笔记

Go语言中,函数是一种类型

opencv学习笔记五——梯度计算/边缘检测

漏洞複現-Fastjson 反序列化
![[quick start of Digital IC Verification] 10. Verilog RTL design must know FIFO](/img/56/82f4533b5bded73df222ef65101a72.png)
[quick start of Digital IC Verification] 10. Verilog RTL design must know FIFO

Splunk中single value视图使用将数值替换为文字

轻松上手Fluentd,结合 Rainbond 插件市场,日志收集更快捷
随机推荐
Make LIVELINK's initial pose consistent with that of the mobile capture actor
Pytoch (VI) -- model tuning tricks
Opencv learning note 5 - gradient calculation / edge detection
Splunk子查询模糊匹配csv中字段值为*
解析创新教育体系中的创客教育
One click installation of highly available Nacos clusters in rainbow
Train your dataset with swinunet
Le système mes est un choix nécessaire pour la production de l'entreprise
CTF-WEB shrine模板注入nmap的基本使用
Analysis of maker education in innovative education system
接口作为参数(接口回调)
PVTV2--Pyramid Vision TransformerV2学习笔记
Deit learning notes
Rainbow version 5.6 was released, adding a variety of installation methods and optimizing the topology operation experience
2-3查找树
Tuowei information uses the cloud native landing practice of rainbow
The reified keyword in kotlin is used for generics
Obsidan之数学公式的输入
Caractéristiques de bisenet
DeiT学习笔记