how to convert map to struct

3,658 views
Skip to first unread message

Yasutaka Kawamoto

unread,
Oct 12, 2013, 5:18:32 AM10/12/13
to golan...@googlegroups.com
Please tell me how to covert map to struct.
# I saw the link, but I didn't understand much.

For example,

//define map and initialization
var m url.Vaues (= map[string][]string)  ) 
m["name"] = []string{"go"}
m["address"] = []string{"japan"}
m["hobbies"] = []string{"watching tv", "listen music"}

//define struct
type example struct{
    name string
    address string
    hobbies  []string
}

// convert map to struct
????

// expect like this
example{ "go", "japan",[]string{"watching tv", "listen music"}}


Thank you.
Yasutaka

Jan Mercl

unread,
Oct 12, 2013, 5:28:58 AM10/12/13
to Yasutaka Kawamoto, golang-nuts
On Sat, Oct 12, 2013 at 11:18 AM, Yasutaka Kawamoto <kwm...@gmail.com> wrote:
> Please tell me how to covert map to struct.

// will panic for empty m["name"] or m["address"]
v := example{m["name"][0], m["address"][0], m["hobbies"]}

Also: http://play.golang.org/p/oxnu6K9jgX

-j

Sebastien Binet

unread,
Oct 12, 2013, 5:36:52 AM10/12/13
to Jan Mercl, Yasutaka Kawamoto, golang-nuts
there is also this nice package:
https://github.com/mitchellh/mapstructure

-s

Yasutaka Kawamoto

unread,
Oct 12, 2013, 5:57:29 AM10/12/13
to Sebastien Binet, Jan Mercl, golang-nuts
Thank you Jan.
But I assume the struct has a lot of fields, I think I want to use
reflect package.

Thank you Sebastien.
> https://github.com/mitchellh/mapstructure
This is good package.
But the type like map[string][]string does not seem to support.

In fact, the following message is displayed.
cannot use map[string][]string literal (type map[string][]string) as
type map[string]interface {} in assignment


Yasutaka

Sebastien Binet

unread,
Oct 12, 2013, 6:04:47 AM10/12/13
to Yasutaka Kawamoto, Jan Mercl, golang-nuts
converting your map[string][]string to something mapstructure can
understand is easy enough:
http://play.golang.org/p/yyDt8gUV-7

-s

Jan Mercl

unread,
Oct 12, 2013, 6:20:42 AM10/12/13
to Yasutaka Kawamoto, Sebastien Binet, golang-nuts
On Sat, Oct 12, 2013 at 11:57 AM, Yasutaka Kawamoto <kwm...@gmail.com> wrote:
> But I assume the struct has a lot of fields, I think I want to use
> reflect package.

It's only _one_ (1!) simple assignment statement. Using reflect for
that is, umm it is, ... err, just forget what I said.

-j

Yasutaka Kawamoto

unread,
Oct 12, 2013, 6:48:45 AM10/12/13
to Jan Mercl, Sebastien Binet, golang-nuts
Thanks All,

> http://play.golang.org/p/yyDt8gUV-7
I used this and mapstructure lib, but it is not the result I expect.
http://play.golang.org/p/6L88pgTjQg
// Of course, I know it does't work on playground because use github package.

I don't know what the problem...

yasu

Dan Kortschak

unread,
Oct 12, 2013, 7:22:29 AM10/12/13
to Yasutaka Kawamoto, Jan Mercl, Sebastien Binet, golang-nuts
You'll need the fields to be exported since the package uses reflect.

Cao Nguyên

unread,
Oct 12, 2013, 8:15:37 AM10/12/13
to golan...@googlegroups.com
If performance is not your concern, you can do:

map -> json -> struct (with the encoding/json pkg)

this way you can easyly handle nested map/struct, I mean:
m := make(map[string]interface{})
m["a"] = 1
m["B"] = map[string]int{"C":2, "D":3}

can be convert to Foo

type Foo struct {
  A int `json:"a"`
  B Bar
}
type Bar struct {
  C int
  D int
}
Vào 16:18:32 UTC+7 Thứ bảy, ngày 12 tháng mười năm 2013, Yasutaka Kawamoto đã viết:

Yasutaka Kawamoto

unread,
Oct 12, 2013, 10:31:20 AM10/12/13
to Cao Nguyên, Dan Kortschak, golang-nuts
To Dan,
Oh... I forgot
Thank you

To Cao,
> map -> json -> struct (with the encoding/json pkg)
That's it!
Although I thought I need to use reflect package, I didn't notice
that I can do with json package.

To All,
I could do.
http://play.golang.org/p/Kd7TRoRG5w

Thank you very much

Dan Kortschak

unread,
Oct 12, 2013, 6:08:13 PM10/12/13
to Yasutaka Kawamoto, Cao Nguyên, golang-nuts
You are using reflect if you are using encoding/json. This is not an
efficient approach.
Reply all
Reply to author
Forward
0 new messages