英文:
Golang: making the entire package OS specific
问题
我了解到可以使用go build标签(// +build windows
)或文件名后缀(source_windows.go
)来使go源文件特定于操作系统。但是,是否有一种方法可以使包目录中的所有源文件都特定于操作系统?
猜测:将包命名为mypackage_windows
会使包目录下的所有文件仅适用于Windows操作系统吗?
如果上述猜测不起作用,我是否需要添加额外的间接层,例如mypackage_usecase_windows.go
,其中包含相应的导入语句:import "mymodule/mypackage_windows"
?
英文:
While I learnt that I can use go build tags (// +build windows
) or filename suffix (source_windows.go
) to make a go source file to be OS specific, is there a way to make all source files inside a package directory to be OS specific?
Guess: would a package name mypackage_windows
make all files under the package dir Windows only?
If the above guess doesn't work, do I have to make an extra level of indirection such as mypackage_usecase_windows.go
, which contains the respective import statement: import "mymodule/mypackage_windows"
?
答案1
得分: 2
不,唯一的方法是让该包中的所有文件都遵循文件名模式或在其中具有所需的构建约束。基于文件名模式的构建约束仅在源文件级别上起作用。
拥有和维护 N 个实现相同 mypackage
的操作系统或架构特定代码并没有太多意义。当前的工具提供了一种方式,可以拥有一个 mypackage
,并根据操作系统或架构决定如何实现其功能(在该包内的平台特定源文件中)。
英文:
No, the only way is to make all files inside that package follow file name pattern or have desired build constraints in them. File name pattern based build constraints work only at source file level.
It doesn't make much sense to have and maintain N implementations of the same mypackage
with OS or achitecture specific code. Current tooling gives you a way to have one mypackage
and based on OS or achitecture decide how to implement its features (in platform specific source files inside that package).
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论