Skip to content

Instantly share code, notes, and snippets.

@pyk
Created March 12, 2015 15:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pyk/61779fd8036795a21435 to your computer and use it in GitHub Desktop.
Save pyk/61779fd8036795a21435 to your computer and use it in GitHub Desktop.
Read data from a file using go language. Read more here https://golang.kertaskampus.com/How-To-Read-Data-From-A-File/
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
tempor incididunt ut labore et dolore magna aliqua.
package main
import (
"fmt"
"log"
"os"
)
func main() {
// open input.txt file using read only flag and 0777 permission
file, err := os.OpenFile("input.txt", os.O_RDONLY, os.ModePerm)
if err != nil {
log.Fatal(err)
}
// initialize []byte
data := make([]byte, 100)
// read data from a file
_, err = file.Read(data)
if err != nil {
log.Fatal(err)
}
// print data to os.Stdout
fmt.Fprintf(os.Stdout, "%q\n", data[:])
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment