The go tool is a CLI for managing Go source.

Basics

Run, build and install

Source is placed within $GOPATH like $GOPATH/src/github.com/bm4cs/cool-app/main.go

To just run a program, without building a binary output:

$ go run main.go
hello world

To build a statically linked binary output:

$ go build
$ file demo
demo: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), statically linked

Cross compiling is available too:

$ GOOS=windows go build
$ file demo.exe
demo.exe: PE32+ executable (console) x86-64 (stripped to external PDB), for MS Windows

Optionally the binary can be written to $GOPATH/bin, which is normally on the PATH:

$ go install
$ ls $GOPATH/bin | grep demo
demo

Dependencies

$ go get github.com/golang/example/hello
$ $GOPATH/bin/hello
Hello, Go examples!

Formatting

Format a file to stdout:

$ gofmt main.go

Diff:

$ gofmt -d main.go

Update (write) the original source file:

$ gofmt -w main.go

Documentation

Local CLI help:

$ go doc fmt Printf

func Printf(format string, a ...interface{}) (n int, err error)
Printf formats according to a format specifier and writes to standard
output. It returns the number of bytes written and any write error
encountered.

Local documentation HTTP server:

$ godoc -http :7070

Structuring a source tree

project-layout curates common conventions followed by the community.

Check out go-structure-examples for flat, layered, modular and DDD based layouts.

Libraries

Middleware

  • negroni idiomatic HTTP middleware
  • alice painless middleware chaining

Web

  • gin HTTP web framework
  • go-jose implements the Javascript Object Signing and Encryption set of standards JWE, JWS and JWT (JSON Web Encryption, JSON Web Signature and JSON Web Token)
  • gorilla a general purpose web toolkit that solves doing context, routing, RPC over HTTP, strong typing forms to structs, secure cookies, session and websockets.