Converting map to struct

前端 未结 6 1364
灰色年华
灰色年华 2020-12-04 05:46

I am trying to create a generic method in Go that will fill a struct using data from a map[string]interface{}. For example, the method signature an

6条回答
  •  感情败类
    2020-12-04 06:31

    • the simplest way to do that is using encoding/json package

    just for example:

    package main
    import (
        "fmt"
        "encoding/json"
    )
    
    type MyAddress struct {
        House string
        School string
    }
    type Student struct {
        Id int64
        Name string
        Scores float32
        Address MyAddress
        Labels []string
    }
    
    func Test() {
    
        dict := make(map[string]interface{})
        dict["id"] = 201902181425       // int
        dict["name"] = "jackytse"       // string
        dict["scores"] = 123.456        // float
        dict["address"] = map[string]string{"house":"my house", "school":"my school"}   // map
        dict["labels"] = []string{"aries", "warmhearted", "frank"}      // slice
    
        jsonbody, err := json.Marshal(dict)
        if err != nil {
            // do error check
            fmt.Println(err)
            return
        }
    
        student := Student{}
        if err := json.Unmarshal(jsonbody, &student); err != nil {
            // do error check
            fmt.Println(err)
            return
        }
    
        fmt.Printf("%#v\n", student)
    }
    
    func main() {
        Test()
    }
    

提交回复
热议问题