From: Emmanuel Odeke Date: Wed, 26 Apr 2017 02:21:50 +0000 (-0600) Subject: net: defer file.close() + minor style cleanup X-Git-Tag: go1.9beta1~464 X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=c433c374b53fc6484b571a606581f3f067a6f97a;p=gostls13.git net: defer file.close() + minor style cleanup Moved the relevant file.close() usages close to after the file opens and put them in defer statements, so that readers don't have to think too much as to where the file is being closed. Change-Id: Ic4190b02ea2f5ac281b9ba104e0023e9f87ca8c7 Reviewed-on: https://go-review.googlesource.com/41796 Reviewed-by: Brad Fitzpatrick Run-TryBot: Brad Fitzpatrick TryBot-Result: Gobot Gobot --- diff --git a/src/net/ipsock_plan9.go b/src/net/ipsock_plan9.go index 7e24fbc227..312e4adb47 100644 --- a/src/net/ipsock_plan9.go +++ b/src/net/ipsock_plan9.go @@ -28,6 +28,7 @@ func probe(filename, query string) bool { if file, err = open(filename); err != nil { return false } + defer file.close() r := false for line, ok := file.readLine(); ok && !r; line, ok = file.readLine() { @@ -42,7 +43,6 @@ func probe(filename, query string) bool { } } } - file.close() return r } diff --git a/src/net/lookup_unix.go b/src/net/lookup_unix.go index 8d4b7bddf4..158cc94a99 100644 --- a/src/net/lookup_unix.go +++ b/src/net/lookup_unix.go @@ -16,28 +16,31 @@ var onceReadProtocols sync.Once // readProtocols loads contents of /etc/protocols into protocols map // for quick access. func readProtocols() { - if file, err := open("/etc/protocols"); err == nil { - for line, ok := file.readLine(); ok; line, ok = file.readLine() { - // tcp 6 TCP # transmission control protocol - if i := byteIndex(line, '#'); i >= 0 { - line = line[0:i] - } - f := getFields(line) - if len(f) < 2 { - continue + file, err := open("/etc/protocols") + if err != nil { + return + } + defer file.close() + + for line, ok := file.readLine(); ok; line, ok = file.readLine() { + // tcp 6 TCP # transmission control protocol + if i := byteIndex(line, '#'); i >= 0 { + line = line[0:i] + } + f := getFields(line) + if len(f) < 2 { + continue + } + if proto, _, ok := dtoi(f[1]); ok { + if _, ok := protocols[f[0]]; !ok { + protocols[f[0]] = proto } - if proto, _, ok := dtoi(f[1]); ok { - if _, ok := protocols[f[0]]; !ok { - protocols[f[0]] = proto - } - for _, alias := range f[2:] { - if _, ok := protocols[alias]; !ok { - protocols[alias] = proto - } + for _, alias := range f[2:] { + if _, ok := protocols[alias]; !ok { + protocols[alias] = proto } } } - file.close() } } diff --git a/src/net/port_unix.go b/src/net/port_unix.go index 868d1e4784..829f51fcf0 100644 --- a/src/net/port_unix.go +++ b/src/net/port_unix.go @@ -17,6 +17,8 @@ func readServices() { if err != nil { return } + defer file.close() + for line, ok := file.readLine(); ok; line, ok = file.readLine() { // "http 80/tcp www www-http # World Wide Web HTTP" if i := byteIndex(line, '#'); i >= 0 { @@ -43,7 +45,6 @@ func readServices() { } } } - file.close() } // goLookupPort is the native Go implementation of LookupPort.