Golang简单的对象池

版权声明:版权声明:本文为邵聪聪(聪少)原创文章,请随意转载并注明出处。 https://blog.csdn.net/cto_scc/article/details/53391536

Golang简单的对象池

  • 复用的好处
    • 减少gc压力
    • 减少不必要的内存分配

import (
    "fmt"
    "sync"
)

var bufPool sync.Pool

type buf struct {
    b []byte
}

func main() {
    for {
        var bf *buf
        // 从池中取数据
        v := bufPool.Get()
        if v == nil {
            //若不存在buf,创建新的
            fmt.Println("no buf ,create!")
            bf = &buf{
                b: make([]byte, 10),
            }
        } else {
            // 池里存在buf,v这里是interface{},需要做类型转换
            bf = v.(*buf)
        }
        fmt.Println("使用数据", bf)
        // bf使命完成,放入池中
        bufPool.Put(bf)
    }
}

猜你喜欢

转载自blog.csdn.net/cto_scc/article/details/53391536