Skip to content

Commit f98ed80

Browse files
committed
Update buffer-read.md
1 parent 4609500 commit f98ed80

File tree

1 file changed

+19
-20
lines changed

1 file changed

+19
-20
lines changed

posts/buffered-read.md

+19-20
Original file line numberDiff line numberDiff line change
@@ -14,23 +14,22 @@ You can see `test.txt` contains `4` lines.
1414
(2) See the following program:
1515

1616
package main
17-
17+
1818
import (
1919
"bufio"
20-
"bytes"
2120
"fmt"
2221
"io"
23-
"io/ioutil"
2422
"log"
23+
"os"
2524
)
2625

2726
func main() {
28-
p, err := ioutil.ReadFile("test.txt")
27+
f, err := os.Open("test.txt")
2928
if err != nil {
3029
log.Fatal(err)
3130
}
3231

33-
r := bufio.NewReader(bytes.NewReader(p))
32+
r := bufio.NewReader(f)
3433
for {
3534
if s, err := r.ReadSlice('\n'); err == nil || err == io.EOF {
3635
fmt.Printf("%s", s)
@@ -44,16 +43,17 @@ You can see `test.txt` contains `4` lines.
4443
}
4544
}
4645

46+
4747
(a)
4848

49-
p, err := ioutil.ReadFile("test.txt")
50-
`ioutil.ReadFile("test.txt")` reads the whole content of `test.txt` into a slice: `p`.
49+
f, err := os.Open("test.txt")
50+
Open `test.txt` file.
5151

5252
(b)
5353

54-
r := bufio.NewReader(bytes.NewReader(p))
54+
r := bufio.NewReader(f)
5555

56-
`bufio.NewReader(bytes.NewReader(p))` creates a [bufio.Reader](https://golang.org/pkg/bufio/#Reader) struct which implements buffered read function.
56+
`bufio.NewReader(f)` creates a [bufio.Reader](https://golang.org/pkg/bufio/#Reader) struct which implements buffered read function.
5757

5858
(c)
5959

@@ -83,19 +83,18 @@ We can also use [bufio.Scanner](https://golang.org/pkg/bufio/#Scanner) to implem
8383

8484
import (
8585
"bufio"
86-
"bytes"
8786
"fmt"
88-
"io/ioutil"
8987
"log"
88+
"os"
9089
)
9190

9291
func main() {
93-
p, err := ioutil.ReadFile("test.txt")
92+
f, err := os.Open("test.txt")
9493
if err != nil {
9594
log.Fatal(err)
9695
}
9796

98-
s := bufio.NewScanner(bytes.NewReader(p))
97+
s := bufio.NewScanner(f)
9998

10099
for s.Scan() {
101100
fmt.Println(s.Text())
@@ -105,8 +104,8 @@ We can also use [bufio.Scanner](https://golang.org/pkg/bufio/#Scanner) to implem
105104

106105
(a)
107106

108-
s := bufio.NewScanner(bytes.NewReader(p))
109-
`bufio.NewScanner(bytes.NewReader(p))` creates a new [bufio.Scanner](https://golang.org/pkg/bufio/#Scanner) struct which splits the content by line by default.
107+
s := bufio.NewScanner(f)
108+
`bufio.NewScanner(f)` creates a new [bufio.Scanner](https://golang.org/pkg/bufio/#Scanner) struct which splits the content by line by default.
110109

111110
(b)
112111

@@ -118,22 +117,21 @@ We can also use [bufio.Scanner](https://golang.org/pkg/bufio/#Scanner) to implem
118117
We can also customize [SplitFunc](https://golang.org/pkg/bufio/#SplitFunc) function which doesn't separate content by line. Check the following code:
119118

120119
package main
121-
120+
122121
import (
123122
"bufio"
124-
"bytes"
125123
"fmt"
126-
"io/ioutil"
127124
"log"
125+
"os"
128126
)
129127

130128
func main() {
131-
p, err := ioutil.ReadFile("test.txt")
129+
f, err := os.Open("test.txt")
132130
if err != nil {
133131
log.Fatal(err)
134132
}
135133

136-
s := bufio.NewScanner(bytes.NewReader(p))
134+
s := bufio.NewScanner(f)
137135
split := func(data []byte, atEOF bool) (advance int, token []byte, err error) {
138136
for i := 0; i < len(data); i++ {
139137
if data[i] == 'h' {
@@ -148,6 +146,7 @@ We can also customize [SplitFunc](https://golang.org/pkg/bufio/#SplitFunc) funct
148146
fmt.Println(s.Text())
149147
}
150148
}
149+
151150
The `split` function separates the content by "`h`", and the running result is:
152151

153152
abcd

0 commit comments

Comments
 (0)