If you care to use the reflect package, you could do something like
(disclaimer: totally untested):
myMap := make(map[string]interface{})
myObj := // whatever
type := reflect.TypeOf(myobj).(*StructType) // myObj had better be a struct!
for i := 0; i < type.NumField(); i++ {
myMap[type.Field(i).Name] = // something
}
For the //something, I /think/ you have to do something with
Field.offset, which would require the unsafe package as well.
On 8/16/10, m <phill...@gmail.com> wrote:
> Does there happen to be a quick conversion between the two? It would
> be extremely convenient. Since interfaces only support methods.
--
Scott Lawrence
package mainimport("fmt""encoding/json")
type Envelop map[string]interface{}type message struct {Type, Text string}
func main(){var m = make(Envelop)m["message"] = message{"error","You can't do that!"}j, err := json.Marshal(&m)if err != nil {fmt.Println(err)}fmt.Printf("Struct: %s\n", m)fmt.Printf("JsonIn: %s\n", j)var mm map[string]Enveloperr = json.Unmarshal(j, &mm)if err != nil {fmt.Println(err)}var ss Envelopss = mm["message"]fmt.Println("MR: ", ss)}