Skip to content

Commit 57ea449

Browse files
authored
go: Export the latest version of internal guide. (#840)
The updates chiefly include copy editing, simplifications of code bodies, explanation of error handling in package initialization, discussion of zero value declarations, enhancement of initialisms, and further hardening of goroutine lifetimes and resource leak avoidance.
1 parent 75e85d0 commit 57ea449

File tree

2 files changed

+38
-14
lines changed

2 files changed

+38
-14
lines changed

go/best-practices.md

+17-8
Original file line numberDiff line numberDiff line change
@@ -387,12 +387,11 @@ import "path/to/creditcardtest"
387387

388388
func TestProcessor(t *testing.T) {
389389
var spyCC creditcardtest.Spy
390-
391390
proc := &Processor{CC: spyCC}
392391

393392
// declarations omitted: card and amount
394393
if err := proc.Process(card, amount); err != nil {
395-
t.Errorf("proc.Process(card, amount) = %v, want %v", got, want)
394+
t.Errorf("proc.Process(card, amount) = %v, want nil", err)
396395
}
397396

398397
charges := []creditcardtest.Charge{
@@ -420,7 +419,7 @@ func TestProcessor(t *testing.T) {
420419

421420
// declarations omitted: card and amount
422421
if err := proc.Process(card, amount); err != nil {
423-
t.Errorf("proc.Process(card, amount) = %v, want %v", got, want)
422+
t.Errorf("proc.Process(card, amount) = %v, want nil", err)
424423
}
425424

426425
charges := []creditcardtest.Charge{
@@ -1223,8 +1222,9 @@ func answer(i int) string {
12231222
```
12241223

12251224
[Do not call `log` functions before flags have been parsed.](https://pkg.go.dev/github.com/golang/glog#pkg-overview)
1226-
If you must die in an `init` func, a panic is acceptable in place of the logging
1227-
call.
1225+
If you must die in a package initialization function (an `init` or a
1226+
["must" function](decisions#must-functions)), a panic is acceptable in place of
1227+
the fatal logging call.
12281228

12291229
<a id="documentation"></a>
12301230

@@ -1723,7 +1723,7 @@ var i = 42
17231723
17241724
<a id="vardeclzero"></a>
17251725
1726-
### Non-pointer zero values
1726+
### Declaring variables with zero values
17271727
17281728
The following declarations use the [zero value]:
17291729
@@ -1760,6 +1760,15 @@ var coords Point
17601760
if err := json.Unmarshal(data, &coords); err != nil {
17611761
```
17621762
1763+
It is also okay to use the zero value in the following form when you need a
1764+
variable of a pointer type:
1765+
1766+
```go
1767+
// Good:
1768+
msg := new(pb.Bar) // or "&pb.Bar{}"
1769+
if err := proto.Unmarshal(data, msg); err != nil {
1770+
```
1771+
17631772
If you need a lock or other field that [must not be copied](decisions#copying)
17641773
in your struct, you can make it a value type to take advantage of zero value
17651774
initialization. It does mean that the containing type must now be passed via a
@@ -1792,7 +1801,7 @@ func NewCounter(name string) *Counter {
17921801
return c
17931802
}
17941803

1795-
var myMsg = new(pb.Bar) // or "&pb.Bar{}".
1804+
var msg = new(pb.Bar) // or "&pb.Bar{}".
17961805
```
17971806
17981807
This is because `*pb.Something` satisfies [`proto.Message`] while `pb.Something`
@@ -1806,7 +1815,7 @@ func NewCounter(name string) *Counter {
18061815
return &c
18071816
}
18081817

1809-
var myMsg = pb.Bar{}
1818+
var msg = pb.Bar{}
18101819
```
18111820
18121821
[`proto.Message`]: https://pkg.go.dev/google.golang.org/protobuf/proto#Message

go/decisions.md

+21-6
Original file line numberDiff line numberDiff line change
@@ -195,8 +195,8 @@ const (
195195

196196
Words in names that are initialisms or acronyms (e.g., `URL` and `NATO`) should
197197
have the same case. `URL` should appear as `URL` or `url` (as in `urlPony`, or
198-
`URLPony`), never as `Url`. This also applies to `ID` when it is short for
199-
"identifier"; write `appID` instead of `appId`.
198+
`URLPony`), never as `Url`. As a general rule, identifiers (e.g., `ID` and `DB`)
199+
should also be capitalized similar to their usage in English prose.
200200

201201
* In names with multiple initialisms (e.g. `XMLAPI` because it contains `XML`
202202
and `API`), each letter within a given initialism should have the same case,
@@ -211,7 +211,7 @@ have the same case. `URL` should appear as `URL` or `url` (as in `urlPony`, or
211211

212212
<!-- Keep this table narrow. If it must grow wider, replace with a list. -->
213213

214-
Initialism(s) | Scope | Correct | Incorrect
214+
English Usage | Scope | Correct | Incorrect
215215
------------- | ---------- | -------- | --------------------------------------
216216
XML API | Exported | `XMLAPI` | `XmlApi`, `XMLApi`, `XmlAPI`, `XMLapi`
217217
XML API | Unexported | `xmlAPI` | `xmlapi`, `xmlApi`
@@ -221,6 +221,11 @@ gRPC | Exported | `GRPC` | `Grpc`
221221
gRPC | Unexported | `gRPC` | `grpc`
222222
DDoS | Exported | `DDoS` | `DDOS`, `Ddos`
223223
DDoS | Unexported | `ddos` | `dDoS`, `dDOS`
224+
ID | Exported | `ID` | `Id`
225+
ID | Unexported | `id` | `iD`
226+
DB | Exported | `DB` | `Db`
227+
DB | Unexported | `db` | `dB`
228+
Txn | Exported | `Txn` | `TXN`
224229

225230
<!--#include file="/go/g3doc/style/includes/special-name-exception.md"-->
226231

@@ -1779,7 +1784,7 @@ canvas.RenderCube(cube,
17791784
```
17801785
17811786
Note that the lines in the above example are not wrapped at a specific column
1782-
boundary but are grouped based on co-ordinate triples.
1787+
boundary but are grouped based on coordinate triples.
17831788
17841789
Long string literals within functions should not be broken for the sake of line
17851790
length. For functions that include such strings, a line break can be added after
@@ -2043,6 +2048,8 @@ For errors that indicate "impossible" conditions, namely bugs that should always
20432048
be caught during code review and/or testing, a function may reasonably return an
20442049
error or call [`log.Fatal`].
20452050
2051+
Also see [when panic is acceptable](best-practices.md#when-to-panic).
2052+
20462053
**Note:** `log.Fatalf` is not the standard library log. See [#logging].
20472054
20482055
[Effective Go section on errors]: http://golang.org/doc/effective_go.html#errors
@@ -2176,12 +2183,18 @@ clear. It is conventionally managed with a `context.Context`:
21762183
```go
21772184
// Good:
21782185
func (w *Worker) Run(ctx context.Context) error {
2186+
var wg sync.WaitGroup
21792187
// ...
21802188
for item := range w.q {
21812189
// process returns at latest when the context is cancelled.
2182-
go process(ctx, item)
2190+
wg.Add(1)
2191+
go func() {
2192+
defer wg.Done()
2193+
process(ctx, item)
2194+
}()
21832195
}
21842196
// ...
2197+
wg.Wait() // Prevent spawned goroutines from outliving this function.
21852198
}
21862199
```
21872200
@@ -2222,12 +2235,14 @@ See also:
22222235
* Rethinking Classical Concurrency Patterns: [slides][rethinking-slides],
22232236
[video][rethinking-video]
22242237
* [When Go programs end]
2238+
* [Documentation Conventions: Contexts]
22252239
22262240
[synchronous functions]: #synchronous-functions
22272241
[cheney-stop]: https://dave.cheney.net/2016/12/22/never-start-a-goroutine-without-knowing-how-it-will-stop
22282242
[rethinking-slides]: https://drive.google.com/file/d/1nPdvhB0PutEJzdCq5ms6UI58dp50fcAN/view
22292243
[rethinking-video]: https://www.youtube.com/watch?v=5zXAHh5tJqQ
22302244
[When Go programs end]: https://changelog.com/gotime/165
2245+
[Documentation Conventions: Contexts]: best-practices.md#documentation-conventions-contexts
22312246
22322247
<a id="interfaces"></a>
22332248
@@ -2737,7 +2752,7 @@ formatting to do.
27372752
See also:
27382753
27392754
* Best practices on [logging errors](best-practices#error-logging) and
2740-
[custom verbosily levels](best-practices#vlog)
2755+
[custom verbosity levels](best-practices#vlog)
27412756
* When and how to use the log package to
27422757
[stop the program](best-practices#checks-and-panics)
27432758

0 commit comments

Comments
 (0)