WalzoneInterview Prep
📞 Interviewing soon? Practice with a realistic AI mock phone interview — it calls you, then scores you. First 15 min FREE →

Go · Advanced · question 53 of 100

How can you use ’sync.Pool’ and why might you use it?

📕 Buy this interview preparation book: 100 Go questions & answers — PDF + EPUB for $5

The ‘sync.Pool‘ is a standardized Go mechanism to implement a free list or a cache of allocated, ready-to-use objects to reduce the overhead of object allocation and garbage collection. It is often useful for memory-intensive or concurrent programs, where many instances of a certain object need to be created and destroyed frequently.

The primary use case for ‘sync.Pool‘ is to manage a collection of temporary objects that are expensive to create but can be reused safely. The pool helps to reduce the number of object allocations, and as a result, reduces the overhead of garbage collection and improves application performance.

To use ‘sync.Pool‘, you need to define a factory function that represents the constructor of the object type that you want the pool to manage. The factory function should return a new instance of the object when the pool is empty.

Here’s an example illustrating the usage of ‘sync.Pool‘. Let’s say you have an HTTP server that handles a large number of incoming requests, and for each request, you need to marshal JSON data with ‘bytes.Buffer‘.

import (
    "bytes"
    "sync"
)

var bufferPool sync.Pool

func init() {
    bufferPool.New = func() interface{} {
        return &bytes.Buffer{}
    }
}

While handling requests, you can use ‘bufferPool.Get()‘ to obtain a ‘*bytes.Buffer‘ object, and ‘bufferPool.Put(buf)‘ to return the object to the pool when you’re done using it. This reduces the number of memory allocations and garbage collections.

func handleRequest(payload []byte) {
    buf := bufferPool.Get().(*bytes.Buffer)
    defer bufferPool.Put(buf)
    
    buf.Reset()
    buf.Write(payload)
    // ... process payload
}

Please note that objects obtained from the ‘sync.Pool‘ should be used and released by the same goroutine, otherwise, there might be race conditions and potential data corruption.

Using ‘sync.Pool‘ is not always beneficial, and it’s important to use it judiciously. In cases where object allocation is infrequent or inexpensive in terms of time and resources, the overhead of managing a pool may not be worth it. Additionally, since ‘sync.Pool‘ can hold onto an object indefinitely, it can cause a memory leak if the object contains pointers to other large data structures.

Reading is step one. Saying it out loud is the interview. Our AI interviewer calls your phone and runs a realistic Go interview — then scores it.
📞 Practice Go — free 15 min
📕 Buy this interview preparation book: 100 Go questions & answers — PDF + EPUB for $5

All 100 Go questions · All topics