常用类型及数据操作方法
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

272 lines
5.5 KiB

3 years ago
3 years ago
  1. /*
  2. * string functions
  3. */
  4. package helper
  5. import (
  6. "errors"
  7. "fmt"
  8. "log"
  9. "math/rand"
  10. "regexp"
  11. "strconv"
  12. "strings"
  13. "time"
  14. )
  15. func ToString(v interface{}) string {
  16. var value string
  17. switch v.(type) {
  18. case string:
  19. value = v.(string)
  20. case int:
  21. value = strconv.Itoa(v.(int))
  22. case float64:
  23. value = strconv.FormatFloat(v.(float64), 'f', 2, 64)
  24. case float32:
  25. value = strconv.FormatFloat(float64(v.(float32)), 'f', 2, 64)
  26. case int64:
  27. value = strconv.FormatInt(v.(int64), 10)
  28. case []uint8:
  29. value = string(v.([]uint8))
  30. // case []byte:
  31. // value = string(v.([]byte))
  32. case interface{}:
  33. value = v.(string)
  34. case nil:
  35. value = ""
  36. default:
  37. log.Println("参数值类型错误", v, "not in string|int|float64|interface|int64")
  38. }
  39. return strings.Trim(value, " ")
  40. }
  41. func ToStr(v interface{}) string {
  42. var value string
  43. switch v.(type) {
  44. case string:
  45. value = v.(string)
  46. case int:
  47. value = strconv.Itoa(v.(int))
  48. case float64:
  49. value = strconv.FormatFloat(v.(float64), 'f', 0, 64)
  50. case float32:
  51. value = strconv.FormatFloat(float64(v.(float32)), 'f', 0, 64)
  52. case int64:
  53. value = strconv.FormatInt(v.(int64), 10)
  54. case []uint8:
  55. value = string(v.([]uint8))
  56. // case []byte:
  57. // value = string(v.([]byte))
  58. case interface{}:
  59. value = v.(string)
  60. case nil:
  61. value = ""
  62. default:
  63. log.Println("参数值类型错误", v, "not in string|int|float64|interface|int64")
  64. }
  65. return strings.Trim(value, " ")
  66. }
  67. func ToFloat64(v interface{}) (float64, error) {
  68. var value float64
  69. var err error = nil
  70. switch v.(type) {
  71. case string:
  72. v_tmp := v.(string)
  73. value, _ = strconv.ParseFloat(v_tmp, 64)
  74. case int:
  75. value = float64(v.(int))
  76. case int64:
  77. value = float64(v.(int64))
  78. case float64:
  79. value = v.(float64)
  80. case interface{}:
  81. value = v.(float64)
  82. case nil:
  83. value = 0
  84. default:
  85. err = errors.New("参数值类型错误")
  86. log.Println("参数值类型错误", v, "not in string|int|float64|interface|int64")
  87. }
  88. return value, err
  89. }
  90. func ToInt(inter interface{}) int {
  91. var value int
  92. switch inter.(type) {
  93. case string:
  94. value, _ = strconv.Atoi(inter.(string))
  95. case int:
  96. value = inter.(int)
  97. case int64:
  98. value = int(inter.(int64))
  99. case float64:
  100. value, _ = strconv.Atoi(fmt.Sprintf("%1.0f", inter))
  101. case nil:
  102. value = 0
  103. case interface{}:
  104. value = inter.(int)
  105. default:
  106. log.Println("参数值类型错误", inter, "not in string|int|float64|interface|int64")
  107. }
  108. return value
  109. }
  110. func ToInt64(inter interface{}) int64 {
  111. var value int64
  112. switch inter.(type) {
  113. case string:
  114. value, _ = strconv.ParseInt(inter.(string), 10, 64)
  115. case int:
  116. value = int64(inter.(int))
  117. case int64:
  118. value = inter.(int64)
  119. case float64:
  120. value_int, _ := strconv.Atoi(fmt.Sprintf("%1.0f", inter))
  121. value = int64(value_int)
  122. case nil:
  123. value = 0
  124. case interface{}:
  125. if _, ok := inter.(int64); !ok {
  126. value = inter.(int64)
  127. }
  128. default:
  129. log.Println("参数值类型错误", inter, "not in string|int|float64|interface|int64")
  130. }
  131. return value
  132. }
  133. //生成随机字符串
  134. func GetRandomString(length int) string {
  135. str := "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
  136. bytes := []byte(str)
  137. result := []byte{}
  138. r := rand.New(rand.NewSource(time.Now().UnixNano()))
  139. for i := 0; i < length; i++ {
  140. result = append(result, bytes[r.Intn(len(bytes))])
  141. }
  142. return string(result)
  143. }
  144. //生成随机数字
  145. func GetRandomNumber(length int) string {
  146. str := "0123456789"
  147. bytes := []byte(str)
  148. result := []byte{}
  149. r := rand.New(rand.NewSource(time.Now().UnixNano()))
  150. for i := 0; i < length; i++ {
  151. result = append(result, bytes[r.Intn(len(bytes))])
  152. }
  153. return string(result)
  154. }
  155. /**
  156. * 字符串转大驼峰 ios_bbbbbbbb -> IosBbbbbbbbb
  157. */
  158. func StrFirstToUpper(str string) string {
  159. temp := strings.Split(str, "_")
  160. var upperStr string
  161. for y := 0; y < len(temp); y++ {
  162. vv := []rune(temp[y])
  163. for i := 0; i < len(vv); i++ {
  164. if i == 0 {
  165. vv[i] -= 32
  166. upperStr += string(vv[i])
  167. } else {
  168. upperStr += string(vv[i])
  169. }
  170. }
  171. }
  172. return upperStr
  173. }
  174. /**
  175. * 是否存在在字符切片中
  176. */
  177. func IsInStringArray(arr []string, str string) bool {
  178. var isIn bool = false
  179. length := len(arr)
  180. if length < 1 {
  181. return false
  182. }
  183. for _, item := range arr {
  184. if item == str {
  185. isIn = true
  186. break
  187. }
  188. }
  189. return isIn
  190. }
  191. /**
  192. * 字符串在字符切片中对应索引
  193. */
  194. func InStringArrayIndex(arr []string, str string) int {
  195. // var isIn bool = false
  196. length := len(arr)
  197. if length < 1 {
  198. return -1
  199. }
  200. var index int = -1
  201. for k, item := range arr {
  202. if item == str {
  203. // isIn = true
  204. index = k
  205. break
  206. }
  207. }
  208. return index
  209. }
  210. /*
  211. * 删除多余空格
  212. * 2019/05/05
  213. */
  214. func DeleteExtraSpace(s string) string {
  215. s1 := strings.Replace(s, " ", " ", -1) //替换tab
  216. regstr := "\\s{2,}" //两个及两个以上空格的正则表达式
  217. reg, _ := regexp.Compile(regstr) //编译正则表达式
  218. s2 := make([]byte, len(s1))
  219. copy(s2, s1)
  220. spc_index := reg.FindStringIndex(string(s2)) //在字符串中搜索
  221. for len(spc_index) > 0 {
  222. s2 = append(s2[:spc_index[0]+1], s2[spc_index[1]:]...) //删除多余空格
  223. spc_index = reg.FindStringIndex(string(s2))
  224. }
  225. return string(s2)
  226. }
  227. /*
  228. * 连接多个字符串
  229. * 2019/05/05
  230. */
  231. func StringJoin(s ...string) string {
  232. var build strings.Builder
  233. if len(s) > 0 {
  234. for _, v := range s {
  235. build.WriteString(v)
  236. }
  237. }
  238. return build.String()
  239. }
  240. /*
  241. * 连接url
  242. * 2019/05/05
  243. */
  244. func UrlJoin(host, url string) string {
  245. if strings.Contains(url, "http://") {
  246. return url
  247. }
  248. if strings.Contains(url, "https://") {
  249. return url
  250. }
  251. return StringJoin(host, url)
  252. }