Conditional compilation in Go

给你一囗甜甜゛ 提交于 2019-12-04 23:13:59

问题


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:


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

Are the [Go] authors opposed to preprocessing?




回答2:


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.



来源:https://stackoverflow.com/questions/11143026/conditional-compilation-in-go

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!