When you want to read bytes into a buffer, you should use this definetion
buf1 := make([]byte, 3)
instead of this
var buf2 []byte
because buf2 is nil
pointer to []byte
, data only can be appended to it.
package main
import (
"fmt"
"strings"
)
func main() {
var buf []byte
buf1 := make([]byte, 3)
fmt.Printf("%#v, %#v \n", buf, buf1)
r := strings.NewReader("abcde")
// Cauction: buf can not receive any bytes.
n, err := r.Read(buf)
fmt.Println(n, err)
// buf1 can receive bytes.
n, err = r.Read(buf1)
fmt.Println(n, err)
}
// []byte(nil), []byte{0x0, 0x0, 0x0}
// 0 <nil>
// 3 <nil>