github.com/code-reading/golang@v0.0.0-20220303082512-ba5bc0e589a3/coding/sync/00-pool-demo.go (about)

     1  package main
     2  
     3  import (
     4  	"fmt"
     5  	"sync"
     6  )
     7  
     8  type Info struct {
     9  	Age int
    10  }
    11  
    12  func main() {
    13  	pool := sync.Pool{
    14  		New: func() interface{} {
    15  			return &Info{
    16  				Age: 1,
    17  			}
    18  		},
    19  	}
    20  	infoObject := pool.Get().(*Info)
    21  	fmt.Println(infoObject.Age) // print 1
    22  	pool.Put(infoObject)
    23  }