Golang Patterns and Approaches
We initially trialled golang on a number of smaller services and tools (Sirius User Admin, ECS stabiliser). We’ve since started rolling it out in other services (Supervision Workflow, Search Service). Now we have a bit more experience working with the language. It’s worth documenting our preferred approaches, tools and libraries for doing so. This will help future developers get up to speed on our services more easily and set some expectations for team members to reference when starting new work.
Why Golang?
We chose to adopt golang as a secondary language within OPG Digital because:
- Modern language
- Small ecosystem
- Web features as first class citizens
- Performant and produces small containers
- Aligned to infrastructure tooling languages
Prefered Tools & Libraries
- golangci-lint - Fast golang linter for checking code style in CI or locally. This bundles lots of the other linters in the golang ecosystem including gosec to check for potential security issues. Install it to your local machine with brew install golangci-lint
- testify - Testing tools, including more readable assertions and mocking tools.
- opg-go-healthcheck - Adds a healthcheck to your go service for use with ECS and other container orchestration systems.
- aws-sdk-go-v2 - AWS SDK for golang. Because we use lots of AWS. Make sure you move to v2.
- air - Allows hot reloading during development so you don’t need to manually recompile on changes.
- zap - A nice structured logging library for JSON output
Tools useful in services
- aws-secretsmanager-caching-go - Caching secrets in memory and calling for new ones if they fail rather than using environment vars.
- jwt-go - JWT implementation for go, useful for talking to Sirius APIs.
- objx - Useful tools for dealing with JSON and maps.
- go-spew - A nicer pretty printer for development/debugging.
Note we previously recommended gorilla/mux, but that set of libraries had maintenence issues and since that time the core HTTP router has evolved to cover most uses cases. Use the internal HTTP router unless you have a very odd edge case, see the golang 1.22 release notes.
Preferred Patterns
Uber-go - Go has a very good built-in formatter for writing correct Go. This Uber style README.md covers extra stylistic concerns not covered by Go’s standard practices. For example the Verifying Interface Compliance
Generally our golang services are microservices and micro-frontends. This allows us to build small, isolated components focused on a particular domain that can be deployed independently. One repo per service.
Our normal coding standards and repo principles still apply.
go:embed annotations are useful for bundling templates
Our micro-frontends use cypress for end-to-end testing.
We recommend you add in pre-commit hooks to your repo.
Learning Resources
- https://tour.golang.org/ - The official Golang tour for learning Go. Very informative
- https://gobyexample.com - Introductory site that explains with clear examples.
- https://github.com/tmrts/go-patterns - A whole GitHub repo full of READMEs on different design patterns you can use when building out a application
- https://lets-go.alexedwards.net/#packages - Build a Go web application. Alex Edwards is a very well known Go engineer who’s written a very informative book on building a whole web application in Go. (Might have to ask line manager about getting the money to buy the book) His main blog also has some very good free resources on different topics in Go
- https://github.com/golang/go/wiki/CodeReviewComments - Resources you can use when reviewing a PR to try and make sure the Go code is following Go’s best practices.
- https://medium.com/wesionary-team/pointers-and-passby-value-reference-in-golang-a00c8c59b7f1 - understanding Pointers and Values
- https://github.com/dariubs/GoBooks - GitHub repo with a list of a bunch of books which you may find useful
Video Courses
Ask your line manager about a Pluralsight license.
- https://www.pluralsight.com/courses/creating-web-applications-go-update - Creating Web Applications with Go, quick and simple intro course focusing on web applications.
- https://app.pluralsight.com/library/courses/go-create-test-applications - Testing Go Applications, look at golang test tools, including its benchmarking library
- https://app.pluralsight.com/library/courses/go-delve-debugging-applications - A look at using the golang debugger with your code