]> Cypherpunks repositories - gostls13.git/commitdiff
net: defer file.close() + minor style cleanup
authorEmmanuel Odeke <emm.odeke@gmail.com>
Wed, 26 Apr 2017 02:21:50 +0000 (20:21 -0600)
committerBrad Fitzpatrick <bradfitz@golang.org>
Wed, 26 Apr 2017 03:12:20 +0000 (03:12 +0000)
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 <bradfitz@golang.org>
Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>

src/net/ipsock_plan9.go
src/net/lookup_unix.go
src/net/port_unix.go

index 7e24fbc2271e83dfdb9cd734fb392ab5ad5311c8..312e4adb47debef9e1fe659d30d8946814b9f4a0 100644 (file)
@@ -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
 }
 
index 8d4b7bddf4e551e5742b762749349d7b015c4f40..158cc94a99dfa77539b8b65096d42ebaac5e19d8 100644 (file)
@@ -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()
        }
 }
 
index 868d1e4784f2110e4e152a5d5e2a49f0d5bb5e01..829f51fcf0f438260a96440d286ed8dd2acf7f8a 100644 (file)
@@ -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.