Created
August 22, 2013 00:20
-
-
Save johnwesonga/6301861 to your computer and use it in GitHub Desktop.
Reading a file in Go using the os package
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
package main | |
import ( | |
"fmt" | |
"log" | |
"os" | |
) | |
func main() { | |
f, err := os.Open("sample.txt") | |
if err != nil { | |
log.Fatalf("%v", err) | |
} | |
defer f.Close() | |
stat, err := f.Stat() | |
if err != nil { | |
return | |
} | |
bs := make([]byte, stat.Size()) | |
fmt.Println("file size ", stat.Size()) | |
_, err = f.Read(bs) | |
if err != nil { | |
return | |
} | |
fmt.Println(string(bs)) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment