Last active
February 21, 2016 19:40
-
-
Save tylertreat/cd310a66af5a4a27b2c0 to your computer and use it in GitHub Desktop.
Closing file without defer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func findHelloWorld(filename string) error { | |
file, err := os.Open(filename) | |
if err != nil { | |
return err | |
} | |
defer file.Close() | |
scanner := bufio.NewScanner(file) | |
for scanner.Scan() { | |
if scanner.Text() == "hello, world!" { | |
return nil | |
} | |
} | |
if err := scanner.Err(); err != nil { | |
return err | |
} | |
return errors.New("Didn't find hello world") | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func findHelloWorld(filename string) error { | |
file, err := os.Open(filename) | |
if err != nil { | |
return err | |
} | |
scanner := bufio.NewScanner(file) | |
for scanner.Scan() { | |
if scanner.Text() == "hello, world!" { | |
file.Close() | |
return nil | |
} | |
} | |
file.Close() | |
if err := scanner.Err(); err != nil { | |
return err | |
} | |
return errors.New("Didn't find hello world") | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment