mirror of
https://github.com/lightningnetwork/lnd.git
synced 2025-11-10 22:27:22 +01:00
Merge pull request #9806 from guggero/formatting-list-initializations
docs: document formatting for slice definitions
This commit is contained in:
@@ -230,9 +230,13 @@ value, err := bar(
|
|||||||
)
|
)
|
||||||
```
|
```
|
||||||
|
|
||||||
As long as the visual symmetry of the opening and closing parentheses is
|
As long as the visual symmetry of the opening and closing parentheses (or curly
|
||||||
preserved, arguments that would otherwise introduce a new level of indentation
|
braces) is preserved, arguments that would otherwise introduce a new level of
|
||||||
are allowed to be written in a more compact form.
|
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:
|
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:
|
Example with nested function call:
|
||||||
|
|
||||||
**ACCEPTABLE**:
|
**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
|
## Recommended settings for your editor
|
||||||
|
|
||||||
To make it easier to follow the rules outlined above, we recommend setting up
|
To make it easier to follow the rules outlined above, we recommend setting up
|
||||||
|
|||||||
Reference in New Issue
Block a user