github.com/jxskiss/gopkg@v0.17.3/ptr/convert.go (about) 1 package ptr 2 3 import "strconv" 4 5 // IntToStringp converts x to a string pointer. 6 func IntToStringp(x int) *string { 7 str := strconv.FormatInt(int64(x), 10) 8 return &str 9 } 10 11 // IntpToStringp converts x to a string pointer. 12 // It returns nil if x is nil. 13 func IntpToStringp(x *int) *string { 14 if x == nil { 15 return nil 16 } 17 return IntToStringp(*x) 18 } 19 20 // IntpToString converts x to a string. 21 // It returns an empty string if x is nil. 22 func IntpToString(x *int) string { 23 if x == nil { 24 return "" 25 } 26 return strconv.FormatInt(int64(*x), 10) 27 } 28 29 // Int32ToStringp converts x to a string pointer. 30 func Int32ToStringp(x int32) *string { 31 str := strconv.FormatInt(int64(x), 10) 32 return &str 33 } 34 35 // Int32pToStringp converts x to a string pointer. 36 // It returns nil if x is nil. 37 func Int32pToStringp(x *int32) *string { 38 if x == nil { 39 return nil 40 } 41 return Int32ToStringp(*x) 42 } 43 44 // Int32pToString converts x to a string. 45 // It returns an emtpy string if x is nil. 46 func Int32pToString(x *int32) string { 47 if x == nil { 48 return "" 49 } 50 return strconv.FormatInt(int64(*x), 10) 51 } 52 53 // Int64ToStringp converts x to a string pointer. 54 func Int64ToStringp(x int64) *string { 55 str := strconv.FormatInt(x, 10) 56 return &str 57 } 58 59 // Int64pToStringp converts x to a string pointer. 60 // It returns nil if x is nil. 61 func Int64pToStringp(x *int64) *string { 62 if x == nil { 63 return nil 64 } 65 return Int64ToStringp(*x) 66 } 67 68 // Int64pToString converts x to a string. 69 // It returns an empty string if x is nil. 70 func Int64pToString(x *int64) string { 71 if x == nil { 72 return "" 73 } 74 return strconv.FormatInt(*x, 10) 75 } 76 77 // StringToIntp converts x to an int pointer. 78 // It returns nil if x is not a valid number string. 79 func StringToIntp(x string) *int { 80 i, err := strconv.ParseInt(x, 0, 0) 81 if err != nil { 82 return nil 83 } 84 ii := int(i) 85 return &ii 86 } 87 88 // StringpToIntp converts x to an int pointer. 89 // It returns nil if x is nil or not a valid number string. 90 func StringpToIntp(x *string) *int { 91 if x == nil { 92 return nil 93 } 94 return StringToIntp(*x) 95 } 96 97 // StringpToInt converts x to an integer. 98 // It returns zero if x is nil or not a valid number string. 99 func StringpToInt(x *string) int { 100 if x == nil { 101 return 0 102 } 103 i, _ := strconv.ParseInt(*x, 0, 0) 104 return int(i) 105 } 106 107 // StringToInt32p converts x to an int32 pointer. 108 // It returns nil if x is not a valid number string. 109 func StringToInt32p(x string) *int32 { 110 i, err := strconv.ParseInt(x, 0, 0) 111 if err != nil { 112 return nil 113 } 114 ii := int32(i) 115 return &ii 116 } 117 118 // StringpToInt32p converts x to an int32 pointer. 119 // It returns nil if x is nil or not a valid number string. 120 func StringpToInt32p(x *string) *int32 { 121 if x == nil { 122 return nil 123 } 124 return StringToInt32p(*x) 125 } 126 127 // StringpToInt32 converts x to an integer. 128 // It returns zero if x is nil or not a valid number string. 129 func StringpToInt32(x *string) int32 { 130 if x == nil { 131 return 0 132 } 133 i, _ := strconv.ParseInt(*x, 0, 0) 134 return int32(i) 135 } 136 137 // StringToInt64p converts x to an int64 pointer. 138 // It returns nil if x is not a valid number string. 139 func StringToInt64p(x string) *int64 { 140 i, err := strconv.ParseInt(x, 0, 0) 141 if err != nil { 142 return nil 143 } 144 return &i 145 } 146 147 // StringpToInt64p converts x to an int64 pointer. 148 // It returns nil if x is nil or not a valid number string. 149 func StringpToInt64p(x *string) *int64 { 150 if x == nil { 151 return nil 152 } 153 return StringToInt64p(*x) 154 } 155 156 // StringpToInt64 converts x to an int64 value. 157 // It returns zero if x is nil or not a valid number string. 158 func StringpToInt64(x *string) int64 { 159 if x == nil { 160 return 0 161 } 162 i, _ := strconv.ParseInt(*x, 0, 0) 163 return i 164 }