golang将interface{}转化成[]interface{}、interface{}转化成map、interface{}转化成string

//将map[string]interface{}转换成map[string][]map[string]string
result := make(map[string]interface{})
//1.将interface{}转[]interface{}
//取interface{}
tmp1 := result["data"]
tmpResult, ok := tmp1.([]interface{})
if !ok {
log.Print("失败")
}
//2.将interface{}转map
for _, v := range tmpResult {
tmp2 := v.(map[string]interface{})
}
//3.将interface{}转化成string
last := InterfaceToString(tmp2)

func InterfaceToString(value interface{}) (s 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
	}

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要引用一个golang map[string]interface{},可以使用以下方法: 1. 通过键名引用 可以使用map[key]语法来引用map中的值,其中key是map中的键名。例如: ``` m := map[string]interface{}{ "name": "John", "age": 25, "address": map[string]string{ "street": "123 Main St", "city": "New York", }, } fmt.Println(m["name"]) // Output: John fmt.Println(m["age"]) // Output: 25 fmt.Println(m["address"]) // Output: map[city:New York street:123 Main St] ``` 2. 使用类型断言引用 当map的值是interface{}类型时,可以使用类型断言(type assertion)来获取具体类型的值。例如: ``` m := map[string]interface{}{ "name": "John", "age": 25, } name, ok := m["name"].(string) if ok { fmt.Println(name) // Output: John } ``` 在上面的代码中,我们首先使用类型断言将map中的值转换为string类型,然后检查类型断言是否成功。如果成功,我们就可以使用转换后的值。 3. 使用结构体引用 如果map中的值是一个结构体,我们可以使用结构体来引用它。例如: ``` type Address struct { Street string City string } type Person struct { Name string Age int Address Address } m := map[string]interface{}{ "name": "John", "age": 25, "address": Address{ Street: "123 Main St", City: "New York", }, } person := Person{ Name: m["name"].(string), Age: m["age"].(int), Address: m["address"].(Address), } fmt.Println(person.Name) // Output: John fmt.Println(person.Age) // Output: 25 fmt.Println(person.Address.Street) // Output: 123 Main St fmt.Println(person.Address.City) // Output: New York ``` 在上面的代码中,我们首先定义了一个Person结构体和一个Address结构体,然后将map中的值转换为结构体,并将其赋值给person变量。最后,我们可以通过person变量来引用map中的值。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值