根据Examiner ,Gmail API 错误响应结构包括一个代码字段
_, err := gmailService.Users.Messages.Send("me", &gMsg).Do()
t := reflect.TypeOf(err)
examiner(t,0)
回复
Type is and kind is ptr
Contained type:
Type is Error and kind is struct
Field 1 name is Code type is int and kind is int
...
我可以使用字段索引成功获取错误代码,但这更晦涩(因此更难维护)并且感觉更脆弱:
errReflectElem := reflect.ValueOf(err).Elem()
fmt.Printf("err.Code %v\n",errReflectElem.Field(0).Interface())
看来我也应该能够根据这个示例得到如下错误代码:
fmt.Println("Code: %v",err.Code)
但是,这会产生构建错误:
err.Code undefined (type error has no field or method Code)
我错过了什么?
守候你守候我
相关分类