我已经实现了一个非常简单的解码方法(gob.Decoder现在使用)——这适用于单个响应——它甚至适用于切片,但我需要实现一个 DecodeMany 方法,它能够解码一组单独的响应(不是一片)。
工作解码方法:
var v MyType
_ = Decode(&v)
...
func Decode(v interface{}) error {
buf, _ := DoSomething() // func DoSomething() ([]byte, error)
// error handling omitted for brevity
return gob.NewDecoder(bytes.NewReader(buf)).Decode(v)
}
我试图为 DecodeMany 方法做的是处理不一定是切片的响应:
var vv []MyType
_ = DecodeMany(&vv)
...
func DecodeMany(vv []interface{}) error {
for _, g := range DoSomething() { // func DoSomething() []struct{Buf []bytes}
// Use g.Buf as an individual "interface{}"
// want something like:
var v interface{} /* Somehow create instance of single vv type? */
_ = gob.NewDecoder(bytes.NewReader(g.Buf)).Decode(v)
vv = append(vv, v)
}
return
}
除了不编译上面还有错误:
不能在 DecodeMany 的参数中使用 &vv(类型 *[]MyType 的值)作为类型 []interface{}
宝慕林4294392
相关分类