问:刚开始学习grpc,遇到问题请咨询机型:
type User struct {
Id int64 `json:"id"`
Nickname string `json:"nickname"`
}
func Users()[]*User {
var rows []*User
db.Find(&rows)
return rows
}
原型
type User struct {
Id int64 `protobuf:"varint,1,opt,name=id,proto3" json:"id,omitempty"`
Nickname string `protobuf:"bytes,2,opt,name=nickname,proto3" json:"nickname,omitempty"`
XXX_NoUnkeyedLiteral struct{} `json:"-"`
XXX_unrecognized []byte `json:"-"`
XXX_sizecache int32 `json:"-"`
}
响应
type UserResponse struct {
Message string `protobuf:"bytes,1,opt,name=message,proto3" json:"message,omitempty"`
Data []*User `protobuf:"bytes,2,rep,name=data,proto3" json:"data,omitempty"`
XXX_NoUnkeyedLiteral struct{} `json:"-"`
XXX_unrecognized []byte `json:"-"`
XXX_sizecache int32 `json:"-"`
}
userServer
func (c *userServer) Users(ctx context.Context, request *pb.UserRequest, response *pb.UserResponse) error {
users := model.Users()
//Here we want to add []* model.User Return, but Pb generated []* pb.User
//How to solve this problem?
}
答:func (c *userServer) Users(ctx context.Context, request *pb.UserRequest, response *pb.UserResponse) error {
users := model.Users()
for _,u := range users {
pb.Data = append(pb.Data, &pb.User{Id:u.Id,Nickname:u.Nickname})
}
//Here we want to add []* model.User Return, but Pb generated []* pb.User
//How to solve this problem?
}