问题描述
map[interface {}]interface {}
map[interface {}]interface {}
map[string]string
map[string]string
我还需要从数据中生成一个键列表,因为这些是事先不知道的.
I need to generate a list of the keys from the data as well, as those are not known beforehand.
mfmt.Println(m)map[k0:v0 K1:v1 k2:v2 ... ]
mfmt.Println(m)map[k0:v0 K1:v1 k2:v2 ... ]
我怎样才能做 fmt.Println 能做的事?
How can I do what fmt.Println is able to do?
推荐答案
一种处理未知接口的安全方式,只需使用 fmt.Sprintf()
A secure way to process unknown interfaces, just use fmt.Sprintf()
package main
import (
"fmt"
)
func main() {
mapInterface := make(map[interface{}]interface{})
mapString := make(map[string]string)
mapInterface["k1"] = 1
mapInterface[3] = "hello"
mapInterface["world"] = 1.05
for key, value := range mapInterface {
strKey := fmt.Sprintf("%v", key)
strValue := fmt.Sprintf("%v", value)
mapString[strKey] = strValue
}
fmt.Printf("%#v", mapString)
}
这篇关于将 map[interface {}]interface {} 转换为 map[string]string的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!