Group Similar Declarations
Go supports grouping similar declarations.
Bad | Good |
---|
- import "a"
- import "b"
|
- import (
- "a"
- "b"
- )
|
This also applies to constants, variables, and type declarations.
Bad | Good |
---|
- const a = 1
- const b = 2
-
-
-
- var a = 1
- var b = 2
-
-
-
- type Area float64
- type Volume float64
|
- const (
- a = 1
- b = 2
- )
-
- var (
- a = 1
- b = 2
- )
-
- type (
- Area float64
- Volume float64
- )
|
Only group related declarations. Do not group declarations that are unrelated.
Bad | Good |
---|
- type Operation int
-
- const (
- Add Operation = iota + 1
- Subtract
- Multiply
- ENV_VAR = "MY_ENV"
- )
|
- type Operation int
-
- const (
- Add Operation = iota + 1
- Subtract
- Multiply
- )
-
- const ENV_VAR = "MY_ENV"
|
Groups are not limited in where they can be used. For example, you can use theminside of functions.
Bad | Good |
---|
- func f() string {
- var red = color.New(0xff0000)
- var green = color.New(0x00ff00)
- var blue = color.New(0x0000ff)
-
- …
- }
|
- func f() string {
- var (
- red = color.New(0xff0000)
- green = color.New(0x00ff00)
- blue = color.New(0x0000ff)
- )
-
- …
- }
|