github.com/zhongdalu/gf@v1.0.0/g/container/gtype/bytes.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 "sync/atomic" 10 11 type Bytes struct { 12 value atomic.Value 13 } 14 15 // NewBytes returns a concurrent-safe object for []byte type, 16 // with given initial value <value>. 17 func NewBytes(value ...[]byte) *Bytes { 18 t := &Bytes{} 19 if len(value) > 0 { 20 t.value.Store(value[0]) 21 } 22 return t 23 } 24 25 // Clone clones and returns a new concurrent-safe object for []byte type. 26 func (v *Bytes) Clone() *Bytes { 27 return NewBytes(v.Val()) 28 } 29 30 // Set atomically stores <value> into t.value and returns the previous value of t.value. 31 // Note: The parameter <value> cannot be nil. 32 func (v *Bytes) Set(value []byte) (old []byte) { 33 old = v.Val() 34 v.value.Store(value) 35 return 36 } 37 38 // Val atomically loads t.value. 39 func (v *Bytes) Val() []byte { 40 if s := v.value.Load(); s != nil { 41 return s.([]byte) 42 } 43 return nil 44 }