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

252 lines
5.2 KiB

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. * 2019/05/05
  194. */
  195. func DeleteExtraSpace(s string) string {
  196. s1 := strings.Replace(s, " ", " ", -1) //替换tab
  197. regstr := "\\s{2,}" //两个及两个以上空格的正则表达式
  198. reg, _ := regexp.Compile(regstr) //编译正则表达式
  199. s2 := make([]byte, len(s1))
  200. copy(s2, s1)
  201. spc_index := reg.FindStringIndex(string(s2)) //在字符串中搜索
  202. for len(spc_index) > 0 {
  203. s2 = append(s2[:spc_index[0]+1], s2[spc_index[1]:]...) //删除多余空格
  204. spc_index = reg.FindStringIndex(string(s2))
  205. }
  206. return string(s2)
  207. }
  208. /*
  209. * 连接多个字符串
  210. * 2019/05/05
  211. */
  212. func StringJoin(s ...string) string {
  213. var build strings.Builder
  214. if len(s) > 0 {
  215. for _, v := range s {
  216. build.WriteString(v)
  217. }
  218. }
  219. return build.String()
  220. }
  221. /*
  222. * 连接url
  223. * 2019/05/05
  224. */
  225. func UrlJoin(host, url string) string {
  226. if strings.Contains(url, "http://") {
  227. return url
  228. }
  229. if strings.Contains(url, "https://") {
  230. return url
  231. }
  232. return StringJoin(host, url)
  233. }