|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596 |
- package utils
-
- import (
- "fmt"
- "reflect"
- "strings"
- )
-
- func Implode(glue string, args ...interface{}) string {
- data := make([]string, len(args))
- for i, s := range args {
- data[i] = fmt.Sprint(s)
- }
- return strings.Join(data, glue)
- }
- func ImplodeByKey(glue string, args ...interface{}) string {
- data := make([]string, len(args))
- for i, _ := range args {
- data[i] = fmt.Sprint(i)
- }
- return strings.Join(data, glue)
- }
-
- //字符串是否在数组里
- func InArr(target string, str_array []string) bool {
- for _, element := range str_array {
- if target == element {
- return true
- }
- }
- return false
- }
-
- //把数组的值放到key里
- func ArrayColumn(array interface{}, key string) (result map[string]interface{}, err error) {
- result = make(map[string]interface{})
- t := reflect.TypeOf(array)
- v := reflect.ValueOf(array)
- if t.Kind() != reflect.Slice {
- return nil, nil
- }
- if v.Len() == 0 {
- return nil, nil
- }
- for i := 0; i < v.Len(); i++ {
- indexv := v.Index(i)
- if indexv.Type().Kind() != reflect.Struct {
- return nil, nil
- }
- mapKeyInterface := indexv.FieldByName(key)
- if mapKeyInterface.Kind() == reflect.Invalid {
- return nil, nil
- }
- mapKeyString, err := InterfaceToString(mapKeyInterface.Interface())
- if err != nil {
- return nil, err
- }
- result[mapKeyString] = indexv.Interface()
- }
- return result, err
- }
-
- //转string
- func InterfaceToString(v interface{}) (result string, err error) {
- switch reflect.TypeOf(v).Kind() {
- case reflect.Int64, reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32:
- result = fmt.Sprintf("%v", v)
- case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
- result = fmt.Sprintf("%v", v)
- case reflect.String:
- result = v.(string)
- default:
- err = nil
- }
- return result, err
- }
-
- func HideTrueName(name string) string {
- res := "**"
- if name != "" {
- runs := []rune(name)
- leng := len(runs)
- if leng <= 3 {
- res = string(runs[0:1]) + res
- } else if leng < 5 {
- res = string(runs[0:2]) + res
- } else if leng < 10 {
- res = string(runs[0:2]) + "***" + string(runs[leng-2:leng])
- } else if leng < 16 {
- res = string(runs[0:3]) + "****" + string(runs[leng-3:leng])
- } else {
- res = string(runs[0:4]) + "*****" + string(runs[leng-4:leng])
- }
- }
- return res
- }
|