且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

将map [interface {}] interface {}转换为map [string]字符串

更新时间:2022-10-21 23:18:01

也许我误会了这个问题,但这个工作吗?

  m:= make(map [interface {}] interface {})
m [foo] =bar

m2:= make(map [string] string)

为键值:=范围m {
SW it键:= key。(type){
case string:
switch value:= value。(type){
case string:
m2 [key] = value
}
}
}


From a source I cannot influence I am given data in a map, which arrives as map[interface {}]interface {}.

I need to process the contained data, preferably as map[string]string (the data within is perfectly suitable for that).

I need to generate a list of the keys from the data as well, as those are not known beforehand.

Most similar questions I could find on the web say more or less, that this is impossible, but if my map is m, fmt.Println(m) shows the data is there, readable as map[k0:v0 K1:v1 k2:v2 ... ].

How can I do what fmt.Println is able to do?

Perhaps I misunderstand the question, but would this work?

m := make(map[interface{}]interface{})
m["foo"] = "bar"

m2 := make(map[string]string)   

for key, value := range m {        
    switch key := key.(type) {
    case string:
        switch value := value.(type) {
        case string:
            m2[key] = value
        }
    }
}