Skip to content

Instantly share code, notes, and snippets.

@maxfunke
Last active November 11, 2018 09:37
Show Gist options
  • Save maxfunke/77c3b0827df589ed5dffa9a51acb5856 to your computer and use it in GitHub Desktop.
Save maxfunke/77c3b0827df589ed5dffa9a51acb5856 to your computer and use it in GitHub Desktop.
Golang build target OS dependent tools
  1. Naming file with build restriction foo_${GOOS}.go

  2. Adding build directives to the top of the file //+build linux,386 darwin windows

  3. on build system set $GOOS variable to target system (no matter which OS is your build system linux<>windows) GOOS = windows go build main.go

  4. cpu architecture can be set GOARCH=386 go build main.go

  5. check file type file main.exe (linux/wsl)

Credits: (Caskey L. Dickson)[https://youtu.be/COCUqAwAbD0]

cmd := GetCommand()
out, err := exec.Command(cmd[0], cmd[1:]...).Output()
// +build linux darwin netbsd openbsd freebsd
// compiler recognizes "magic comment" above and includes if target OS is listed there
func GetCommand() []string {
return []string{"ifconfig", "-a"}
}
// compiler recognizes _windows from file name and includes if target OS is windows
func GetCommand() []string {
return []string{"ipconfig", "/all"}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment