...
1
2
3
4
5
6
7 package intern
8
9 import "sync"
10
11 var (
12 pool sync.Pool = sync.Pool{
13 New: func() interface{} {
14 return make(map[string]string)
15 },
16 }
17 )
18
19
20 func String(s string) string {
21 m := pool.Get().(map[string]string)
22 c, ok := m[s]
23 if ok {
24 pool.Put(m)
25 return c
26 }
27 m[s] = s
28 pool.Put(m)
29 return s
30 }
31
32
33 func Bytes(b []byte) string {
34 m := pool.Get().(map[string]string)
35 c, ok := m[string(b)]
36 if ok {
37 pool.Put(m)
38 return c
39 }
40 s := string(b)
41 m[s] = s
42 pool.Put(m)
43 return s
44 }
45
View as plain text