推荐库一个类型转换库 https://github.com/spf13/cast

    1. package convert
    2. import "strconv"
    3. func StringPtr(value interface{}) *string {
    4. var res string
    5. switch val := value.(type) {
    6. case string:
    7. res = val
    8. case bool:
    9. if val {
    10. res = "true"
    11. } else {
    12. res = "false"
    13. }
    14. case int:
    15. res = strconv.FormatInt(int64(val), 10)
    16. case uint:
    17. res = strconv.FormatUint(uint64(val), 10)
    18. case int8:
    19. res = strconv.FormatInt(int64(val), 10)
    20. case uint8:
    21. res = strconv.FormatUint(uint64(val), 10)
    22. case int16:
    23. res = strconv.FormatInt(int64(val), 10)
    24. case uint16:
    25. res = strconv.FormatUint(uint64(val), 10)
    26. case int32:
    27. res = strconv.FormatInt(int64(val), 10)
    28. case uint32:
    29. res = strconv.FormatUint(uint64(val), 10)
    30. case int64:
    31. res = strconv.FormatInt(val, 10)
    32. case uint64:
    33. res = strconv.FormatUint(val, 10)
    34. case float32:
    35. res = strconv.FormatFloat(float64(val), 'f', -1, 64)
    36. case float64:
    37. res = strconv.FormatFloat(val, 'f', -1, 64)
    38. case []byte:
    39. res = string(val)
    40. default:
    41. return nil
    42. }
    43. return &res
    44. }
    45. func BoolPtr(value interface{}) *bool {
    46. var res bool
    47. switch val := value.(type) {
    48. case bool:
    49. res = val
    50. case int, uint, int8, uint8, int16, uint16, int32, uint32, int64, uint64:
    51. res = val != 0
    52. case float32:
    53. if val != 0 {
    54. res = true
    55. } else {
    56. res = false
    57. }
    58. case float64:
    59. if val != 0 {
    60. res = true
    61. } else {
    62. res = false
    63. }
    64. case string:
    65. switch val {
    66. case "1", "true", "TRUE", "True", "On":
    67. res = true
    68. case "null":
    69. return nil
    70. default:
    71. res = false
    72. }
    73. case []byte:
    74. switch string(val) {
    75. case "1", "true", "TRUE", "True", "On":
    76. res = true
    77. case "null":
    78. return nil
    79. default:
    80. res = false
    81. }
    82. default:
    83. return nil
    84. }
    85. return &res
    86. }
    87. // int
    88. const INT_MAX = int(^uint(0) >> 1)
    89. const INT_MIN = ^INT_MAX
    90. const INT_MAX_64 = int64(INT_MAX)
    91. const INT_MIN_64 = int64(INT_MIN)
    92. // uint
    93. const UINT_MIN uint = 0
    94. const UINT_MAX = ^uint(0)
    95. const UINT_MAX_64 = uint64(UINT_MAX)
    96. const UINT_MIN_64 = uint64(UINT_MIN)
    97. // int8 -127 ~ 128
    98. const INT8_MAX = int8(^uint8(0) >> 1)
    99. const INT8_MIN = ^INT8_MAX
    100. const INT8_MAX_64 = int64(INT8_MAX)
    101. const INT8_MIN_64 = int64(INT8_MIN)
    102. // uint8 0 ~ 255
    103. const UINT8_MAX = ^uint8(0)
    104. const UINT8_MIN = uint8(0)
    105. const UINT8_MAX_64 = uint64(UINT8_MAX)
    106. const UINT8_MIN_64 = uint64(UINT8_MIN)
    107. // int16 -32768 ~ 32767
    108. const INT16_MAX = int16(^uint16(0) >> 1)
    109. const INT16_MIN = ^INT16_MAX
    110. const INT16_MAX_64 = int64(INT16_MAX)
    111. const INT16_MIN_64 = int64(INT16_MIN)
    112. // uint16 0 ~ 65535
    113. const UINT16_MAX = ^uint16(0)
    114. const UINT16_MIN = uint16(0)
    115. const UINT16_MAX_64 = uint64(UINT16_MAX)
    116. const UINT16_MIN_64 = uint64(UINT16_MIN)
    117. // int32 -2147483648 ~ 2147483647
    118. const INT32_MAX = int32(^uint32(0) >> 1)
    119. const INT32_MIN = ^INT32_MAX
    120. const INT32_MAX_64 = int64(INT32_MAX)
    121. const INT32_MIN_64 = int64(INT32_MIN)
    122. // uint32 0 ~ 4294967295
    123. const UINT32_MAX = ^uint32(0)
    124. const UINT32_MIN = uint32(0)
    125. const UINT32_MAX_64 = uint64(UINT32_MAX)
    126. const UINT32_MIN_64 = uint64(UINT32_MIN)
    127. func IntPtr(value interface{}) *int {
    128. ptr := Int64Ptr(value)
    129. if ptr == nil || *ptr > INT_MAX_64 || *ptr < INT_MIN_64 {
    130. return nil
    131. }
    132. val := int(*ptr)
    133. return &val
    134. }
    135. func UintPtr(value interface{}) *uint {
    136. ptr := Uint64Ptr(value)
    137. if ptr == nil || *ptr > UINT_MAX_64 || *ptr < UINT_MIN_64 {
    138. return nil
    139. }
    140. val := uint(*ptr)
    141. return &val
    142. }
    143. func Int8Ptr(value interface{}) *int8 {
    144. ptr := Int64Ptr(value)
    145. if ptr == nil || *ptr > INT8_MAX_64 || *ptr < INT8_MIN_64 {
    146. return nil
    147. }
    148. val := int8(*ptr)
    149. return &val
    150. }
    151. func Uint8Ptr(value interface{}) *uint8 {
    152. ptr := Uint64Ptr(value)
    153. if ptr == nil || *ptr > UINT8_MAX_64 || *ptr < UINT8_MIN_64 {
    154. return nil
    155. }
    156. val := uint8(*ptr)
    157. return &val
    158. }
    159. func Int16Ptr(value interface{}) *int16 {
    160. ptr := Int64Ptr(value)
    161. if ptr == nil || *ptr > INT16_MAX_64 || *ptr < INT16_MIN_64 {
    162. return nil
    163. }
    164. val := int16(*ptr)
    165. return &val
    166. }
    167. func Uint16Ptr(value interface{}) *uint16 {
    168. ptr := Uint64Ptr(value)
    169. if ptr == nil || *ptr > UINT16_MAX_64 || *ptr < UINT16_MIN_64 {
    170. return nil
    171. }
    172. val := uint16(*ptr)
    173. return &val
    174. }
    175. func Int32Ptr(value interface{}) *int32 {
    176. ptr := Int64Ptr(value)
    177. if ptr == nil || *ptr > INT32_MAX_64 || *ptr < INT32_MIN_64 {
    178. return nil
    179. }
    180. val := int32(*ptr)
    181. return &val
    182. }
    183. func Uint32Ptr(value interface{}) *uint32 {
    184. ptr := Uint64Ptr(value)
    185. if ptr == nil || *ptr > UINT32_MAX_64 || *ptr < UINT32_MIN_64 {
    186. return nil
    187. }
    188. val := uint32(*ptr)
    189. return &val
    190. }
    191. func Int64Ptr(value interface{}) *int64 {
    192. var res int64
    193. switch val := value.(type) {
    194. case bool:
    195. if val {
    196. res = 1
    197. } else {
    198. res = 0
    199. }
    200. case int:
    201. res = int64(val)
    202. case uint:
    203. res = int64(val)
    204. case int8:
    205. res = int64(val)
    206. case uint8:
    207. res = int64(val)
    208. case int16:
    209. res = int64(val)
    210. case uint16:
    211. res = int64(val)
    212. case int32:
    213. res = int64(val)
    214. case uint32:
    215. res = int64(val)
    216. case int64:
    217. res = val
    218. case uint64:
    219. res = int64(val)
    220. case float32:
    221. res = int64(val)
    222. case float64:
    223. res = int64(val)
    224. case []byte:
    225. if ret, err := strconv.ParseInt(string(val), 10, 0); err == nil {
    226. res = ret
    227. } else {
    228. return nil
    229. }
    230. case string:
    231. if ret, err := strconv.ParseInt(val, 10, 0); err == nil {
    232. res = ret
    233. } else {
    234. return nil
    235. }
    236. default:
    237. return nil
    238. }
    239. return &res
    240. }
    241. func Uint64Ptr(value interface{}) *uint64 {
    242. switch val := value.(type) {
    243. case uint64:
    244. res := val
    245. return &res
    246. case []byte:
    247. if ret, err := strconv.ParseUint(string(val), 10, 0); err == nil {
    248. return &ret
    249. }
    250. case string:
    251. if ret, err := strconv.ParseUint(val, 10, 0); err == nil {
    252. return &ret
    253. }
    254. default:
    255. intVal := Int64Ptr(value)
    256. if intVal != nil {
    257. res := uint64(*intVal)
    258. return &res
    259. }
    260. }
    261. return nil
    262. }
    263. func Float32Ptr(value interface{}) *float32 {
    264. ptr := Float64Ptr(value)
    265. if ptr == nil {
    266. return nil
    267. }
    268. val := float32(*ptr)
    269. return &val
    270. }
    271. func Float64Ptr(value interface{}) *float64 {
    272. var res float64
    273. switch val := value.(type) {
    274. case bool:
    275. if val {
    276. res = 1
    277. } else {
    278. res = 0
    279. }
    280. case int:
    281. res = float64(val)
    282. case uint:
    283. res = float64(val)
    284. case int8:
    285. res = float64(val)
    286. case uint8:
    287. res = float64(val)
    288. case int16:
    289. res = float64(val)
    290. case uint16:
    291. res = float64(val)
    292. case int32:
    293. res = float64(val)
    294. case uint32:
    295. res = float64(val)
    296. case int64:
    297. res = float64(val)
    298. case uint64:
    299. res = float64(val)
    300. case float32:
    301. res = float64(val)
    302. case float64:
    303. res = val
    304. case []byte:
    305. if ret, err := strconv.ParseFloat(string(val), 10); err == nil {
    306. res = ret
    307. } else {
    308. return nil
    309. }
    310. case string:
    311. if ret, err := strconv.ParseFloat(val, 10); err == nil {
    312. res = ret
    313. } else {
    314. return nil
    315. }
    316. default:
    317. return nil
    318. }
    319. return &res
    320. }
    321. func StringVal(value interface{}) (res string) {
    322. if ptr := StringPtr(value); ptr != nil {
    323. res = *ptr
    324. }
    325. return
    326. }
    327. func BoolVal(value interface{}) (res bool) {
    328. if ptr := BoolPtr(value); ptr != nil {
    329. res = *ptr
    330. }
    331. return
    332. }
    333. func IntVal(value interface{}) (res int) {
    334. if ptr := IntPtr(value); ptr != nil {
    335. res = *ptr
    336. }
    337. return
    338. }
    339. func UintVal(value interface{}) (res uint) {
    340. if ptr := UintPtr(value); ptr != nil {
    341. res = *ptr
    342. }
    343. return
    344. }
    345. func Int8Val(value interface{}) (res int8) {
    346. if ptr := Int8Ptr(value); ptr != nil {
    347. res = *ptr
    348. }
    349. return
    350. }
    351. func Uint8Val(value interface{}) (res uint8) {
    352. if ptr := Uint8Ptr(value); ptr != nil {
    353. res = *ptr
    354. }
    355. return
    356. }
    357. func Int16Val(value interface{}) (res int16) {
    358. if ptr := Int16Ptr(value); ptr != nil {
    359. res = *ptr
    360. }
    361. return
    362. }
    363. func Uint16Val(value interface{}) (res uint16) {
    364. if ptr := Uint16Ptr(value); ptr != nil {
    365. res = *ptr
    366. }
    367. return
    368. }
    369. func Int32Val(value interface{}) (res int32) {
    370. if ptr := Int32Ptr(value); ptr != nil {
    371. res = *ptr
    372. }
    373. return
    374. }
    375. func Uint32Val(value interface{}) (res uint32) {
    376. if ptr := Uint32Ptr(value); ptr != nil {
    377. res = *ptr
    378. }
    379. return
    380. }
    381. func Int64Val(value interface{}) (res int64) {
    382. if ptr := Int64Ptr(value); ptr != nil {
    383. res = *ptr
    384. }
    385. return
    386. }
    387. func Uint64Val(value interface{}) (res uint64) {
    388. if ptr := Uint64Ptr(value); ptr != nil {
    389. res = *ptr
    390. }
    391. return
    392. }
    393. func Float32Val(value interface{}) (res float32) {
    394. if ptr := Float32Ptr(value); ptr != nil {
    395. res = *ptr
    396. }
    397. return
    398. }
    399. func Float64Val(value interface{}) (res float64) {
    400. if ptr := Float64Ptr(value); ptr != nil {
    401. res = *ptr
    402. }
    403. return
    404. }
    1. package convert
    2. import (
    3. "github.com/a8m/expect"
    4. "testing"
    5. )
    6. func TestString(t *testing.T) {
    7. expect := expect.New(t)
    8. expect(*StringPtr("str")).To.Equal("str")
    9. expect(*StringPtr(`str`)).To.Equal("str")
    10. expect(*StringPtr([]byte(`str`))).To.Equal("str")
    11. expect(*StringPtr(-1)).To.Equal("-1")
    12. expect(*StringPtr(2)).To.Equal("2")
    13. expect(*StringPtr(3.14)).To.Equal("3.14")
    14. expect(*StringPtr(3888.1455555)).To.Equal("3888.1455555")
    15. expect(*StringPtr(38881455555.555)).To.Equal("38881455555.555")
    16. expect(*StringPtr(true)).To.Equal("true")
    17. expect(*StringPtr(false)).To.Equal("false")
    18. expect(nil == StringPtr(nil)).To.Equal(true)
    19. }
    20. func TestBool(t *testing.T) {
    21. expect := expect.New(t)
    22. expect(*BoolPtr("true")).To.Equal(true)
    23. expect(*BoolPtr("false")).To.Equal(false)
    24. expect(nil == BoolPtr("null")).To.Equal(true)
    25. expect(*BoolPtr("x")).To.Equal(false)
    26. expect(*BoolPtr(123)).To.Equal(true)
    27. expect(*BoolPtr(123.4)).To.Equal(true)
    28. expect(*BoolPtr(0)).To.Equal(false)
    29. expect(*BoolPtr(0.0000000000000000000001)).To.Equal(true)
    30. expect(*BoolPtr(0.0000000000000000000000)).To.Equal(false)
    31. expect(nil == BoolPtr(nil)).To.Equal(true)
    32. }
    33. func TestInt(t *testing.T) {
    34. expect := expect.New(t)
    35. expect(nil == Int64Ptr("str")).To.Equal(true)
    36. expect(nil == Int64Ptr("123.4")).To.Equal(true)
    37. expect(nil == Uint64Ptr("123.4")).To.Equal(true)
    38. expect(nil == Uint64Ptr("str")).To.Equal(true)
    39. expect(-123 == *Int64Ptr("-123")).To.Equal(true)
    40. expect(123 == *Int64Ptr("0123")).To.Equal(true)
    41. expect(123456798 == *Int64Ptr("123456798")).To.Equal(true)
    42. expect(nil == Int8Ptr("123456")).To.Equal(true)
    43. expect(nil == Int8Ptr(123456)).To.Equal(true)
    44. expect(-1 == *Int8Ptr("-1")).To.Equal(true)
    45. expect(0 == *Int8Ptr("0")).To.Equal(true)
    46. expect(1 == *Int8Ptr("1")).To.Equal(true)
    47. expect(nil == Int8Ptr(-123456)).To.Equal(true)
    48. expect(nil == Int8Ptr("-123456")).To.Equal(true)
    49. expect(nil == Uint8Ptr("123456")).To.Equal(true)
    50. expect(0 == *Uint8Ptr("0")).To.Equal(true)
    51. expect(1 == *Uint8Ptr("1")).To.Equal(true)
    52. expect(nil == Uint8Ptr("-1")).To.Equal(true)
    53. expect(nil == Int16Ptr("123456798")).To.Equal(true)
    54. expect(nil == Int16Ptr(123456789)).To.Equal(true)
    55. expect(-1 == *Int16Ptr("-1")).To.Equal(true)
    56. expect(0 == *Int16Ptr("0")).To.Equal(true)
    57. expect(1 == *Int16Ptr("1")).To.Equal(true)
    58. expect(nil == Int16Ptr(-123456789)).To.Equal(true)
    59. expect(nil == Int16Ptr("-123456789")).To.Equal(true)
    60. expect(nil == Uint16Ptr("123456789")).To.Equal(true)
    61. expect(0 == *Uint16Ptr("0")).To.Equal(true)
    62. expect(1 == *Uint16Ptr("1")).To.Equal(true)
    63. expect(nil == Uint16Ptr("-1")).To.Equal(true)
    64. expect(nil == Int32Ptr("1234567890123456")).To.Equal(true)
    65. expect(nil == Int32Ptr(1234567890123456)).To.Equal(true)
    66. expect(-1 == *Int32Ptr("-1")).To.Equal(true)
    67. expect(0 == *Int32Ptr("0")).To.Equal(true)
    68. expect(1 == *Int32Ptr("1")).To.Equal(true)
    69. expect(nil == Int32Ptr(-1234567890123456)).To.Equal(true)
    70. expect(nil == Int32Ptr("-1234567890123456")).To.Equal(true)
    71. expect(nil == Uint32Ptr("1234567890123456")).To.Equal(true)
    72. expect(0 == *Uint32Ptr("0")).To.Equal(true)
    73. expect(1 == *Uint32Ptr("1")).To.Equal(true)
    74. expect(nil == Uint32Ptr("-1")).To.Equal(true)
    75. }
    76. func TestFloat(t *testing.T) {
    77. expect := expect.New(t)
    78. expect(nil == Float64Ptr("true")).To.Equal(true)
    79. expect(nil == Float32Ptr("true")).To.Equal(true)
    80. expect(123 == *Float64Ptr("123")).To.Equal(true)
    81. expect(123 == *Float32Ptr("123")).To.Equal(true)
    82. expect(123.4 == *Float64Ptr("123.4")).To.Equal(true)
    83. expect(123.4 == *Float32Ptr("123.4")).To.Equal(true)
    84. expect(0 == *Float64Ptr("0")).To.Equal(true)
    85. expect(0 == *Float32Ptr("0")).To.Equal(true)
    86. expect(0 == *Float64Ptr(0)).To.Equal(true)
    87. expect(0 == *Float32Ptr(0)).To.Equal(true)
    88. expect(123 == *Float64Ptr(123)).To.Equal(true)
    89. expect(123 == *Float32Ptr(123)).To.Equal(true)
    90. expect(52 == *Float64Ptr(uint8(52))).To.Equal(true)
    91. expect(52 == *Float32Ptr(uint8(52))).To.Equal(true)
    92. expect(0 == *Float64Ptr("0.0000000000000000000001")).To.Equal(false)
    93. expect(0.0000000000000000000001 == *Float64Ptr("0.0000000000000000000001")).To.Equal(true)
    94. expect(0 == *Float64Ptr("0.0000000000000000000000")).To.Equal(true)
    95. }