mirror of
https://github.com/teivah/100-go-mistakes.git
synced 2026-06-21 00:47:11 +08:00
Fix sync.Pool example
This commit is contained in:
parent
cad4f47cbe
commit
3035221cb1
1 changed files with 10 additions and 6 deletions
|
|
@ -7,17 +7,21 @@ import (
|
|||
|
||||
var pool = sync.Pool{
|
||||
New: func() any {
|
||||
return make([]byte, 1024)
|
||||
b := make([]byte, 1024)
|
||||
return &b
|
||||
},
|
||||
}
|
||||
|
||||
func write(w io.Writer) {
|
||||
buffer := pool.Get().([]byte)
|
||||
buffer = buffer[:0]
|
||||
defer pool.Put(buffer)
|
||||
bPtr := pool.Get().(*[]byte)
|
||||
defer func() {
|
||||
*bPtr = (*bPtr)[:0]
|
||||
pool.Put(bPtr)
|
||||
}()
|
||||
|
||||
getResponse(buffer)
|
||||
_, _ = w.Write(buffer)
|
||||
b := *bPtr
|
||||
getResponse(b)
|
||||
_, _ = w.Write(b)
|
||||
}
|
||||
|
||||
func getResponse([]byte) {
|
||||
|
|
|
|||
Loading…
Reference in a new issue