Slide 1

Slide 1 text

The Boring Programmer Best Practices in Go

Slide 2

Slide 2 text

Project Structure

Slide 3

Slide 3 text

Packages Packages should contain code that has a single purpose. Group of packages that provide the same set of functionalities should be organized under the same parent. The choices of your package have a huge impact on the testability.

Slide 4

Slide 4 text

github.com/simplaex/lighthouse ├── cmd/ │ └──lighthouse/ │ ├── subcmd/ │ └── lighthouse.go ├── internal/ │ ├── api/ │ ├── datastore/ │ ├── model/ │ ├── registrations/ │ └── platform/ | └── vendor/ ├── github.com/ │ ├── golang/ │ ├── go-kit/ └── golang.org/ All programs go under cmd. Each subdirectory has a matching source code file containing the main package Packages under internal can be imported within the project, but not from outside Model packages for your domain entities. Datastore package to implement your database queries. Dependencies go under vendor.

Slide 5

Slide 5 text

Concurrency

Slide 6

Slide 6 text

Channels orchestrate Mutexes serialize

Slide 7

Slide 7 text

No content

Slide 8

Slide 8 text

No content

Slide 9

Slide 9 text

No content

Slide 10

Slide 10 text

No content

Slide 11

Slide 11 text

No content

Slide 12

Slide 12 text

No content

Slide 13

Slide 13 text

Dependencies

Slide 14

Slide 14 text

Dep dep is a prototype dependency management tool for Go. dep is safe for production use. dep is the official experiment, but not yet the official tool. $ dep ensure $ dep ensure -add github.com/foo/bar $ ls Gopkg.toml Gopkg.lock

Slide 15

Slide 15 text

CI Setup Use gometalinter github.com/alecthomas/gometalinter. Over 30 tools for statically checking Go source for errors and warnings. Not only about nit-picking, actually points towards potential sources of bugs. Always test with -race. Especially with you integration tests, try to perform your actions concurrently to uncover data races. Data races crash your program.

Slide 16

Slide 16 text

CGO

Slide 17

Slide 17 text

CGO IS NOT GO

Slide 18

Slide 18 text

CGO - Cgo must always be guarded with build tags // +build linux,386 darwin,!cgo - Context switches are very expensive - You assume Cgo will be fast - But calls to C are actually atrocious expensive - Treat it like a microservice

Slide 19

Slide 19 text

Interface{} says Nothing

Slide 20

Slide 20 text

Gofmt’s style is no one’s favorite, yet gofmt is everyone’s favorite

Slide 21

Slide 21 text

Errors are values, too

Slide 22

Slide 22 text

Clear is better than Clever

Slide 23

Slide 23 text

Clear is better than Clever - Go code should be straight up boring - If you look at it, you should understand what it’s doing - You don’t put explanations into the comment - Being overly clever will hurt you in the long-run - The boring programmer writes strict and rigid code - That’s why Go is great for production-grade code

Slide 24

Slide 24 text

Thank you

Slide 25

Slide 25 text

https://invite.slack.golangbridge.org/ Join Gophers Slack