-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.go
44 lines (33 loc) · 776 Bytes
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
/*
In this example, a goroutine is started to recycle memory buffers. The give
channel receives old memory buffers and stores them in a list. While the get
channel dispenses these buffers for use. If no buffers are available in the
list, a new one is created.
*/
package main
import "container/list"
func main() {
give := make(chan []byte)
get := make(chan []byte)
go func() {
q := new(list.List)
for {
if q.Len() == 0 {
q.PushFront(make([]byte, 100))
}
e := q.Front()
select {
case s := <-give:
q.PushFront(s)
case get <- e.Value.([]byte):
q.Remove(e)
}
}
}()
// Gets a new buffer from the recycler.
buffer := <-get
// Give it back to the recycler.
give <- buffer
// Get the recycled buffer again.
buffer = <-get
}