How to Write Go Code

Workspaces

A workspace is a directory hierarchy with three directories at its root:

  • src contains Go source files
  • pkg contains package objects
  • bin contains executable commands

The GOPATH environment variable

1
2
3
localhost:dll dll$ cat ~/.bash_profile
export PATH=${PATH}:~/Library/Android/sdk/platform-tools:/usr/local/go/bin
export GOPATH=$HOME/gowork

build、install and run that program with the go tool

1
2
3
4
5
6
// build
go build dll/stringutil
// install
go install dll/hello
// run
$GOPATH/bin/hello