Go Code Organization

Satish Manohar Talim
2 min readSep 3, 2015

--

The go tool is the standard way to fetch, build, and install Go packages and commands. The “go” tool requires you to organize your code in a specific way.

Workspaces

Go code must be kept inside a workspace. A workspace is a directory hierarchy with three directories at its root:

  • “src” contains Go source files organized into packages (one package per directory),
  • “pkg” contains package objects, and
  • “bin” contains executable commands.

The “go” tool builds source packages and installs the resulting binaries to the “pkg” and “bin” directories.

The “src” subdirectory typically contains version control repositories (such as for Git) that track the development of one or more source packages.

The GOPATH environment variable

The “GOPATH” environment variable specifies the location of your workspace.

To get started, create a workspace directory and set “GOPATH” accordingly. Your workspace can be located wherever you like. Note that this must not be the same path as your Go installation.

On my Windows computer, I have set “GOPATH=C:\go_projects\go”. Next I update my system environment variable “PATH” to include my workspace “bin” subdirectory i.e. “PATH=%PATH%;%GOPATH%\bin;”

We need to do the same on *nix or Mac as well.

mkdir $HOME/go_projects/go
export GOPATH=$HOME/go_projects/go
export PATH=$PATH:$GOPATH/bin
My workspace folder structure

Package paths

The packages from the standard library are given short paths such as “fmt” and “net/http”. For your own packages, you must choose a base path that is unlikely to collide with future additions to the standard library or other external libraries. If you have a GitHub account (sign up, if you don’t have one) at “github.com/user”, that should be your base path. We will use “github.com/user” as our base path. Create a directory inside your workspace in which to keep the source code. I have created the folder “C:\go_projects\go\src\github.com\SatishTalim”.

Note: Replace “SatishTalim” with your own username.

Editing a Go program

Go programs are written as plain text Unicode using the UTF-8 encoding. All of Go’s keywords and operators use ASCII characters; however, Go identifiers can start with any Unicode letter followed by any Unicode letters or digits, so Go programmers can freely use their native language.

A Go program

To compile and run a simple program, first choose a package path and create a corresponding package directory inside your workspace:

$ mkdir $GOPATH/src/github.com/SatishTalim/hello

Next copy, the already created file “hello.go” into this folder.

Now you can build and install that program with the “go” tool:

$ cd $GOPATH/src/github.com/SatishTalim/hello
$ go install

The “go” tool will only print output when an error occurs, so if these commands produce no output they have executed successfully.

The above command i.e. go install compiles, builds the “hello” executable binary and then installs that binary to the workspace’s “bin” directory as “hello” (or, under Windows as “hello.exe”).

You can now run the program by typing:

$ hello
Hello, world.

--

--

Satish Manohar Talim

Senior Technical Evangelist at GoProducts Engineering India LLP, Co-Organizer GopherConIndia. Director JoshSoftware and Maybole Technologies. #golang hobbyist