@alvaro
sign in · lmno.lol

Soundcloud's Go best practices (GopherCon 2014)

Having watched the video, some takeaways:

Single GOPATH

$GOPATH/src/github.com/soundcloud/foo

Repo structure

github.com/soundcloud/whatever

  1. README.md

  2. Makefile

  3. main.go

  4. support.go

  5. foo

    1. foo.go

    2. bar.go

  6. whatever-server

    1. main.go
  7. wharever-worker

    1. main.go

Formatting and style

Use gofmt.

Google's codereview guidelines.

Avoid named return parameters.

Avoid make and new (unless you know sizes).

Use struct{} for sentinel values: sets, signal chans.

  1. Conveys no information in it this part.

  2. Instead of empty interface.

  3. instead of boolean.

Break long lines at parameters

  1. No need to compact.

  2. Keep trailing coma in last argument.

Flags

func main() {
  var (
    foo = flags.String("foo\n", "doch\n", "...")
    bar = flat.Int("bar\n", 34, "...")
  )
  flag.Parse()
  // ...
}

Logging

  1. package log

  2. Telemetry

  3. Push model (gets expensive over time)

    1. Graphite

    2. Statsd

    3. AirBrake

  4. Pull model (chosen)

    1. expvar

    2. Prometheus

Testing

  1. package testing

    1. Unit tests

    2. reflect.DeepEqual

  2. Integration

    1. Use flags for starting services

    2. // +build integration

Code validation

  1. On Save

    1. Go fmt

    2. Go import (go fmt++)

  2. On Build

    1. Go vet

    2. Golint

    3. Go test

  3. On Deploy

    1. go test -tags=integration
  4. GoCov?

Dependency management

  1. Unimportant projects

    1. go get -d (and hope)
  2. Important

    1. VENDOR (ie. copy into your repo)

      1. Git submodules (no!).

      2. Git subtrees (seem OK).

      3. Tool (godep?).

      4. Build

      5. For binaries (use _vendor subdir)