在 Go 中封装平台特定代码的正确方法是什么? [英] What is the right approach to encapsulate platform specific code in Go?

查看:25
本文介绍了在 Go 中封装平台特定代码的正确方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想开发一个小型 Go 应用程序,向演示文稿的观众展示所使用的按键快捷键.

I want to develop a small Go application, which shows the used key stroke shortcuts to the audience of a presentation.

要挂钩键盘事件,我将不得不使用一些特定于平台的代码.封装平台特定代码的 Go 方法是什么?我一直在搜索编译器开关或平台模块等关键字,但我真的找不到.

To hook into the keyboard events I will have to use some platform specific code. What is the Go way to encapsulate platform specific code? I've been searching for keywords like compiler switch or platform modules, but I couldn't really find something about.

推荐答案

平台特定代码的解决方案是 构建约束.

The solution to platform specific code is the build constraints.

注意:在 Go 1.17 之前,语法是以 //+build 开头的注释行,但 Go 1.17 引入了 //go:build pragma 现在是首选方式.

Note: Prior to Go 1.17 the syntax was a comment line starting with // +build, but Go 1.17 introduced the //go:build pragma which is now the preferred way.

构建约束,也称为构建标记,是开始的行注释

A build constraint, also known as a build tag, is a line comment that begins

//go:build

列出文件应包含在包中的条件.约束可以出现在任何类型的源文件中(不仅仅是 Go),但它们必须出现在文件顶部附近,前面只能有空行和其他行注释.这些规则意味着在 Go 文件中,构建约束必须出现在 package 子句之前.

that lists the conditions under which a file should be included in the package. Constraints may appear in any kind of source file (not just Go), but they must appear near the top of the file, preceded only by blank lines and other line comments. These rules mean that in Go files a build constraint must appear before the package clause.

所以基本上每个平台特定的 Go 代码应该放在不同的文件中,你可以用它们的目标来标记每个 Go 文件.

So basically each platform specific Go code should go into different files, and you can mark each of these Go files with the target they are intended for.

例如,如果文件包含特定于 Linux 的代码(例如系统调用),请以以下内容开头:

For example if a file contains Linux specific code (e.g. syscalls), start it with:

//go:build linux

如果文件包含特定于 Windows 的系统调用,请使用以下命令启动它:

If a file contains Windows specific syscalls, start it with:

//go:build windows

更多选项"可用,请阅读链接的文档.例如,您可以指定对操作系统、架构、Go 版本、正在使用的编译器的约束.您还可以指定将使用逻辑 OR 或 AND 解释的多个约束,或者您可以使用否定(例如,此代码适用于除 linux 之外的每个目标平台).

A lot more "options" are available, read the linked docs. For example you can specify constraints to the OS, Architecture, Go version, the compiler being used. You can also specify multiple constraints that will be interpreted with logical OR or AND, or you can use negation (e.g. this code is for every target platform except linux).

您甚至可以使用以下约束标记要忽略的 .go 文件:

You can even mark a .go file to be ignored with the following constraint:

//go:build ignore

请注意,构建约束是特定于编译器的.如果特定编译器无法识别构建约束,则编译器将忽略该文件.例如,Go AppEngine SDK 带有一个内置的、经过修改的 Go 编译器,它额外识别

Note that build constraints are compiler specific. If a specific compiler does not recognize a build constraint, the compiler will ignore the file. As an example the Go AppEngine SDK comes with a built-in, modified Go compiler which additionally recognizes the

//go:build appengine

约束,这意味着源文件仅适用于 Google App Engine 平台.常规"Go 编译器将忽略该文件,如果有人尝试在没有 Go AppEngine SDK 的情况下构建代码,您就可能不会出现一堆编译器错误.

constraint, which means the source file is intended for the Google App Engine Platform only. "Regular" Go compilers will ignore the file, giving you the possibility to not have a bunch of compiler errors if someone tries to build the code without the Go AppEngine SDK.

这篇关于在 Go 中封装平台特定代码的正确方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆