Created
September 27, 2017 12:52
-
-
Save tsonglew/9702255015e14df1d8f95a852a0ce0c8 to your computer and use it in GitHub Desktop.
read from file
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 Content(filename string) (string, error) { | |
f, err = os.Open(filename) | |
if err != nil { | |
return nil, err | |
} | |
defer f.Close() | |
var result []byte | |
buf := make([]byte, 100) | |
for { | |
n, err := f.Read(buf[0:]) | |
result = append(result, buf[0:n]...) | |
if err != nil { | |
if err == io.EOF { | |
break | |
} | |
return nil, err | |
} | |
} | |
return string(result), nil | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment