在Go语言中的条件编译

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

Conditional compilation in Go

问题

我正在尝试使用CGo为ENet编写一个Go的包装器。

当我在Mac上尝试编译我的包装器时,库版本较旧,接口略有不同。代码中有99%是相同的,只需要更改一些C调用。

在Go中,处理这种问题的最佳实践是什么?
是否有一种条件编译或条件导入的方法?

英文:

I'm trying to write a Go wrapper using CGo for ENet.

When I tried to compile my wrapper on a Mac the library was older and had a slightly different interface. 99% of the code is the same just a few C calls need to change.

What is the best practice for dealing with a problem like this in Go?
Is there some way to do conditional compilation or conditional imports?

答案1

得分: 18

将平台特定的内容分离到一个单独的文件中,例如stuff.go。

现在用不同平台的版本替换stuff.go,比如stuff_darwin.go(用于Mac),stuff_windows.go,stuff_linux.go等。

如果一个文件有这样的后缀,go命令将只在指定的平台上编译它。

英文:

Separate out the platform-specific stuff into a separate file, e.g. stuff.go

Now replace stuff.go with versions for the different platforms, like stuff_darwin.go (for Mac), stuff_windows.go, stuff_linux.go, etc.

If a file has a suffix like that, the go command will compile it only on the platform specified.

答案2

得分: 2

Go没有条件编译或条件导入。在C代码中处理类型差异。

Go的作者们反对预处理吗?

英文:

Go does not have conditional compilation or conditional imports. Handle the type differences in C code.

Are the [Go] authors opposed to preprocessing?

huangapple
  • 本文由 发表于 2012年6月22日 00:55:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/11143026.html
匿名

发表评论

匿名网友

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

确定