Readers
This commit is contained in:
parent
4b524656ed
commit
728c487b76
1 changed files with 32 additions and 0 deletions
32
3methods/readers.go
Normal file
32
3methods/readers.go
Normal file
|
@ -0,0 +1,32 @@
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"io"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
fmt.Println("the io package specifies io.Reader interface, which represents the end of a stream of data")
|
||||||
|
|
||||||
|
r := strings.NewReader("📩⍼ ꜜx Hello, Your Computer has virus .d")
|
||||||
|
// specifies new Reader
|
||||||
|
|
||||||
|
b := make([]byte, 4)
|
||||||
|
// think this like a bucket for temporary inputs
|
||||||
|
|
||||||
|
for {
|
||||||
|
n, err := r.Read(b)
|
||||||
|
// Read gets Reader (r) and gets next b byte in it
|
||||||
|
// returns n (number of bytes populated)
|
||||||
|
// and err (error, EOF for example)
|
||||||
|
|
||||||
|
fmt.Println(n, err, b)
|
||||||
|
fmt.Println(b[:n])
|
||||||
|
fmt.Printf("%s\n\n", b[:n])
|
||||||
|
|
||||||
|
if err == io.EOF {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue