From: Andrew Gerrand
First, a disclaimer: very few Go packages should need to know about the +hardware and operating system they run on. In the vast majority of cases the +language and standard library handle most portability issues. This section is +a guide for experienced systems programmers who have a good reason to write +platform-specific code, such as assembly-language support for fast +trigonometric functions or code that implements a common interface above +different operating systems.
+ +To compile such code, use the $GOOS
and $GOARCH
+environment variables in your
+source file names and Makefile
.
For example, this Makefile
describes a package that builds on
+different operating systems by parameterizing the file name with
+$GOOS
.
+include $(GOROOT)/src/Make.inc + +TARG=mypackage +GOFILES=\ + my.go\ + my_$(GOOS).go\ + +include $(GOROOT)/src/Make.pkg ++ +
The OS-specific code goes in my_linux.go
,
+my_darwin.go
, and so on.
If you follow these conventional parameterizations, tools such as +goinstall will work seamlessly with your package: +
+ ++my_$(GOOS).go +my_$(GOARCH).go +my_$(GOOS)_$(GOARCH).go ++ +
The same holds for .s
(assembly) and .cgo
files.