0

I always see the code,

//go:build linux
// +build linux

what mean? I don't understand.

just give me some examples

wuqinqiang
  • 77
  • 4
  • Imagine you need to compile a linux-specific code, using some linux-specific api. – Tiago Peczenyj Mar 26 '22 at 14:43
  • More than this: you can define build tags to do some alternative builds like… run integration tests, use some mock / debug / testing implementations, etc. it is the “equivalent” to #ifdef in C preprocessor – Tiago Peczenyj Mar 26 '22 at 15:29

1 Answers1

3

These are build constraints, see https://pkg.go.dev/go/build#hdr-Build_Constraints

In older versions of Go, you would say

// +build linux

where the new syntax from Go 1.17 and up is

//go:build linux

but they do the same thing: only include this file in the Linux build.

gonutz
  • 5,087
  • 3
  • 22
  • 40