blob: b538552e58bd8533f2e207772f4540a8abbe5489 [file] [log] [blame] [view]
Mateusz Zalega29043642021-12-02 15:04:14 +01001# Monogon coding standards
Serge Bazanskib309e3f2021-12-17 17:31:21 +01002
Mateusz Zalega29043642021-12-02 15:04:14 +01003## Programming languages
Serge Bazanskib309e3f2021-12-17 17:31:21 +01004
Mateusz Zalega29043642021-12-02 15:04:14 +01005### Go
Serge Bazanskib309e3f2021-12-17 17:31:21 +01006While working on a change, please adhere to practices documented in the chapter about [frequently occurring code review comments](https://github.com/golang/go/wiki/CodeReviewComments) of [the Go wiki](https://github.com/golang/go/wiki/). In addition, some Monogon-specific style is enforced at build time as follows:
7
8#### Line Length
9
10Comment lines must not be longer than 80 characters. Non-comment lines may be any (reasonable) length.
11
12#### Imports
13
14Imports must be grouped and sorted in the following way:
15
16```go
17import (
18 "errors"
19 "net/http"
20
21 "example.com/some/external/thing"
22 "golang.org/x/crypto"
23
Serge Bazanskia81096f2022-05-18 13:32:52 +020024 "source.monogon.dev/some/internal/library"
25 apkg "source.monogon.dev/some/other/pkg"
Serge Bazanskib309e3f2021-12-17 17:31:21 +010026)
27```
28
29Ie., all stdlib imports must come before all non-Monogon ('global') imports, which must come before all Monogon ('local') imports. Within each group, imports must be sorted.
30
31There can be multiple groups of a given class, but they must be in the right order:
32
33```go
34import (
35 "errors"
36 "net/http"
37
Serge Bazanskia81096f2022-05-18 13:32:52 +020038 "source.monogon.dev/some/internal/library"
39 "source.monogon.dev/some/internal/pkg"
Serge Bazanskib309e3f2021-12-17 17:31:21 +010040
Serge Bazanskia81096f2022-05-18 13:32:52 +020041 "source.monogon.dev/other/subtree/a"
42 "source.monogon.dev/other/subtree/b"
43 foo "source.monogon.dev/other/subtree/c"
Serge Bazanskib309e3f2021-12-17 17:31:21 +010044)
45```
Serge Bazanski144b7862021-12-17 17:30:14 +010046
47A styleguide compliant fork of `goimports` (itself a superset of `gofmt`) can be built by running:
48
49 $ bazel build //:goimports
50
51The resulting binary can then be copied to anywhere in the filesystem (eg. $HOME/bin/goimports-monogon) and any editor which supports gofmt/goimports integration can be pointed at this tool to automatically reformat files to the required format.
Serge Bazanskia81096f2022-05-18 13:32:52 +020052
53When setting up integration with a text editor (or calling the binary manually), you must make it so that goimports get called `-local source.monogon.dev/`. Otherwise goimports will not correctly split away local/Monogon (`source.monogon.dev`) imports from other (global) imports.