From f4729c8ed2ca4de899cce8aeacaa817f70c9e959 Mon Sep 17 00:00:00 2001 From: Eli Bendersky Date: Tue, 4 Jun 2019 16:08:22 -0700 Subject: [PATCH] Fix reading-files example to not show out-of-bounds bytes Fixes #165 --- examples/reading-files/reading-files.go | 6 +++--- examples/reading-files/reading-files.hash | 4 ++-- examples/reading-files/reading-files.sh | 2 +- public/reading-files | 24 +++++++---------------- 4 files changed, 13 insertions(+), 23 deletions(-) diff --git a/examples/reading-files/reading-files.go b/examples/reading-files/reading-files.go index bcff7fe..49387a6 100644 --- a/examples/reading-files/reading-files.go +++ b/examples/reading-files/reading-files.go @@ -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() - } diff --git a/examples/reading-files/reading-files.hash b/examples/reading-files/reading-files.hash index f687f38..8cf5bf6 100644 --- a/examples/reading-files/reading-files.hash +++ b/examples/reading-files/reading-files.hash @@ -1,2 +1,2 @@ -2aa7a2e248065cebfa6f8eece3234b5ffa710273 -GQgp-I3dLb2 +463a6f2999a023887af6b23c8f79f24978eb8115 +cocJ6kBH_iZ diff --git a/examples/reading-files/reading-files.sh b/examples/reading-files/reading-files.sh index b3ab74f..a1deb72 100644 --- a/examples/reading-files/reading-files.sh +++ b/examples/reading-files/reading-files.sh @@ -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 diff --git a/public/reading-files b/public/reading-files index 2e573cd..1b08728 100644 --- a/public/reading-files +++ b/public/reading-files @@ -29,7 +29,7 @@ reading files.

- +
package main
 
@@ -128,7 +128,7 @@ actually were read.

    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]))
 
@@ -147,7 +147,8 @@ and Read from there.

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])) @@ -214,22 +215,11 @@ reading methods it provides.

be scheduled immediately after Opening with defer).

- - - -
    f.Close()
-
- - - - - - - -
}
+            
    f.Close()
+}
 
@@ -247,7 +237,7 @@ be scheduled immediately after Opening with
$ echo "hello" > /tmp/dat
 $ echo "go" >>   /tmp/dat
-$ go run reading-files.go 
+$ go run reading-files.go
 hello
 go
 5 bytes: hello