func main() { readToSlice(strings.NewReader("abcdefghijklmn")) fmt.Printf("\n--------------------------------------\n\n") readToSubSlice(strings.NewReader("abcdefghijklmn")) } func readToSlice(r io.Reader) { b := make([]byte, 0, 20) fmt.Printf("%p, %v\n", b, b) fmt.Printf("%p, %v\n", b[len(b):cap(b)], b[len(b):cap(b)]) fmt.Printf("\n") n, err := r.Read(b) if err != nil { fmt.Println(err) } fmt.Printf("%p, %v\n", b, b) fmt.Printf("%p, %v\n", b[len(b):cap(b)], b[len(b):cap(b)]) fmt.Println(n) fmt.Printf("\n") b = b[:len(b)+n] fmt.Printf("%p, %v\n", b, b) } func readToSubSlice(r io.Reader) { b := make([]byte, 0, 20) fmt.Printf("%p, %v\n", b, b) fmt.Printf("%p, %v\n", b[len(b):cap(b)], b[len(b):cap(b)]) fmt.Printf("\n") n, err := r.Read(b[len(b):cap(b)]) if err != nil { fmt.Println(err) } fmt.Printf("%p, %v\n", b, b) fmt.Printf("%p, %v\n", b[len(b):cap(b)], b[len(b):cap(b)]) fmt.Println(n) fmt.Printf("\n") b = b[:len(b)+n] fmt.Printf("%p, %v\n", b, b) }
zzh@ZZHPC:/zdata/Github/ztest$ go run main.go 0xc00001a240, [] 0xc00001a240, [0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0] 0xc00001a240, [] 0xc00001a240, [0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0] 0 0xc00001a240, [] -------------------------------------- 0xc00001a258, [] 0xc00001a258, [0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0] 0xc00001a258, [] 0xc00001a258, [97 98 99 100 101 102 103 104 105 106 107 108 109 110 0 0 0 0 0 0] 14 0xc00001a258, [97 98 99 100 101 102 103 104 105 106 107 108 109 110]
标签:err,Read,fmt,cap,len,io,Printf,Go,0xc00001a240 From: https://www.cnblogs.com/zhangzhihui/p/18048375