2017-10-06 10:23:49 +00:00
|
|
|
|
# Code Style
|
|
|
|
|
|
2020-07-14 11:58:00 +00:00
|
|
|
|
In addition to standard Go code style (`gofmt`, `goimports`), we use `golangci-lint`
|
|
|
|
|
to run a number of linters, the exact list can be found under linters in [.golangci.yml](.golangci.yml).
|
2020-12-07 11:23:01 +00:00
|
|
|
|
[Installation](https://github.com/golangci/golangci-lint#install-golangci-lint) and [Editor
|
|
|
|
|
Integration](https://golangci-lint.run/usage/integrations/#editor-integration) for
|
2019-12-25 18:28:10 +00:00
|
|
|
|
it can be found in the readme of golangci-lint.
|
2017-10-06 10:23:49 +00:00
|
|
|
|
|
|
|
|
|
For rare cases where a linter is giving a spurious warning, it can be disabled
|
2019-12-25 18:28:10 +00:00
|
|
|
|
for that line or statement using a [comment
|
2020-12-07 11:23:01 +00:00
|
|
|
|
directive](https://golangci-lint.run/usage/false-positives/#nolint), e.g. `var
|
2019-12-25 18:28:10 +00:00
|
|
|
|
bad_name int //nolint:golint,unused`. This should be used sparingly and only
|
|
|
|
|
when its clear that the lint warning is spurious.
|
2017-10-06 10:23:49 +00:00
|
|
|
|
|
2020-08-24 17:13:43 +00:00
|
|
|
|
The linters can be run using [build/scripts/find-lint.sh](/build/scripts/find-lint.sh)
|
2017-10-17 16:11:00 +00:00
|
|
|
|
(see file for docs) or as part of a build/test/lint cycle using
|
2020-08-24 17:13:43 +00:00
|
|
|
|
[build/scripts/build-test-lint.sh](/build/scripts/build-test-lint.sh).
|
2017-10-17 16:11:00 +00:00
|
|
|
|
|
2017-10-06 10:23:49 +00:00
|
|
|
|
|
2020-07-14 11:58:00 +00:00
|
|
|
|
## Labels
|
2017-10-06 10:23:49 +00:00
|
|
|
|
|
2020-07-14 11:58:00 +00:00
|
|
|
|
In addition to `TODO` and `FIXME` we also use `NOTSPEC` to identify deviations
|
|
|
|
|
from the Matrix specification.
|
2017-10-06 10:23:49 +00:00
|
|
|
|
|
2017-11-22 09:35:25 +00:00
|
|
|
|
## Logging
|
|
|
|
|
|
|
|
|
|
We generally prefer to log with static log messages and include any dynamic
|
|
|
|
|
information in fields.
|
|
|
|
|
|
|
|
|
|
```golang
|
|
|
|
|
logger := util.GetLogger(ctx)
|
|
|
|
|
|
|
|
|
|
// Not recommended
|
|
|
|
|
logger.Infof("Finished processing keys for %s, number of keys %d", name, numKeys)
|
|
|
|
|
|
|
|
|
|
// Recommended
|
|
|
|
|
logger.WithFields(logrus.Fields{
|
|
|
|
|
"numberOfKeys": numKeys,
|
|
|
|
|
"entityName": name,
|
|
|
|
|
}).Info("Finished processing keys")
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
This is useful when logging to systems that natively understand log fields, as
|
|
|
|
|
it allows people to search and process the fields without having to parse the
|
|
|
|
|
log message.
|
|
|
|
|
|
|
|
|
|
|
2017-10-06 10:23:49 +00:00
|
|
|
|
## Visual Studio Code
|
|
|
|
|
|
|
|
|
|
If you use VSCode then the following is an example of a workspace setting that
|
|
|
|
|
sets up linting correctly:
|
|
|
|
|
|
|
|
|
|
```json
|
|
|
|
|
{
|
2019-12-25 18:28:10 +00:00
|
|
|
|
"go.lintTool":"golangci-lint",
|
|
|
|
|
"go.lintFlags": [
|
|
|
|
|
"--fast"
|
|
|
|
|
]
|
2017-10-06 10:23:49 +00:00
|
|
|
|
}
|
|
|
|
|
```
|