go 中Interface{} 转string

func GetInterfaceToString(value interface{}) string {
   // interface 转 string
   var key string
   if value == nil {
      return key
   }

   switch value.(type) {
   case float64:
      ft := value.(float64)
      key = strconv.FormatFloat(ft, 'f', -1, 64)
   case float32:
      ft := value.(float32)
      key = strconv.FormatFloat(float64(ft), 'f', -1, 64)
   case int:
      it := value.(int)
      key = strconv.Itoa(it)
   case uint:
      it := value.(uint)
      key = strconv.Itoa(int(it))
   case int8:
      it := value.(int8)
      key = strconv.Itoa(int(it))
   case uint8:
      it := value.(uint8)
      key = strconv.Itoa(int(it))
   case int16:
      it := value.(int16)
      key = strconv.Itoa(int(it))
   case uint16:
      it := value.(uint16)
      key = strconv.Itoa(int(it))
   case int32:
      it := value.(int32)
      key = strconv.Itoa(int(it))
   case uint32:
      it := value.(uint32)
      key = strconv.Itoa(int(it))
   case int64:
      it := value.(int64)
      key = strconv.FormatInt(it, 10)
   case uint64:
      it := value.(uint64)
      key = strconv.FormatUint(it, 10)
   case string:
      key = value.(string)
   case []byte:
      key = string(value.([]byte))
   default:
      newValue, _ := json.Marshal(value)
      key = string(newValue)
   }

   return key
}

需要取下标时候 非常好用

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/989665.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-21
下一篇 2022-05-21

发表评论

登录后才能评论

评论列表(0条)

保存