Skip to content

Latest commit

 

History

History
134 lines (86 loc) · 4.37 KB

README.md

File metadata and controls

134 lines (86 loc) · 4.37 KB

Golang at Clever

Onboarding

It is suggested that you introduce yourself to Golang by first completing the tour. Most topics are covered to help you hit the ground running.

Most Go idioms can then be learned by reading Effective Go. This is highly suggested before you begin writing Go code for production.

The Go Blog contains many useful articles going over how to use several essential Go features like go-routines, JSON, constants, etc.

Golang versions

All golang services written by Clever should be on Go 1.16.

Style

The style guides used for Go are Effective Go and the community Go style guide. There are two tools that can be used to detect common mistakes.

  • go fmt should be run in any directory that contains go files. It will automatically format the code.
  • golint file.go should be run for every go file. It will lint the code and return any issues it finds.

Optionally you can have Vim run the linters for you:

" highlight Go errors
let g:syntastic_go_checkers = ['golint', 'govet']

Recommended setup

  • Makefiles: A Go package should have a Makefile that runs "golint" on all files. See the sfncli Makefile as an example.
  • toolchain version check
  • testing
    • gofmt
    • golint
    • go vet
    • go test
  • emacs: Go has an official emacs mode that ships with a gofmt command. To get it to run on save, you can add this to your .emacs:

    (add-hook 'before-save-hook 'gofmt-before-save)
    
  • sublime: Add GoSublime for code highlighting and go fmt on save.

  • vim:

    • The vim-go plugin adds a lot of functionality, including gofmt on save as well as much more.
    • Alternatively, if vim-go is too heavy for you, see the directions here. It's strongly advised to set up gofmt on save.

Best Practices

Sets

Sets are typically best represented by a map[key type]bool structure where every boolean value is set to true. Since maps return the default value of the value type when a key does not exist, they will return false (the default boolean value) when accessed with a nonexistent key. This allows you to use code like:

crew := map[string]bool{
    "Malcom":   true,
    "Zoe":      true,
    "Wash":     true,
    "Inara":    true,
    "Jayne":    true,
    "Kaylee":   true,
    "Simon":    true,
    "River":    true,
    "Shepherd": true,
}

// now we can use a map access like a boolean condition
if crew["River"] {
    println("member of the crew!")
}

// alternatively the "comma ok" idiom obfuscates what is happening
if _, isCrew := crew["River"]; isCrew {
    println("member of the crew!")
}

If you need more than addition and presence methods, please consider using a more full featured set implementation (https://github.com/fatih/set).

Docker Images

runtime

Drone should build your executable and it should be copied into Docker:

  • gliderlabs/alpine:3.2
    • smaller image footprint
    • requires additional build configuration
    • see catapult/Makefile as an example
  • debian:jessie
    • larger image
    • no special configuration needed
    • suggested if you have any dependencies you exec
    • see shorty/Dockerfile as an example

Dependencies

We use go mod to manage golang dependencies. See go/mod.

Deprecated tooling

Dep

See our dep documentation.

Glide

See our glide documentation

Godeps

See our godeps documentation.

Essential Godeps for extended instruction.