Documentation
¶
Overview ¶
Package weak implement weak reference
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Pool ¶
type Pool struct { sync.Mutex New func(name string) interface{} // contains filtered or unexported fields }
Pool is a object pool which share object by name
type Strong ¶
type Strong interface { // Get the referenced data. it's always not nil, because Wrap(nil) is not allowed Get() interface{} // Get the corresponding weak reference Weak() Weak }
Strong reference
type Weak ¶
type Weak interface { // Get the referenced data, this function will return nil when all strong references are finalized. Get() interface{} // Get a strong reference Strong() (Strong, bool) }
Weak reference
Example ¶
s, w := Wrap(new(int)) // wrap data into strong reference _ = s.Weak() // get the weak reference from the strong one _ = s.Get().(*int) // get the referenced data from the strong reference s = nil // release the strong reference // a weak reference can be promoted to a strong one if s1, ok := w.Strong(); ok { fmt.Println("promoted:", s1.Get()) } // the weak reference may be released at anytime after all the strong references finalized. if x, ok := w.Get().(*int); ok { fmt.Println("*x=", *x) }
Output:
Click to show internal directories.
Click to hide internal directories.