douwei8096 2014-11-18 14:14
浏览 11
已采纳

使用带有匿名功能的标签

There's a New function inside a Pool struct in the source code of the sync package that's defined like this

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{}
}

At line 103 of a golang stack trace package created by Facebook, there's an anonymous function defined inside a sync pool struct like this with the New: label:

var pcsPool = sync.Pool{
    New: func() interface{} {
        return make([]uintptr, maxStackSize)
    },
}

So, going from the comments in the source code, I'm assuming that the Facebook package has "specified a function to generate a value when Get would otherwise return nil," but why might it be defined with New: like this?

  • 写回答

1条回答 默认 最新

  • douzhao4071 2014-11-18 14:38
    关注

    New isn't a label, it's a field name in a 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
    }
    

    It's no different than N in the following example

    type T struct {
        N int
    }
    
    t := T{
        N: 1,
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 算法题:数的划分,用记忆化DFS做WA求调
  • ¥15 chatglm-6b应用到django项目中,模型加载失败
  • ¥15 武汉岩海低应变分析软件,导数据库里不显示波形图
  • ¥15 CreateBitmapFromWicBitmap内存释放问题。
  • ¥30 win c++ socket
  • ¥30 CanMv K210开发板实现功能
  • ¥15 C# datagridview 栏位进度
  • ¥15 vue3页面el-table页面数据过多
  • ¥100 vue3中融入gRPC-web
  • ¥15 kali环境运行volatility分析android内存文件,缺profile