Skip to content

Instantly share code, notes, and snippets.

@johnwesonga
Created August 22, 2013 00:20
Show Gist options
  • Save johnwesonga/6301861 to your computer and use it in GitHub Desktop.
Save johnwesonga/6301861 to your computer and use it in GitHub Desktop.
Reading a file in Go using the os package
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