구조체 배열이 interface{}
매개 변수로 전달 될 때 Unmarshal
bson 바이트 [] 데이터를 구조체 배열에 저장하는 가장 좋은 방법은 무엇입니까?이동에서 bson byte [] 데이터를 구조체 배열에 Unmarshal하는 방법?
데모 목적으로 다음 코드에서 데이터 형식을 얻으려면 inStructArr
에 bson.Marshal()
을 사용합니다. 이것은 bson.Unmarshal(...)
을 사용하여 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)
}
, 내가 오류 메시지는 다음과 같습니다 Unsupported document type for unmarshalling: []User
. 이 작업을 수행하는 올바른 방법은 무엇입니까?
미리 감사드립니다.