|
@@ -0,0 +1,33 @@
|
|
|
+package test
|
|
|
+
|
|
|
+import (
|
|
|
+ "fmt"
|
|
|
+ "sync"
|
|
|
+ "testing"
|
|
|
+)
|
|
|
+
|
|
|
+func TestSync(t *testing.T) {
|
|
|
+
|
|
|
+ pool := sync.Pool{
|
|
|
+ New: func() interface{} {
|
|
|
+ fmt.Println("Creating a new instance.")
|
|
|
+ return "New Object"
|
|
|
+ },
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ obj1 := pool.Get().(string)
|
|
|
+ fmt.Println("Got:", obj1)
|
|
|
+
|
|
|
+
|
|
|
+ pool.Put("Reused Object")
|
|
|
+
|
|
|
+
|
|
|
+ obj2 := pool.Get().(string)
|
|
|
+ fmt.Println("Got:", obj2)
|
|
|
+
|
|
|
+
|
|
|
+ obj3 := pool.Get().(string)
|
|
|
+ fmt.Println("Got:", obj3)
|
|
|
+
|
|
|
+}
|