英文:
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 does not have conditional compilation or conditional imports. Handle the type differences in C code.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论