Skip to content

Commit a204ed5

Browse files
1911860538gopherbot
authored andcommitted
net: simplify readProtocols via sync.OnceFunc
In this case, using sync.OnceFunc is a better choice. Change-Id: I52d27b9741265c90300a04a03537020e1aaaaaa7 GitHub-Last-Rev: a281dae GitHub-Pull-Request: golang#73434 Reviewed-on: https://go-review.googlesource.com/c/go/+/666635 Reviewed-by: David Chase <drchase@google.com> Auto-Submit: Jorropo <jorropo.pgm@gmail.com> Reviewed-by: Damien Neil <dneil@google.com> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Reviewed-by: Jorropo <jorropo.pgm@gmail.com>
1 parent ad04342 commit a204ed5

File tree

1 file changed

+4
-6
lines changed

1 file changed

+4
-6
lines changed

src/net/lookup_unix.go

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,9 @@ import (
1212
"sync"
1313
)
1414

15-
var onceReadProtocols sync.Once
16-
17-
// readProtocols loads contents of /etc/protocols into protocols map
15+
// readProtocolsOnce loads contents of /etc/protocols into protocols map
1816
// for quick access.
19-
func readProtocols() {
17+
var readProtocolsOnce = sync.OnceFunc(func() {
2018
file, err := open("/etc/protocols")
2119
if err != nil {
2220
return
@@ -43,12 +41,12 @@ func readProtocols() {
4341
}
4442
}
4543
}
46-
}
44+
})
4745

4846
// lookupProtocol looks up IP protocol name in /etc/protocols and
4947
// returns correspondent protocol number.
5048
func lookupProtocol(_ context.Context, name string) (int, error) {
51-
onceReadProtocols.Do(readProtocols)
49+
readProtocolsOnce()
5250
return lookupProtocolMap(name)
5351
}
5452

0 commit comments

Comments
 (0)