Converting map to struct

前端 未结 6 1358
灰色年华
灰色年华 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:28

    There are two steps:

    1. Convert interface to JSON Byte
    2. Convert JSON Byte to struct

    Below is an example:

    dbByte, _ := json.Marshal(dbContent)
    _ = json.Unmarshal(dbByte, &MyStruct)
    

提交回复
热议问题