栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Go语言

go 中Interface{} 转string

Go语言 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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
}

需要取下标时候 非常好用

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/902591.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号