包的源代碼中的Pool 結構中有一個 New 函數sync,其定義如下type Pool struct {local unsafe.Pointer // local fixed-size per-P pool, actual type is [P]poolLocallocalSize 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 堆棧跟蹤包的第 103 行,在同步池結構中定義了一個匿名函數,如下所示New::var pcsPool = sync.Pool{ New: func() interface{} { return make([]uintptr, maxStackSize) },}所以,從源代碼中的注釋來看,我假設 Facebook 包已經“指定了一個函數來生成一個值,當 Get 否則返回 nil 時”,但為什么它會被定義為New:這樣呢?
- 0 回答
- 0 關注
- 201 瀏覽
添加回答
舉報
0/150
提交
取消