当前位置:网站首页>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 .
边栏推荐
- Learn how to compile basic components of rainbow from the source code
- [quick start of Digital IC Verification] 13. SystemVerilog interface and program learning
- 【Go ~ 0到1 】 第七天 获取时间戳,时间比较,时间格式转换,Sleep与定时器
- Improve the delivery efficiency of enterprise products (1) -- one click installation and upgrade of enterprise applications
- 数据中台落地实施之法
- Train your dataset with swinunet
- The truth of robot education in hands-on practice
- 一文了解如何源码编译Rainbond基础组件
- 打通法律服务群众“最后一公里”,方正璞华劳动人事法律自助咨询服务平台频获“点赞”
- Several ways of lambda used in functions in kotlin (higher-order functions)
猜你喜欢
提高企业产品交付效率系列(1)—— 企业应用一键安装和升级
Splunk子查询模糊匹配csv中字段值为*
Opencv learning notes II - basic image operations
Obsidan之数学公式的输入
在Rainbond中实现数据库结构自动化升级
CTF-WEB shrine模板注入nmap的基本使用
在Rainbond中一键部署高可用 EMQX 集群
[quick start of Digital IC Verification] 11. Introduction to Verilog testbench (VTB)
AVL平衡二叉搜索树
Register of assembly language by Wang Shuang
随机推荐
MES系統,是企業生產的必要選擇
漏洞複現-Fastjson 反序列化
在Rainbond中实现数据库结构自动化升级
Caractéristiques de bisenet
Game attack and defense world reverse
GFS分布式文件系统
opencv学习笔记三——图像平滑/去噪处理
Explore creativity in steam art design
Unityhub cracking & unity cracking
2-3查找树
Interpreting the practical application of maker thinking and mathematics curriculum
Infix keyword infix expression and the use of generic extension function in kotlin
雅思考试自己的复习进度以及方法使用【日更版】
Pytoch (VI) -- model tuning tricks
Vulnerability recurrence fastjson deserialization
DeiT学习笔记
Deit learning notes
Bisenet features
PVTV2--Pyramid Vision TransformerV2学习笔记
Zcmu--1396: queue problem (2)