常用类型及数据操作方法
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.

273 lines
5.5 KiB

3 years ago
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. str = strings.ReplaceAll(str, "`", "")
  160. temp := strings.Split(str, "_")
  161. var upperStr string
  162. for y := 0; y < len(temp); y++ {
  163. vv := []rune(temp[y])
  164. for i := 0; i < len(vv); i++ {
  165. if i == 0 {
  166. vv[i] -= 32
  167. upperStr += string(vv[i])
  168. } else {
  169. upperStr += string(vv[i])
  170. }
  171. }
  172. }
  173. return upperStr
  174. }
  175. /**
  176. * 是否存在在字符切片中
  177. */
  178. func IsInStringArray(arr []string, str string) bool {
  179. var isIn bool = false
  180. length := len(arr)
  181. if length < 1 {
  182. return false
  183. }
  184. for _, item := range arr {
  185. if item == str {
  186. isIn = true
  187. break
  188. }
  189. }
  190. return isIn
  191. }
  192. /**
  193. * 字符串在字符切片中对应索引
  194. */
  195. func InStringArrayIndex(arr []string, str string) int {
  196. // var isIn bool = false
  197. length := len(arr)
  198. if length < 1 {
  199. return -1
  200. }
  201. var index int = -1
  202. for k, item := range arr {
  203. if item == str {
  204. // isIn = true
  205. index = k
  206. break
  207. }
  208. }
  209. return index
  210. }
  211. /*
  212. * 删除多余空格
  213. * 2019/05/05
  214. */
  215. func DeleteExtraSpace(s string) string {
  216. s1 := strings.Replace(s, " ", " ", -1) //替换tab
  217. regstr := "\\s{2,}" //两个及两个以上空格的正则表达式
  218. reg, _ := regexp.Compile(regstr) //编译正则表达式
  219. s2 := make([]byte, len(s1))
  220. copy(s2, s1)
  221. spc_index := reg.FindStringIndex(string(s2)) //在字符串中搜索
  222. for len(spc_index) > 0 {
  223. s2 = append(s2[:spc_index[0]+1], s2[spc_index[1]:]...) //删除多余空格
  224. spc_index = reg.FindStringIndex(string(s2))
  225. }
  226. return string(s2)
  227. }
  228. /*
  229. * 连接多个字符串
  230. * 2019/05/05
  231. */
  232. func StringJoin(s ...string) string {
  233. var build strings.Builder
  234. if len(s) > 0 {
  235. for _, v := range s {
  236. build.WriteString(v)
  237. }
  238. }
  239. return build.String()
  240. }
  241. /*
  242. * 连接url
  243. * 2019/05/05
  244. */
  245. func UrlJoin(host, url string) string {
  246. if strings.Contains(url, "http://") {
  247. return url
  248. }
  249. if strings.Contains(url, "https://") {
  250. return url
  251. }
  252. return StringJoin(host, url)
  253. }