Merge pull request #9806 from guggero/formatting-list-initializations

docs: document formatting for slice definitions
This commit is contained in:
Oliver Gugger
2025-08-07 01:49:54 -06:00
committed by GitHub

View File

@@ -230,9 +230,13 @@ value, err := bar(
)
```
As long as the visual symmetry of the opening and closing parentheses is
preserved, arguments that would otherwise introduce a new level of indentation
are allowed to be written in a more compact form.
As long as the visual symmetry of the opening and closing parentheses (or curly
braces) is preserved, arguments that would otherwise introduce a new level of
indentation are allowed to be written in a more compact form.
Visual symmetry here means that when two or more opening parentheses or curly
braces are on the same line, then they must also be closed on the same line.
And the closing line needs to have the same indentation level as the opening
line.
Example with inline struct creation:
@@ -254,6 +258,13 @@ Example with inline struct creation:
})
```
**WRONG**
```go
response, err := node.AddInvoice(ctx, &lnrpc.Invoice{
Memo: "invoice",
ValueMsat: int64(oneUnitMilliSat - 1)})
```
Example with nested function call:
**ACCEPTABLE**:
@@ -448,6 +459,65 @@ func foo(a, b, c,
}
```
### Inline slice definitions
In Go a list of slices can be initialized with values directly, using curly
braces. Whenever possible, the more verbose/indented style should be used for
better readability and easier git diff handling. Because that results in more
levels of code indentation, the more compact version is allowed in situations
where the remaining space would otherwise be too restricted, resulting in too
long lines (or excessive use of the `// nolint: ll` directive).
**ACCEPTABLE**
```go
testCases := []testCase{{
name: "spend exactly all",
coins: []wallet.Coin{{
TxOut: wire.TxOut{
PkScript: p2wkhScript,
Value: 1 * btcutil.SatoshiPerBitcoin,
},
}},
}, {
name: "spend more",
coins: []wallet.Coin{{
TxOut: wire.TxOut{
PkScript: p2wkhScript,
Value: 1 * btcutil.SatoshiPerBitcoin,
},
}},
}}
```
**PREFERRED**
```go
coin := btcutil.SatoshiPerBitcoin
testCases := []testCase{
{
name: "spend exactly all",
coins: []wallet.Coin{
{
TxOut: wire.TxOut{
PkScript: p2wkhScript,
Value: 1 * coin,
},
},
},
},
{
name: "spend more",
coins: []wallet.Coin{
{
TxOut: wire.TxOut{
PkScript: p2wkhScript,
Value: 1 * coin,
},
},
},
},
}
```
## Recommended settings for your editor
To make it easier to follow the rules outlined above, we recommend setting up