Go Development Conventions
Introduction
This guide documents development conventions for Go at AiB. Check general conventions for language-independent conventions that are also applicable to Go projects.
Supported Go Versions
- Our libraries support the latest two stable major versions of Go. Both versions should be included in CI.
- Our applications support only the latest stable Go version.
Dependency Management
If your project is a library or an application:
- Use modules to manage dependencies.
- Do not check in the
vendor
directory into git.
Code Generation
- All generated code should be always processed by
gofmt
. - Generated code should have a
// Code generated ... DO NOT EDIT.
comment before the package clause, but not attached to it (see convention).
Docker
- If you use alpine-based images, your binaries need to be built with
CGO_ENABLED=0
. If your project usescgo
, you will have to use alpine for the build of binaries for Docker.
Testing
- Use testify.
Error handling
Use the standard errors
package and wrap errors using %w
when needed.
Logging
- Use logging in your applications.
- Use zerlog.
Other libraries
Missing info
TODO: fill with other generally used libraries.
Best Practices
- Use
gofmt
,goimports
,go vet
. - Use
golangci-lint
. Configuration example here - Follow community well-established best practices: Effective Go and Go Code Review Comments.
Group code blocks with blank lines
To improve readability, we use single blank lines to separate logic blocks of code inside functions. A blank line is usually added after the end of any control structure, too.
Naming
For naming: follow Uber Go style guide
CLI
- Put sources for your executable commands in
cmd/<command-name>/
, with themain
function incmd/<command-name>/main.go
. - We use ff extensively for CLI options parsing.
- Implement commands in a subpackage.
Configuration
- Use environment variables for configuration.
Missing info
TODO: Add other possible configurations
Last update:
August 2, 2023
Created: July 24, 2023
Created: July 24, 2023