Home > Software design >  Golang: making the entire package OS specific
Golang: making the entire package OS specific

Time:06-28

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"?

CodePudding user response:

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).

  •  Tags:  
  • go
  • Related