新Sir

导航

 
package main

import (

    "fmt"
    "github.com/mitchellh/mapstructure"

)

type User struct {
    Name string
    Age int
}

func MapToStruct() {
    mapInstance := make(map[string]interface{})
    mapInstance["name"] = "stefan"
    mapInstance["age"] = 28
    fmt.Println(mapInstance)

    var person User
    if err := mapstructure.Decode(mapInstance, &person); err != nil {
        fmt.Println(err)
    }
    fmt.Printf("struct:%v", person)
    fmt.Println(person.Name,person.Age)
}



func main()  {
    MapToStruct()

}

 

posted on 2020-03-25 19:11  新Sir  阅读(4230)  评论(0编辑  收藏  举报