英文:
How to create a map of strings from map[string]interface{}?
问题
新手地鼠在这里。
我想要填充这个结构体字段:
userData map[string]interface{}
数据如下:
ud := map[string]string{"userName": "noob"}
但是我得到了这个编译时错误:
无法将ud(类型为map[string]string)作为map[string]interface{}类型的字段值使用
我还尝试了:
ud := map[string]interface{}{"userName": "noob".(string)}
但是这会报错:
语法错误:意外的字符串文字
我该如何修复这个问题?
英文:
Noob gopher here.
I'd like to fill data into this struct field:
userData map[string]interface{}
The data is like:
ud := map[string]string{"userName": "noob"}
but I get this comple time error:
> cannot use ud (type map[string]string) as type map[string]interface {}
> in field value
I also tried:
ud := map[string]interface{"userName": "noob".(string)}
but this gives:
> syntax error: unexpected string literal
How can I fix this?
答案1
得分: 20
感谢gopher slack上的好心人,我意识到问题出在哪里了。
基本上,我只是错过了接口的{},像这样:
ud := map[string]interface{}{"userName": "noob"}
英文:
Thanks good guys on gopher slack, I realized what was wrong.
Basically I just missed {} of the interface, like this:
ud := map[string]interface{}{"userName": "noob"}
答案2
得分: 4
你也可以这样做:
ud := make(map[string]interface{})
ud["userName"] = "noob"
英文:
Also you can do like this
ud := make(map[string]interface{})
ud["userName"] = "noob"
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论