英文:
How can I handle platform-specific modules in Go?
问题
我正在使用Go编写一个命令行实用程序,其中(作为其操作的一部分)需要从用户那里获取密码。Unix系统有一个很棒的gopass
模块可以实现这一点,而且我知道如何为Windows控制台编写一个类似的模块。问题是,Windows模块显然无法在nix上构建,而nix版本无法在Windows上构建。由于Go缺乏任何预处理器支持(据我所知),我完全不知道正确的方法是什么。我知道这是可能的,因为Go自身必须为其自己的库执行此操作,但我习惯的工具(条件导入/预处理器等)似乎缺失了。
英文:
I'm writing a command-line utility in Go that (as part of its operation) needs to get a password from the user. There's a great gopass
module for Unix that does this, and I know how to write one for the Windows console. The problem is that the Windows module obviously won't build on *nix, and the *nix version won't build on Windows. Since Go lacks any preprocessor support (as far as I can tell), I have absolutely no idea what the right way to approach this is. I know it's possible, since Go itself must do this for its own libraries, but the tooling I'm used to (conditional imports/preprocessors/etc.) seems to be missing.
答案1
得分: 8
Go有构建约束,可以在.go文件中作为注释或作为文件名的一部分指定。
一组约束用于目标操作系统,因此您可以为Windows拥有一个文件,为Linux等拥有另一个文件,并在这两个文件中以两种不同的方式实现相同的函数。
有关构建约束的更多信息,请参阅http://golang.org/pkg/go/build/#hdr-Build_Constraints
英文:
Go has build constraints, which can either be specified as comments in a .go file, or as part of the file name.
One set of constraints is for target operating system, so you can have one file for Windows, one for e.g. Linux and implement the same function in two different ways in the two.
More information on build constraints are at http://golang.org/pkg/go/build/#hdr-Build_Constraints
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论