Welcome to MLink Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
299 views
in Technique[技术] by (71.8m points)

请问这段代码中的每一行如何理解?

type handler struct {
    p sync.Pool
}

func newHandler() *handler {
    h := &handler{}
    h.p.New = func() interface{} {
        return &Context{}
    }

    return h
}

func main (){
    ctx := h.p.Get().(*Context)
    defer h.p.Put(ctx)
}

1.上面这段代码中 h := &handler{} 这一行的意思是不是生成一个 hander 对象并将地址赋值给 h?

2.代码中的 return &Context{} 是不是会生成一个 context 的对象并将这个对象的地址放到缓存池?

3.这个 Context 就是普通的 struct吗? 还是go 语言中的保留字符?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

已经找到答案....真是自问自答赚积分= - =。context 也是一个自己定义的struct ,并且go 中没法不声明就这样初始化一个context


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to MLink Developer Q&A Community for programmer and developer-Open, Learning and Share
...