func() interface{} 强制类型转换(结构)(接口)

package main

import (
	"sync"
	"fmt"
)

type handler struct {
	p sync.Pool
}

type context struct{
	A string
	B string
}
func (c *context) set(a,b string){
	c.A=a
	c.B=b
}

func main(){

	h:=handler{}
    //返回 interface{} ,interface 任意的数据,只要类型正确就可以强制转换 interface.(type)
	h.p.New= func() interface{} {
		return &context{}
	}

	s:=h.p.Get().(*context)//get获取数据,.(*context)类型强制转换
	s.set("A","B")

	fmt.Println(s.A,s.B)
}

 

转载于:https://my.oschina.net/u/3529405/blog/1613943