snapshot of https://github.com/uber-go/guide.git (master, 2026-09-15; blobs > 131072B omitted)
This commit is contained in:
41
src/struct-field-key.md
Normal file
41
src/struct-field-key.md
Normal file
@@ -0,0 +1,41 @@
|
||||
# Use Field Names to Initialize Structs
|
||||
|
||||
You should almost always specify field names when initializing structs. This is
|
||||
now enforced by [`go vet`].
|
||||
|
||||
[`go vet`]: https://pkg.go.dev/cmd/vet
|
||||
|
||||
<table>
|
||||
<thead><tr><th>Bad</th><th>Good</th></tr></thead>
|
||||
<tbody>
|
||||
<tr><td>
|
||||
|
||||
```go
|
||||
k := User{"John", "Doe", true}
|
||||
```
|
||||
|
||||
</td><td>
|
||||
|
||||
```go
|
||||
k := User{
|
||||
FirstName: "John",
|
||||
LastName: "Doe",
|
||||
Admin: true,
|
||||
}
|
||||
```
|
||||
|
||||
</td></tr>
|
||||
</tbody></table>
|
||||
|
||||
Exception: Field names *may* be omitted in test tables when there are 3 or
|
||||
fewer fields.
|
||||
|
||||
```go
|
||||
tests := []struct{
|
||||
op Operation
|
||||
want string
|
||||
}{
|
||||
{Add, "add"},
|
||||
{Subtract, "subtract"},
|
||||
}
|
||||
```
|
||||
Reference in New Issue
Block a user