Skip to content

Instantly share code, notes, and snippets.

@hyuki
Created Apr 9, 2018
Embed
What would you like to do?
os.Open
package main
import (
"fmt"
"os"
"log"
)
func main() {
file, err := os.Open("sample.txt")
if err != nil {
log.Fatal(err)
}
data := make([]byte, 100)
count, err := file.Read(data)
if err != nil {
log.Fatal(err)
}
fmt.Printf("read %d bytes: %q\n", count, data[:count])
err = file.Close()
if err != nil {
log.Fatal(err)
}
}
Hello, world!
Hello, world!
Hello, world!
Hello, world!
Hello, world!
Hello, world!
Hello, world!
Hello, world!
@hyuki
Copy link
Author

hyuki commented Apr 9, 2018

 $ go run a.go
read 100 bytes: "Hello, world!\nHello, world!\nHello, world!\nHello, world!\nHello, world!\nHello, world!\nHello, world!\nHe"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment