当前位置:网站首页>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
 Insert picture description here

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 .

 Insert picture description here

原网站

版权声明
本文为[Code two brother]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/188/202207070525336115.html