From 728c487b76930323168bc7a6ceaafbf2d16f3808 Mon Sep 17 00:00:00 2001 From: asandikci Date: Mon, 31 Jul 2023 10:05:18 +0300 Subject: [PATCH] Readers --- 3methods/readers.go | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 3methods/readers.go diff --git a/3methods/readers.go b/3methods/readers.go new file mode 100644 index 0000000..714e405 --- /dev/null +++ b/3methods/readers.go @@ -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 + } + } +}