What's the best way to Unmarshal
bson byte[] data into an array of structs, when the array of structs is passed into an interface{}
parameter?
For demonstration purposes, in the following code, I use bson.Marshal()
on the inStructArr
to get the byte[]
type of data. This is so I can use bson.Unmarshal(...)
to pipe into the outStructArr
.
import "gopkg.in/mgo.v2/bson"
type User struct {
Name string
}
func DecodeArrData(inStructArr, outStructArr interface{}) {
inStructArrData, _ := bson.Marshal(inStructArr)
bson.Unmarshal(inStructArrData, outStructArr) // <-- Error happens here
// What's the right way of accomplishing this?
}
func Main() {
outUsers := &[]User{}
inUsers := []User{User{"A"}, User{"B"}}
DecodeArrData(inUsers, outUsers)
}
When I do this, the error-message I get is: Unsupported document type for unmarshalling: []User
. What's the right way of doing this?
Thanks in advance!