| Mateusz Zalega | 2904364 | 2021-12-02 15:04:14 +0100 | [diff] [blame] | 1 | # Monogon coding standards |
| Serge Bazanski | b309e3f | 2021-12-17 17:31:21 +0100 | [diff] [blame] | 2 | |
| Mateusz Zalega | 2904364 | 2021-12-02 15:04:14 +0100 | [diff] [blame] | 3 | ## Programming languages |
| Serge Bazanski | b309e3f | 2021-12-17 17:31:21 +0100 | [diff] [blame] | 4 | |
| Mateusz Zalega | 2904364 | 2021-12-02 15:04:14 +0100 | [diff] [blame] | 5 | ### Go |
| Serge Bazanski | b309e3f | 2021-12-17 17:31:21 +0100 | [diff] [blame] | 6 | While 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 | |
| 10 | Comment lines must not be longer than 80 characters. Non-comment lines may be any (reasonable) length. |
| 11 | |
| 12 | #### Imports |
| 13 | |
| 14 | Imports must be grouped and sorted in the following way: |
| 15 | |
| 16 | ```go |
| 17 | import ( |
| 18 | "errors" |
| 19 | "net/http" |
| 20 | |
| 21 | "example.com/some/external/thing" |
| 22 | "golang.org/x/crypto" |
| 23 | |
| Serge Bazanski | a81096f | 2022-05-18 13:32:52 +0200 | [diff] [blame^] | 24 | "source.monogon.dev/some/internal/library" |
| 25 | apkg "source.monogon.dev/some/other/pkg" |
| Serge Bazanski | b309e3f | 2021-12-17 17:31:21 +0100 | [diff] [blame] | 26 | ) |
| 27 | ``` |
| 28 | |
| 29 | Ie., 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 | |
| 31 | There can be multiple groups of a given class, but they must be in the right order: |
| 32 | |
| 33 | ```go |
| 34 | import ( |
| 35 | "errors" |
| 36 | "net/http" |
| 37 | |
| Serge Bazanski | a81096f | 2022-05-18 13:32:52 +0200 | [diff] [blame^] | 38 | "source.monogon.dev/some/internal/library" |
| 39 | "source.monogon.dev/some/internal/pkg" |
| Serge Bazanski | b309e3f | 2021-12-17 17:31:21 +0100 | [diff] [blame] | 40 | |
| Serge Bazanski | a81096f | 2022-05-18 13:32:52 +0200 | [diff] [blame^] | 41 | "source.monogon.dev/other/subtree/a" |
| 42 | "source.monogon.dev/other/subtree/b" |
| 43 | foo "source.monogon.dev/other/subtree/c" |
| Serge Bazanski | b309e3f | 2021-12-17 17:31:21 +0100 | [diff] [blame] | 44 | ) |
| 45 | ``` |
| Serge Bazanski | 144b786 | 2021-12-17 17:30:14 +0100 | [diff] [blame] | 46 | |
| 47 | A styleguide compliant fork of `goimports` (itself a superset of `gofmt`) can be built by running: |
| 48 | |
| 49 | $ bazel build //:goimports |
| 50 | |
| 51 | The 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 Bazanski | a81096f | 2022-05-18 13:32:52 +0200 | [diff] [blame^] | 52 | |
| 53 | When 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. |