服务器功能代码:
func (s *service) CreateConsignment(ctx context.Context, req *pb.Consignment, resp *pb.Response) error {
consignment, err := s.repo.Create(req)
if err != nil {
return err
}
resp = &pb.Response{Created: true, Consignment: consignment}
return nil
}
客户代码:
resp, err := client.CreateConsignment(context.Background(),consignment)
if err != nil {
log.Fatalf("create consignment error: %v", err)
}
但是客户的反应是:
{false <nil> [] {} [] 0}
我现在可以使其正常工作的唯一方法是,如果在服务器中设置resp是这样的:
resp.Created = true
resp.Consignment = consignment
这里的问题是:
resp = &pb.Response{Created: true, Consignment: consignment}
为什么设置resp手动起作用,但是却不能将其分配给新的局部变量的地址呢?