From struct to map

58 views
Skip to first unread message

Tong Sun

unread,
Aug 21, 2017, 12:42:25 AM8/21/17
to golang-nuts
To covert a Go struct, say,

type Person struct {
 
First string
 
Last  string
}


into a map, 

him["First"]=...
him["Last"]=...

One way to do it is to marshall it into json then unmarhall it back. 

Is this the easiest way to do it? Is there any easier ways? 

Thanks


Jakob Borg

unread,
Aug 21, 2017, 1:14:39 AM8/21/17
to Tong Sun, golan...@googlegroups.com
This package:


Or, more generally, reflection or code generation. For your specific struct I'd just add a method to return the map representation if that's what you need. 

//jb
--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages