| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203 |
- package utils
- import (
- "container/list"
- "encoding/json"
- "strings"
- "github.com/gogf/gf/container/gvar"
- "github.com/gogf/gf/util/gconv"
- )
- // 将带分隔符的字符串切成int数组
- func ToIntArray(str string, split ...string) []int {
- splitChar := ","
- if len(split) > 0 {
- splitChar = split[0]
- }
- result := make([]int, 0)
- if str == "" {
- return result
- }
- arr := strings.Split(str, splitChar)
- if len(arr) > 0 {
- for i := range arr {
- if arr[i] != "" {
- result = append(result, gconv.Int(arr[i]))
- }
- }
- }
- return result
- }
- // 将带分隔符的字符串切成int64数组
- func ToInt64Array(str string, split ...string) []int64 {
- splitChar := ","
- if len(split) > 0 {
- splitChar = split[0]
- }
- result := make([]int64, 0)
- if str == "" {
- return result
- }
- arr := strings.Split(str, splitChar)
- if len(arr) > 0 {
- for i := range arr {
- if arr[i] != "" {
- result = append(result, gconv.Int64(arr[i]))
- }
- }
- }
- return result
- }
- // 将带分隔符的字符串切成string数组
- func ToStringArray(str string, split ...string) []string {
- if str == "" {
- return nil
- }
- splitChar := ","
- if len(split) > 0 {
- splitChar = split[0]
- }
- result := strings.Split(str, splitChar)
- return result
- }
- // 将数据表查询返回的ID数据转换成带分隔符的Ids字符串,形如:1,2,3
- func ToIdsString(array []*gvar.Var, split ...string) string {
- splitChar := ","
- if len(split) > 0 {
- splitChar = split[0]
- }
- result := ""
- for _, v := range array {
- result = result + v.String() + splitChar
- }
- result = result[0 : len(result)-1]
- return result
- }
- // 将数据表查询返回的ID数组(整数数组,支持int,int32,int64)数据转换成带分隔符的Ids字符串,形如:1,2,3
- func IntArrayToIdsString(array interface{}, split ...string) string {
- if array == nil {
- return ""
- }
- splitChar := ","
- if len(split) > 0 {
- splitChar = split[0]
- }
- result := ""
- switch value := array.(type) {
- case []int:
- if len(value) == 0 {
- return ""
- }
- for _, v := range value {
- result = result + gconv.String(v) + splitChar
- }
- case []int32:
- if len(value) == 0 {
- return ""
- }
- for _, v := range value {
- result = result + gconv.String(v) + splitChar
- }
- case []int64:
- if len(value) == 0 {
- return ""
- }
- for _, v := range value {
- result = result + gconv.String(v) + splitChar
- }
- case []string:
- if len(value) == 0 {
- return ""
- }
- for _, v := range value {
- result = result + v + splitChar
- }
- }
- result = result[0 : len(result)-1]
- return result
- }
- // 将数据表查询返回的ID数据转换成带分隔符的Ids字符串,形如:1,2,3
- func Int64ArrayToIdsString(array []int64, split ...string) string {
- splitChar := ","
- if len(split) > 0 {
- splitChar = split[0]
- }
- result := ""
- for _, v := range array {
- result = result + gconv.String(v) + splitChar
- }
- result = result[0 : len(result)-1]
- return result
- }
- // 过滤收尾有分隔符的数字字符串
- func ReplaceHeadAndEndStr(str string, split ...string) string {
- splitChar := ","
- if len(split) > 0 {
- splitChar = split[0]
- }
- result := ""
- arr := strings.Split(str, splitChar)
- if len(arr) <= 0 {
- return result
- }
- for i := range arr {
- if arr[i] != "" {
- if i == 0 {
- result = arr[i]
- } else {
- result += "," + arr[i]
- }
- }
- }
- return result
- }
- // ToJsonStr 对象转字符串
- func ToJsonString(data interface{}) (string, error) {
- result, err := json.Marshal(data)
- return string(result), err
- }
- // Json字符串转对象
- func JsonToStruct(source string, destination interface{}) error {
- err := json.Unmarshal([]byte(source), destination)
- return err
- }
- // Json字符串转Map
- func JsonToMap(source string) (map[string]string, error) {
- resultMap := make(map[string]string)
- err := json.Unmarshal([]byte(source), &resultMap)
- if err != nil {
- return nil, err
- }
- return resultMap, nil
- }
- // list对象转数组
- func ListToArray(list *list.List) []interface{} {
- var len = list.Len()
- if len == 0 {
- return nil
- }
- var arr []interface{}
- for e := list.Front(); e != nil; e = e.Next() {
- arr = append(arr, e.Value)
- }
- return arr
- }
|