【发布时间】:2021-06-29 06:59:42
【问题描述】:
// reflect/value.go
func ValueOf(i interface{}) Value {
if i == nil {
return Value{}
}
// TODO: Maybe allow contents of a Value to live on the stack.
// For now we make the contents always escape to the heap. It
// makes life easier in a few places (see chanrecv/mapassign
// comment below).
escapes(i)
上面的代码是golang中Value.go的源码,escapes(i)上面的注释说明每次调用ValueOf函数,i都会逃到堆里,这是为什么呢?即,如何解释在一些地方让生活更轻松?
【问题讨论】:
-
(see chanrecv/mapassign comment below)他们说什么? -
@tkausl
// Note: some of the noescape annotations below are technically a lie, // but safe in the context of this package. Functions like chansend // and mapassign don't escape the referent, but may escape anything // the referent points to (they do shallow copies of the referent). // It is safe in this package because the referent may only point // to something a Value may point to, and that is always in the heap // (due to the escapes() call in ValueOf). -
@tkausl 我不知道
chanrecv/mapassign的评论的含义
标签: go reflection implementation