golang 错误: code:8, msg:MarshalFail
func getCandidatePoolResult(ctx *test.Context) ([]proto_rec_match.Item, error) {
req := proto_rec_match.GetCandidatePoolReq{}
rsp := proto_rec_match.GetCandidatePoolRsp{}
req.MaxNum = 500
stCallDesc := tesc.CallDesc{
LocalServiceName: "go_song_match",
ServiceName: proto_rec_match.CMD_GET_CANDIDATE_POOL,
Timeout: 1000 * time.Millisecond,
}
stClient := tesc.New(stCallDesc, ctx.GetAuthInfo(), req, rsp)
if err := stClient.Do(ctx); err != nil {
iRet := int32(stClient.GetErrCode())
ctx.SetResult(iRet)
return nil, fmt.Errorf("get pool error: +%v", err)
}
return rsp.CandidatePool, nil
}
异常报错: code:8, msg:MarshalFail
原因: req 和 rsp 需要传递引用,将
stClient := tesc.New(stCallDesc, ctx.GetAuthInfo(), req, rsp)
改成
stClient := tesc.New(stCallDesc, ctx.GetAuthInfo(), &req, &rsp)
tesc包底层代码New方法的解析:
func New(desc CallDesc, authInfo th.AuthInfo, reqBody, rspBody interface{})
interface{} 表示的是空接口类型。在go 当中任何值都满足这个接口。 所以空接口可以用来保存任何值,也可以从空接口中取出原值。
所以,在上述代码当中传递req 和 &req 形参 interface{} 都能接收。在执行 stClient.Do(ctx)的时候,对 pb 进行 Marshal 的时候抛出了异常。
具体抛出异常的位置在哪里,也不是很清楚。底层代码没有深入研究。等以后有机会再研究一下protobuffer的 Marshal 机制。
博客主要讲述了Golang开发中出现的错误,代码为8,信息是MarshalFail。分析了异常报错原因,即req和rsp需传递引用。还对包底层代码方法进行了解析,提到空接口可保存任何值。最后表示后续有机会再研究protobuffer的Marshal机制。
1832

被折叠的 条评论
为什么被折叠?



