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

307 lines
6.5 KiB

3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
2 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 time.Time:
  33. value = v.(time.Time).Format("2006-01-02 15:04:05")
  34. case interface{}:
  35. value = v.(string)
  36. case nil:
  37. value = ""
  38. default:
  39. log.Println("参数值类型错误", v, "not in string|int|float64|interface|int64")
  40. }
  41. return strings.Trim(value, " ")
  42. }
  43. func ToStr(v interface{}) string {
  44. var value string
  45. switch v.(type) {
  46. case string:
  47. value = v.(string)
  48. case int:
  49. value = strconv.Itoa(v.(int))
  50. case float64:
  51. value = strconv.FormatFloat(v.(float64), 'f', 0, 64)
  52. case float32:
  53. value = strconv.FormatFloat(float64(v.(float32)), 'f', 0, 64)
  54. case int64:
  55. value = strconv.FormatInt(v.(int64), 10)
  56. case []uint8:
  57. value = string(v.([]uint8))
  58. // case []byte:
  59. // value = string(v.([]byte))
  60. case interface{}:
  61. value = v.(string)
  62. case nil:
  63. value = ""
  64. default:
  65. log.Println("参数值类型错误", v, "not in string|int|float64|interface|int64")
  66. }
  67. return strings.Trim(value, " ")
  68. }
  69. func ToFloat64(v interface{}) (float64, error) {
  70. var value float64
  71. var err error = nil
  72. switch v.(type) {
  73. case string:
  74. v_tmp := v.(string)
  75. value, _ = strconv.ParseFloat(v_tmp, 64)
  76. case int:
  77. value = float64(v.(int))
  78. case int64:
  79. value = float64(v.(int64))
  80. case float64:
  81. value = v.(float64)
  82. case interface{}:
  83. value = v.(float64)
  84. case nil:
  85. value = 0
  86. default:
  87. err = errors.New("参数值类型错误")
  88. log.Println("参数值类型错误", v, "not in string|int|float64|interface|int64")
  89. }
  90. return value, err
  91. }
  92. func ToInt(inter interface{}) int {
  93. var value int
  94. switch inter.(type) {
  95. case string:
  96. value, _ = strconv.Atoi(inter.(string))
  97. case int:
  98. value = inter.(int)
  99. case int64:
  100. value = int(inter.(int64))
  101. case float64:
  102. value, _ = strconv.Atoi(fmt.Sprintf("%1.0f", inter))
  103. case nil:
  104. value = 0
  105. case interface{}:
  106. value = inter.(int)
  107. default:
  108. log.Println("参数值类型错误", inter, "not in string|int|float64|interface|int64")
  109. }
  110. return value
  111. }
  112. func ToInt64(inter interface{}) int64 {
  113. var value int64
  114. switch inter.(type) {
  115. case string:
  116. value, _ = strconv.ParseInt(inter.(string), 10, 64)
  117. case int:
  118. value = int64(inter.(int))
  119. case int64:
  120. value = inter.(int64)
  121. case float64:
  122. value_int, _ := strconv.Atoi(fmt.Sprintf("%1.0f", inter))
  123. value = int64(value_int)
  124. case nil:
  125. value = 0
  126. case interface{}:
  127. if _, ok := inter.(int64); !ok {
  128. value = inter.(int64)
  129. }
  130. default:
  131. log.Println("参数值类型错误", inter, "not in string|int|float64|interface|int64")
  132. }
  133. return value
  134. }
  135. // 生成随机字符串
  136. func GetRandomString(length int) string {
  137. str := "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
  138. bytes := []byte(str)
  139. result := []byte{}
  140. r := rand.New(rand.NewSource(time.Now().UnixNano()))
  141. for i := 0; i < length; i++ {
  142. result = append(result, bytes[r.Intn(len(bytes))])
  143. }
  144. return string(result)
  145. }
  146. // 生成随机数字
  147. func GetRandomNumber(length int) string {
  148. str := "0123456789"
  149. bytes := []byte(str)
  150. result := []byte{}
  151. r := rand.New(rand.NewSource(time.Now().UnixNano()))
  152. for i := 0; i < length; i++ {
  153. result = append(result, bytes[r.Intn(len(bytes))])
  154. }
  155. return string(result)
  156. }
  157. /**
  158. * 字符串转大驼峰 ios_bbbbbbbb -> IosBbbbbbbbb
  159. */
  160. func StrFirstToUpper(str string) string {
  161. str = strings.ReplaceAll(str, "`", "")
  162. temp := strings.Split(str, "_")
  163. var upperStr string
  164. for y := 0; y < len(temp); y++ {
  165. vv := []rune(temp[y])
  166. for i := 0; i < len(vv); i++ {
  167. if i == 0 {
  168. vv[i] -= 32
  169. upperStr += string(vv[i])
  170. } else {
  171. upperStr += string(vv[i])
  172. }
  173. }
  174. }
  175. return upperStr
  176. }
  177. /**
  178. * 是否存在在字符切片中
  179. */
  180. func IsInStringArray(arr []string, str string) bool {
  181. var isIn bool = false
  182. length := len(arr)
  183. if length < 1 {
  184. return false
  185. }
  186. for _, item := range arr {
  187. if item == str {
  188. isIn = true
  189. break
  190. }
  191. }
  192. return isIn
  193. }
  194. /**
  195. * 字符串在字符切片中对应索引
  196. */
  197. func InStringArrayIndex(arr []string, str string) int {
  198. // var isIn bool = false
  199. length := len(arr)
  200. if length < 1 {
  201. return -1
  202. }
  203. var index int = -1
  204. for k, item := range arr {
  205. if item == str {
  206. // isIn = true
  207. index = k
  208. break
  209. }
  210. }
  211. return index
  212. }
  213. /*
  214. * 删除多余空格
  215. * 2019/05/05
  216. */
  217. func DeleteExtraSpace(s string) string {
  218. s1 := strings.Replace(s, " ", " ", -1) //替换tab
  219. regstr := "\\s{2,}" //两个及两个以上空格的正则表达式
  220. reg, _ := regexp.Compile(regstr) //编译正则表达式
  221. s2 := make([]byte, len(s1))
  222. copy(s2, s1)
  223. spc_index := reg.FindStringIndex(string(s2)) //在字符串中搜索
  224. for len(spc_index) > 0 {
  225. s2 = append(s2[:spc_index[0]+1], s2[spc_index[1]:]...) //删除多余空格
  226. spc_index = reg.FindStringIndex(string(s2))
  227. }
  228. return string(s2)
  229. }
  230. /*
  231. * 连接多个字符串
  232. * 2019/05/05
  233. */
  234. func StringJoin(s ...string) string {
  235. var build strings.Builder
  236. if len(s) > 0 {
  237. for _, v := range s {
  238. build.WriteString(v)
  239. }
  240. }
  241. return build.String()
  242. }
  243. /*
  244. * 连接url
  245. * 2019/05/05
  246. */
  247. func UrlJoin(host, url string) string {
  248. if strings.Contains(url, "http://") {
  249. return url
  250. }
  251. if strings.Contains(url, "https://") {
  252. return url
  253. }
  254. return StringJoin(host, url)
  255. }
  256. /**
  257. * 去除字符串的html标签
  258. * @2021/10/20
  259. */
  260. func TrimHtml(src string) string {
  261. //将HTML标签全转换成小写
  262. re, _ := regexp.Compile("\\<[\\S\\s]+?\\>")
  263. src = re.ReplaceAllStringFunc(src, strings.ToLower)
  264. //去除STYLE
  265. re, _ = regexp.Compile("\\<style[\\S\\s]+?\\</style\\>")
  266. src = re.ReplaceAllString(src, "")
  267. //去除SCRIPT
  268. re, _ = regexp.Compile("\\<script[\\S\\s]+?\\</script\\>")
  269. src = re.ReplaceAllString(src, "")
  270. //去除所有尖括号内的HTML代码,并换成换行符
  271. re, _ = regexp.Compile("\\<[\\S\\s]+?\\>")
  272. src = re.ReplaceAllString(src, "\n")
  273. //去除连续的换行符
  274. re, _ = regexp.Compile("\\s{2,}")
  275. src = re.ReplaceAllString(src, "\n")
  276. return strings.TrimSpace(src)
  277. }
  278. /**
  279. * 腾讯云图片压缩
  280. * @2021/11/19
  281. * @linsen
  282. */
  283. func TencentCloudImageCompress(imgUrl, ratio string) string {
  284. return imgUrl + "?imageMogr2/thumbnail/" + ratio + "x/interlace/1"
  285. }