github.com/zhongdalu/gf@v1.0.0/g/container/gtype/interface.go (about) 1 // Copyright 2018 gf Author(https://github.com/zhongdalu/gf). All Rights Reserved. 2 // 3 // This Source Code Form is subject to the terms of the MIT License. 4 // If a copy of the MIT was not distributed with this file, 5 // You can obtain one at https://github.com/zhongdalu/gf. 6 7 package gtype 8 9 import ( 10 "sync/atomic" 11 ) 12 13 type Interface struct { 14 value atomic.Value 15 } 16 17 // NewInterface returns a concurrent-safe object for interface{} type, 18 // with given initial value <value>. 19 func NewInterface(value ...interface{}) *Interface { 20 t := &Interface{} 21 if len(value) > 0 && value[0] != nil { 22 t.value.Store(value[0]) 23 } 24 return t 25 } 26 27 // Clone clones and returns a new concurrent-safe object for interface{} type. 28 func (v *Interface) Clone() *Interface { 29 return NewInterface(v.Val()) 30 } 31 32 // Set atomically stores <value> into t.value and returns the previous value of t.value. 33 // Note: The parameter <value> cannot be nil. 34 func (v *Interface) Set(value interface{}) (old interface{}) { 35 old = v.Val() 36 v.value.Store(value) 37 return 38 } 39 40 // Val atomically loads t.value. 41 func (v *Interface) Val() interface{} { 42 return v.value.Load() 43 }