How to Use sync.Pool in Go for Object Reuse

Use sync.Pool to cache and reuse objects, reducing allocation overhead and garbage collection frequency.

Use sync.Pool to store reusable objects in a pool, retrieving them with Get() and returning them with Put() after use to reduce garbage collection pressure.

var pool = sync.Pool{
	New: func() any {
		return &MyObject{}
	},
}

// Get an object
obj := pool.Get().(*MyObject)

// Use the object
obj.DoWork()

// Return to pool
pool.Put(obj)