package main
import (
"fmt"
)
func Read(buff []byte) (n int, err error) {
temp := []byte("haha")
buff = temp
return 4, nil
}
func main() {
buff := make([]byte, 5)
n, _ := Read(buff)
fmt.Println(string(buff[:n]), n)
}
为什么调用Read后buff不能返回值呢,求解。
有疑问加站长微信联系(非本文作者)

得这样:
你在 Read 中让 参数buff 引用了新建的 []byte('haha'),而原来的并没有改变
这一句
buff = temp
不是值拷贝。