Use sync.Pool to cache frequently allocated and freed objects, reducing garbage collection pressure by reusing instances instead of creating new ones. Define a sync.Pool variable and implement a New function that creates the object, then use Get() to retrieve it and Put() to return it after use.
var myPool = sync.Pool{
New: func() interface{} {
return &MyStruct{}
},
}
func process() {
obj := myPool.Get().(*MyStruct)
// use obj
myPool.Put(obj)
}