Fix reading-files example to not show out-of-bounds bytes

Fixes #165
This commit is contained in:
Eli Bendersky
2019-06-04 16:08:22 -07:00
parent d1886245ba
commit f4729c8ed2
4 changed files with 13 additions and 23 deletions

View File

@@ -40,7 +40,7 @@ func main() {
b1 := make([]byte, 5)
n1, err := f.Read(b1)
check(err)
fmt.Printf("%d bytes: %s\n", n1, string(b1))
fmt.Printf("%d bytes: %s\n", n1, string(b1[:n1]))
// You can also `Seek` to a known location in the file
// and `Read` from there.
@@ -49,7 +49,8 @@ func main() {
b2 := make([]byte, 2)
n2, err := f.Read(b2)
check(err)
fmt.Printf("%d bytes @ %d: %s\n", n2, o2, string(b2))
fmt.Printf("%d bytes @ %d: ", n2, o2)
fmt.Printf("%v\n", string(b2[:n2]))
// The `io` package provides some functions that may
// be helpful for file reading. For example, reads
@@ -80,5 +81,4 @@ func main() {
// be scheduled immediately after `Open`ing with
// `defer`).
f.Close()
}

View File

@@ -1,2 +1,2 @@
2aa7a2e248065cebfa6f8eece3234b5ffa710273
GQgp-I3dLb2
463a6f2999a023887af6b23c8f79f24978eb8115
cocJ6kBH_iZ

View File

@@ -1,6 +1,6 @@
$ echo "hello" > /tmp/dat
$ echo "go" >> /tmp/dat
$ go run reading-files.go
$ go run reading-files.go
hello
go
5 bytes: hello