简体   繁体   中英

using a label with anonymous function

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM