github.com/jxskiss/gopkg@v0.17.3/ptr/copy.go (about) 1 package ptr 2 3 import ( 4 "reflect" 5 "time" 6 ) 7 8 // CopyBool returns a copy pointer of value *p. 9 // It returns nil if p is nil. 10 func CopyBool(p *bool) *bool { 11 if p == nil { 12 return nil 13 } 14 r := *p 15 return &r 16 } 17 18 // CopyString returns a copy pointer of value *p. 19 // It returns nil if p is nil. 20 func CopyString(p *string) *string { 21 if p == nil { 22 return nil 23 } 24 r := *p 25 return &r 26 } 27 28 // CopyInt returns a copy pointer of value *p. 29 // It returns nil if p is nil. 30 func CopyInt(p *int) *int { 31 if p == nil { 32 return nil 33 } 34 r := *p 35 return &r 36 } 37 38 // CopyInt8 returns a copy pointer of value *p. 39 // It returns nil if p is nil. 40 func CopyInt8(p *int8) *int8 { 41 if p == nil { 42 return nil 43 } 44 r := *p 45 return &r 46 } 47 48 // CopyInt16 returns a copy pointer of value *p. 49 // It returns nil if p is nil. 50 func CopyInt16(p *int16) *int16 { 51 if p == nil { 52 return nil 53 } 54 r := *p 55 return &r 56 } 57 58 // CopyInt32 returns a copy pointer of value *p. 59 // It returns nil if p is nil. 60 func CopyInt32(p *int32) *int32 { 61 if p == nil { 62 return nil 63 } 64 r := *p 65 return &r 66 } 67 68 // CopyInt64 returns a copy pointer of value *p. 69 // It returns nil if p is nil. 70 func CopyInt64(p *int64) *int64 { 71 if p == nil { 72 return nil 73 } 74 r := *p 75 return &r 76 } 77 78 // CopyUint returns a copy pointer of value *p. 79 // It returns nil if p is nil. 80 func CopyUint(p *uint) *uint { 81 if p == nil { 82 return nil 83 } 84 r := *p 85 return &r 86 } 87 88 // CopyUint8 returns a copy pointer of value *p. 89 // It returns nil if p is nil. 90 func CopyUint8(p *uint8) *uint8 { 91 if p == nil { 92 return nil 93 } 94 r := *p 95 return &r 96 } 97 98 // CopyUint16 returns a copy pointer of value *p. 99 // It returns nil if p is nil. 100 func CopyUint16(p *uint16) *uint16 { 101 if p == nil { 102 return nil 103 } 104 r := *p 105 return &r 106 } 107 108 // CopyUint32 returns a copy pointer of value *p. 109 // It returns nil if p is nil. 110 func CopyUint32(p *uint32) *uint32 { 111 if p == nil { 112 return nil 113 } 114 r := *p 115 return &r 116 } 117 118 // CopyUint64 returns a copy pointer of value *p. 119 // It returns nil if p is nil. 120 func CopyUint64(p *uint64) *uint64 { 121 if p == nil { 122 return nil 123 } 124 r := *p 125 return &r 126 } 127 128 // CopyFloat32 returns a copy pointer of value *p. 129 // It returns nil if p is nil. 130 func CopyFloat32(p *float32) *float32 { 131 if p == nil { 132 return nil 133 } 134 r := *p 135 return &r 136 } 137 138 // CopyFloat64 returns a copy pointer of value *p. 139 // It returns nil if p is nil. 140 func CopyFloat64(p *float64) *float64 { 141 if p == nil { 142 return nil 143 } 144 r := *p 145 return &r 146 } 147 148 // CopyTime returns a copy pointer of value *p. 149 // It returns nil if p is nil. 150 func CopyTime(p *time.Time) *time.Time { 151 if p == nil { 152 return nil 153 } 154 r := *p 155 return &r 156 } 157 158 // CopyDuration returns a copy pointer of value *p. 159 // It returns nil if p is nil. 160 func CopyDuration(p *time.Duration) *time.Duration { 161 if p == nil { 162 return nil 163 } 164 r := *p 165 return &r 166 } 167 168 // CopyAny return a shallow copy of the given pointer or value of any type. 169 // It always returns a pointer if v is not nil. 170 // If v is not a pointer or value of simple types, it may panic. 171 // 172 // If v is nil, it returns a nil interface{}. 173 // But note that if v is a nil pointer (not a nil interface{}), 174 // it returns a nil pointer of the same type. 175 func CopyAny(v interface{}) interface{} { 176 if v == nil { 177 return nil 178 } 179 180 val := reflect.ValueOf(v) 181 typ := val.Type() 182 isNil := false 183 if typ.Kind() == reflect.Ptr { 184 typ = typ.Elem() 185 isNil = val.IsNil() 186 } 187 if isNil { 188 p := reflect.New(reflect.PtrTo(typ)) 189 return p.Elem().Interface() 190 } 191 p := reflect.New(typ) 192 p.Elem().Set(reflect.Indirect(val)) 193 return p.Interface() 194 }