`//go:build xxx` 和 `// +build xxx` 在 Golang 中的含义是什么?

huangapple go评论70阅读模式
英文:

what does //go:build xxx and // +build xxx means in golang?

问题

这段代码是Go语言中的条件编译指令,用于在不同的操作系统上进行编译。在这个例子中,//go:build linux// +build linux表示这段代码只会在Linux操作系统上进行编译。

这样的条件编译指令可以用于在不同的操作系统或平台上编写特定的代码。例如,如果你想编写一段只在Windows上运行的代码,你可以使用//go:build windows// +build windows来指定。

希望这些例子对你有帮助!

英文:

I always see the code,

//go:build linux
// +build linux

what mean? I don't understand.

just give me some examples

答案1

得分: 3

这些是构建约束条件,详见 https://pkg.go.dev/go/build#hdr-Build_Constraints

在较旧版本的Go中,你会这样写:

// +build linux

而从Go 1.17版本开始,新的语法是:

//go:build linux

但它们的作用是相同的:只在Linux构建中包含此文件。

英文:

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.

huangapple
  • 本文由 发表于 2022年3月26日 22:19:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/71628861.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定