2019-03-14 10:17:20 -05:00
# Backend style guide
2020-07-30 10:48:41 -05:00
Grafana's backend has been developed for a long time with a mix of code styles. This guide explains how we want to write Go code in the future.
2019-03-14 10:17:20 -05:00
2019-10-30 15:59:27 -05:00
Unless stated otherwise, use the guidelines listed in the following articles:
- [Effective Go ](https://golang.org/doc/effective_go.html )
- [Code Review Comments ](https://github.com/golang/go/wiki/CodeReviewComments )
- [Go: Best Practices for Production Environments ](http://peter.bourgon.org/go-in-production/#formatting-and-style )
2019-03-14 10:17:20 -05:00
## Linting and formatting
2019-10-30 15:59:27 -05:00
To ensure consistency across the Go codebase, we require all code to pass a number of linter checks.
We use the standard following linters:
- [gofmt ](https://golang.org/cmd/gofmt/ )
- [golint ](https://github.com/golang/lint )
- [go vet ](https://golang.org/cmd/vet/ )
In addition to the standard linters, we also use:
- [revive ](https://revive.run/ ) with a [custom config ](https://github.com/grafana/grafana/blob/master/conf/revive.toml )
- [GolangCI-Lint ](https://github.com/golangci/golangci-lint )
- [gosec ](https://github.com/securego/gosec )
To run all linters, use the `lint-go` Makefile target:
2019-07-02 08:06:59 -05:00
```bash
2019-10-30 15:59:27 -05:00
make lint-go
2019-07-02 08:06:59 -05:00
```
2019-03-14 10:17:20 -05:00
2019-10-30 15:59:27 -05:00
## Testing
2019-03-14 10:17:20 -05:00
2019-10-30 15:59:27 -05:00
We value clean and readable code, that is loosely coupled and covered by unit tests. This makes it easier to collaborate and maintain the code.
2019-06-18 02:31:51 -05:00
2019-10-30 15:59:27 -05:00
Tests must use the standard library, `testing` . For assertions, prefer using [testify ](https://github.com/stretchr/testify ).
2019-03-14 10:17:20 -05:00
2019-10-30 15:59:27 -05:00
The majority of our tests uses [GoConvey ](http://goconvey.co/ ) but that's something we want to avoid going forward.
2019-06-18 02:31:51 -05:00
2019-10-30 15:59:27 -05:00
In the `sqlstore` package we do database operations in tests and while some might say that's not suited for unit tests. We think they are fast enough and provide a lot of value.