github.com/binbinly/pkg@v0.0.11-0.20240321014439-f4fbf666eb0f/util/convert.go (about) 1 package util 2 3 import ( 4 "encoding/base64" 5 "fmt" 6 "reflect" 7 "strconv" 8 "strings" 9 "time" 10 "unsafe" 11 ) 12 13 // S2B StringToBytes converts string to byte slice without a memory allocation. 14 // Ref: gin 15 func S2B(s string) []byte { 16 return *(*[]byte)(unsafe.Pointer(&struct { 17 string 18 Cap int 19 }{s, len(s)})) 20 } 21 22 // B2S BytesToString 23 func B2S(b []byte) string { 24 return *(*string)(unsafe.Pointer(&b)) 25 } 26 27 // MustString 强制转为字符串 28 func MustString(v any, timeLayout ...string) string { 29 switch s := v.(type) { 30 default: 31 return fmt.Sprint(v) 32 case string: 33 return s 34 case []byte: 35 return string(s) 36 case error: 37 return s.Error() 38 case nil: 39 return "" 40 case bool: 41 return strconv.FormatBool(s) 42 case int: 43 return strconv.Itoa(s) 44 case int8: 45 return strconv.FormatInt(int64(s), 10) 46 case int16: 47 return strconv.FormatInt(int64(s), 10) 48 case int32: 49 return strconv.Itoa(int(s)) 50 case int64: 51 return strconv.FormatInt(s, 10) 52 case uint: 53 return strconv.FormatUint(uint64(s), 10) 54 case uint8: 55 return strconv.FormatUint(uint64(s), 10) 56 case uint16: 57 return strconv.FormatUint(uint64(s), 10) 58 case uint32: 59 return strconv.FormatUint(uint64(s), 10) 60 case uint64: 61 return strconv.FormatUint(s, 10) 62 case float32: 63 return strconv.FormatFloat(float64(s), 'f', -1, 32) 64 case float64: 65 return strconv.FormatFloat(s, 'f', -1, 64) 66 case time.Time: 67 if len(timeLayout) > 0 { 68 return s.Format(timeLayout[0]) 69 } 70 return s.Format("2006-01-02 15:04:05") 71 case reflect.Value: 72 return MustString(s.Interface(), timeLayout...) 73 case fmt.Stringer: 74 return s.String() 75 } 76 } 77 78 // MustInt 强制转为整数 (int) 79 func MustInt(v any) int { 80 switch i := v.(type) { 81 default: 82 d, ok := i.(int) 83 if ok { 84 return d 85 } 86 return 0 87 case string: 88 v, err := strconv.Atoi(strings.TrimSpace(i)) 89 if err == nil { 90 return v 91 } 92 return 0 93 case bool: 94 if i { 95 return 1 96 } 97 return 0 98 case nil: 99 return 0 100 case int: 101 return i 102 case int8: 103 return int(i) 104 case int16: 105 return int(i) 106 case int32: 107 return int(i) 108 case int64: 109 return int(i) 110 case uint: 111 return int(i) 112 case uint8: 113 return int(i) 114 case uint16: 115 return int(i) 116 case uint32: 117 return int(i) 118 case uint64: 119 return int(i) 120 case float32: 121 return int(i) 122 case float64: 123 return int(i) 124 } 125 } 126 127 // B64Encode Base64 编码 128 func B64Encode(b []byte) string { 129 return base64.StdEncoding.EncodeToString(b) 130 } 131 132 // B64Decode Base64 解码 133 func B64Decode(s string) []byte { 134 if b, err := base64.StdEncoding.DecodeString(s); err == nil { 135 return b 136 } 137 138 return nil 139 } 140 141 // B64UrlEncode Base64 解码, 安全 URL, 替换: "+/" 为 "-_" 142 func B64UrlEncode(b []byte) string { 143 return base64.URLEncoding.EncodeToString(b) 144 } 145 146 // B64UrlDecode Base64 解码 147 func B64UrlDecode(s string) []byte { 148 if b, err := base64.URLEncoding.DecodeString(s); err == nil { 149 return b 150 } 151 152 return nil 153 }