Mastering-Go-Second-Edition/ch08/bytes.go
2019-01-10 22:09:57 +02:00

34 lines
539 B
Go

package main
import (
"bytes"
"fmt"
"io"
"os"
)
func main() {
var buffer bytes.Buffer
buffer.Write([]byte("This is"))
fmt.Fprintf(&buffer, " a string!\n")
buffer.WriteTo(os.Stdout)
buffer.WriteTo(os.Stdout)
buffer.Reset()
buffer.Write([]byte("Mastering Go!"))
r := bytes.NewReader([]byte(buffer.String()))
fmt.Println(buffer.String())
for {
b := make([]byte, 3)
n, err := r.Read(b)
if err == io.EOF {
break
}
if err != nil {
fmt.Println(err)
continue
}
fmt.Printf("Read %s Bytes: %d\n", b, n)
}
}