Can i pass into function a slice of structs, converted to []interface{}
, fill it and use after function end work?
Here is full example of the problem https://play.golang.org/p/iPijsawEEg
Short describe:
type DBResponse struct {
Rows int `json:"rows"`
Error string `json:"error"`
Value json.RawMessage `json:"value"`
}
type User struct {
Id int `json:"id"`
Name string `json:"name"`
}
func loadDBRows(p []interface{}) {
var response DBResponse
someDataFromDB := []byte("{\"rows\":1, \"error\": \"\", \"value\": {\"name\":\"John\", \"id\":2}}")
json.Unmarshal(someDataFromDB, &response)
json.Unmarshal(response.Value, &p[0])
fmt.Println(p)//p[0] filled with map, not object
}
func main() {
users := make([]User, 5)
data := make([]interface{}, 5)
for i := range users {
data[i] = users[i]
}
loadDBRows(data)
}
This problem can be easily solved for single interface{}
, u can test in at full example. Why i can't solve it for slice?
I want to do it without reflect! Is there any "true way" to write universal json parser to selected data struct without reflect and map[string]interface{}? Don't want complicated code and extra operations
Thank you for help!