获取数据并返回Todo的对象的json的代码块
func (s *Server) Read(ctx context.Context, req *pb.ReadRequest) (*pb.ReadResponse, error) {
var ida int64
var title string
var desc string
result, err := todo.Read(1)
defer result.Close()
for result.Next() {
result.Scan(&ida, &title, &desc)
break
}
log.Get(ctx).Infof("%d %s %s", ida, title, desc)
todo := &pb.ToDo{Id: ida, Title: title, Description: desc}
resp := &pb.ReadResponse{Api: "1", ToDo: todo}
log.Get(ctx).Infof("todo=%v", todo)
log.Get(ctx).Infof("resp=%v", resp)
return resp, err
}
程序执行结果的部分,todo和resp的结果都OK,write response时提示超时的错误信息。
INFO[0016] todo=id:1 title:"a" description:"b" app_id=localapp device="<nil>" env=dev instance_id=mac.local mobi_app="<nil>" platform="<nil>" trace_id=bc7f7c4690d3ef5 uid="<nil>" version="<nil>"
INFO[0016] resp=api:"1" toDo:<id:1 title:"a" description:"b" > app_id=localapp device="<nil>" env=dev instance_id=mac.local mobi_app="<nil>" platform="<nil>" trace_id=bc7f7c4690d3ef5 uid="<nil>" version="<nil>"
ERRO[0176] twirp error unknown: failed to write response, 0 of 59 bytes written: http: Handler timeout app_id=localapp device="<nil>" env=dev instance_id=mac.local mobi_app="<nil>" platform="<nil>" trace_id=bc7f7c4690d3ef5 uid="<nil>" version="<nil>"
service.twirp.go的代码出现的错误
函数:serveReadForm
if n, err := resp.Write(respBytes); err != nil {
msg := fmt.Sprintf("failed to write response, %d of %d bytes written: %s", n, len(respBytes), err.Error())