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 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路
  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应
  • ¥15 matlab基于pde算法图像修复,为什么只能对示例图像有效
  • ¥100 连续两帧图像高速减法
  • ¥15 如何绘制动力学系统的相图