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

查看:78
本文介绍了在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.

构建约束(也称为构建标签)是开始的行注释

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

// +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特定代码(例如syscalls),则使用以下代码启动它:

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

// +build linux

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

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

// +build windows

还有更多的选项"可用,请阅读链接的文档.例如,您可以指定对OS,体系结构,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:

// +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

// +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天全站免登陆