get system platform in go

You can use the runtime.GOOS constant to get the operating system name and runtime.GOARCH constant to get the architecture name in Go:

main.go
package main

import (
	"fmt"
	"runtime"
)

func main() {
	fmt.Printf("OS: %s\nArch: %s", runtime.GOOS, runtime.GOARCH)
}
122 chars
11 lines

This will output something like:

main.go
OS: linux
Arch: amd64
22 chars
3 lines

Here are the possible values for runtime.GOOS:

  • darwin (OS X)
  • dragonfly (DragonFly BSD)
  • freebsd
  • linux
  • netbsd
  • openbsd
  • plan9
  • solaris
  • windows

related categories

gistlibby LogSnag