Skip to content

Commit

Permalink
Clarify guidance for grouping consecutive variable declarations (#140)
Browse files Browse the repository at this point in the history
  • Loading branch information
mway committed Jan 18, 2022
1 parent 22f61e2 commit c9b887c
Showing 1 changed file with 43 additions and 5 deletions.
48 changes: 43 additions & 5 deletions style.md
Original file line number Diff line number Diff line change
Expand Up @@ -2195,11 +2195,11 @@ inside of functions.

```go
func f() string {
var red = color.New(0xff0000)
var green = color.New(0x00ff00)
var blue = color.New(0x0000ff)
red := color.New(0xff0000)
green := color.New(0x00ff00)
blue := color.New(0x0000ff)

...
// ...
}
```

Expand All @@ -2213,7 +2213,45 @@ func f() string {
blue = color.New(0x0000ff)
)

...
// ...
}
```

</td></tr>
</tbody></table>

Exception: Variable declarations, particularly inside functions, should be
grouped together if declared adjacent to other variables. Do this for variables
declared together even if they are unrelated.

<table>
<thead><tr><th>Bad</th><th>Good</th></tr></thead>
<tbody>
<tr><td>

```go
func (c *client) request() {
caller := c.name
format := "json"
timeout := 5*time.Second
var err error

// ...
}
```

</td><td>

```go
func (c *client) request() {
var (
caller = c.name
format = "json"
timeout = 5*time.Second
err error
)

// ...
}
```

Expand Down

0 comments on commit c9b887c

Please sign in to comment.