如何在Go中处理特定平台的模块?

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

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

huangapple
  • 本文由 发表于 2013年5月24日 23:33:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/16738629.html
匿名

发表评论

匿名网友

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

确定