// Created on savesnippets.com ยท https://savesnippets.com/KVs0IeHkZjvhYb package main import ( "bytes" "fmt" "sync" ) var bufPool = sync.Pool{ New: func() any { return new(bytes.Buffer) }, } func processRequest(payload []byte) string { buf := bufPool.Get().(*bytes.Buffer) defer func() { buf.Reset() // wipe before returning bufPool.Put(buf) }() buf.WriteString("processed: ") buf.Write(payload) return buf.String() } func main() { var wg sync.WaitGroup for i := 0; i < 10; i++ { wg.Add(1) go func(i int) { defer wg.Done() fmt.Println(processRequest([]byte(fmt.Sprintf("req-%d", i)))) }(i) } wg.Wait() }