Skip to main content

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

Video Courses

Ask your line manager about a Pluralsight license.

This page was last reviewed on 26 June 2024. It needs to be reviewed again on 26 June 2025 by the page owner #opg-developers .
This page was set to be reviewed before 26 June 2025 by the page owner #opg-developers. This might mean the content is out of date.