Created
March 12, 2015 15:49
-
-
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/
This file contains hidden or 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
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod | |
tempor incididunt ut labore et dolore magna aliqua. |
This file contains hidden or 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() { | |
// 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