This is a creation in Article, where the information may have evolved or changed.
Add a new feature in the Sync Pack for Go 1.3: Pool.
Official documents can be seen here http://golang.org/pkg/sync/#Pool
The purpose of this class of design is to save and reuse temporary objects to reduce memory allocations and reduce CG pressure.
Type Pool
Func (P *pool) Get () interface{} func (P *pool) Put (x interface{}) New func () interface{}
Get returns any object in the pool.
If the pool is empty, call new to return a newly created object.
If new is not set, nil is returned.
Another important feature is that the objects that are placed in the pool will be recycled when you don't know when to go.
So if you put in 100 objects in advance, it is possible to find the pool is empty the next time you get it.
But one of the benefits of this feature is that you don't have to worry about the pool growing because go has helped you recycle the pool.
This cleanup process is done before each garbage collection. Garbage collection is triggered at a fixed two-minute time.
And every cleanup will clean up all the objects in the pool!
Package Main
Import
"Sync" "Log"
)
Func Main () {
Build object var pipe = &sync. Pool{new:func () Interface{}{return "hello,beijing"}}//ready to put the string val: = "hello,world!" into the pipe. Put (val)//Remove log. PRINTLN (pipe. Get ())//re-fetch is not, will automatically call NEWLOG.PRINTLN (pipe. Get ())
}
Output
2014/09/30 15:43:30 hello,world!
2014/09/30 15:43:30 hello,beijing