2019-02-25 22:44:14 +00:00
|
|
|
# Conventions
|
|
|
|
|
2021-09-06 15:40:41 +00:00
|
|
|
This document will list conventions that this repo should follow. These are
|
2022-04-20 18:30:09 +00:00
|
|
|
the guidelines, and if you believe that one should not be followed, please state
|
2021-09-06 15:40:41 +00:00
|
|
|
why in your PR. If you believe that a piece of code does not follow one of the
|
2022-04-20 18:30:09 +00:00
|
|
|
conventions listed, please open an issue before making any changes.
|
2019-02-25 22:44:14 +00:00
|
|
|
|
2021-09-06 15:40:41 +00:00
|
|
|
When submitting a new convention, please open an issue for discussion, if
|
|
|
|
possible please highlight parts in the code where this convention could help the
|
|
|
|
code readability or simplicity.
|
2019-02-25 22:44:14 +00:00
|
|
|
|
2021-09-06 15:40:41 +00:00
|
|
|
## Avoid named return parameters
|
2019-02-25 22:44:14 +00:00
|
|
|
|
|
|
|
func example(test int) (num int) {
|
|
|
|
a = test + 1
|
|
|
|
num = a * test
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-09-06 15:40:41 +00:00
|
|
|
In the above function we have used a named return parameter, which allows you to
|
|
|
|
include a simple return statement without the variables you are returning. This
|
|
|
|
practice can cause confusion when functions become large or the logic becomes
|
|
|
|
complex, so these should be avoided.
|
2020-08-06 16:09:57 +00:00
|
|
|
|
|
|
|
## Use error wrapping
|
|
|
|
|
|
|
|
Bad:
|
|
|
|
```
|
|
|
|
err = SomeAPI()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("something bad happened: %v", err)
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
Good:
|
|
|
|
```
|
|
|
|
err = SomeAPI()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("something bad happened: %w", err)
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
Error wrapping allows `errors.Is` and `errors.As` usage in upper layer
|
|
|
|
functions which might be useful.
|