在sync
包的source code中的Pool结构中有一个新函数,其定义如下
type Pool struct {
local unsafe.Pointer // local fixed-size per-P pool, actual type is [P]poolLocal
localSize uintptr // size of the local array
// New optionally specifies a function to generate
// a value when Get would otherwise return nil.
// It may not be changed concurrently with calls to Get.
New func() interface{}
}
在Facebook创建的golang stack trace package 的第103行,有一个匿名函数在同步池结构中定义,并带有New:
标签:
var pcsPool = sync.Pool{
New: func() interface{} {
return make([]uintptr, maxStackSize)
},
}
因此,从源代码中的注释开始,我假设Facebook包具有"指定了一个函数来生成一个值,否则Get将返回nil,"但为什么可能像New:
这样定义?
答案 0 :(得分:2)
New
不是标签,它是sync.Pool
type Pool struct {
// New optionally specifies a function to generate
// a value when Get would otherwise return nil.
// It may not be changed concurrently with calls to Get.
New func() interface{}
// contains filtered or unexported fields
}
与以下示例中的N
没有区别
type T struct {
N int
}
t := T{
N: 1,
}