Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a map of strings from map[string]interface{}?

Tags:

go

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?

like image 297
Karlom Avatar asked Mar 12 '17 01:03

Karlom


2 Answers

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"}
like image 58
Karlom Avatar answered Sep 25 '22 01:09

Karlom


Also you can do like this

ud := make(map[string]interface{})
ud["userName"] = "noob"
like image 38
Shahriar Avatar answered Sep 25 '22 01:09

Shahriar