Basic Golang setup.

  • Best way to install go in Mac is download official dmg file and install.
  • Check that Go is installed correctly by setting up a workspace and building a simple ‘hello world’ program.
  • Create a workspace directory for go project. set the GOPATH environment variable to point to that location. mkdir go_work and export GOPATH=$HOME/go_work
  • Next, make the directories src/github.com/user/hello inside your workspace
  • Inside the hello directory create a file named hello.go with the following contents:
    package main

    import "fmt"

    func main() {
        fmt.Printf("hello, world\n")
    }
  • Then compile with go install github.com/user/hello. This will create an executable file hello under bin directory of workspace.
  • Next, command to execute hello
   $ $GOPATH/bin/hello
   hello, world