英文:
Error function().String undefined (type string has no field or method String)
问题
我尝试运行一个基于Go的gRPC服务的代码,但是在调试过程中无法完全解决问题。以下是我代码的一部分,请随意提出任何代码建议或评论。
func (b *BookInfo) GetRate(ctx context.Context, xr *pb.RateRequest) (*pb.RateResponse, error) {
// b.log.Info("Handle request for GetRate", "Article Name", xr.GetArticleName(), "Article Review", xr.GetArticleReview())
log.Printf("Received:", "Article Name", xr.GetArticleName(), "Article Review", xr.GetArticleReview())
rt, err := b.rating.GetRatings(xr.GetArticleName().String(), xr.GetArticleReview().String())
if err != nil {
return nil, err
}
return &pb.RateResponse{Rating: rt}, nil
}
保存在bookInfo_server
中。
func (x *RateRequest) GetArticleName() string {
if x != nil {
return x.ArticleName
}
return ""
}
func (x *RateRequest) GetArticleReview() string {
if x != nil {
return x.ArticleReview
}
return ""
}
最后一部分:
type RateResponse struct {
state protoimpl.MessageState
sizeCache protoimpl.SizeCache
unknownFields protoimpl.UnknownFields
Rating Ratings `protobuf:"varint,1,opt,name=rating,proto3,enum=Ratings" json:"rating,omitempty"`
}
保存在bookInfo.pb.go
文件中。
错误信息:
bookInfo_server/bookInfo_server.go:28:52: xr.GetArticleName().String undefined (type string has no field or method String)
bookInfo_server/bookInfo_server.go:28:84: xr.GetArticleReview().String undefined (type string has no field or method String)
bookInfo_server/bookInfo_server.go:33:26: cannot use rt (type string) as type bookInfo.Ratings in field value
英文:
I tried to run a code based on gRPC service in Go, but failed to debug all the way to get it done. Here I add my part of my code below. plz feel free to post any code suggestions or comments.
func (b *BookInfo) GetRate(ctx context.Context, xr *pb.RateRequest) (*pb.RateResponse, error) {
// b.log.Info("Handle request for GetRate", "Article Name", xr.GetArticleName(), "Article Review", xr.GetArticleReview())
log.Printf("Received: ", "Article Name", xr.GetArticleName(), "Article Review", xr.GetArticleReview())
rt, err := b.rating.GetRatings(xr.GetArticleName().String(), xr.GetArticleReview().String())
if err != nil {
return nil, err
}
return &pb.RateResponse{Rating: rt}, nil
}
saved in bookInfo_server
func (x *RateRequest) GetArticleName() string {
if x != nil {
return x.ArticleName
}
return ""
}
func (x *RateRequest) GetArticleReview() string {
if x != nil {
return x.ArticleReview
}
return ""
}
for last part
type RateResponse struct {
state protoimpl.MessageState
sizeCache protoimpl.SizeCache
unknownFields protoimpl.UnknownFields
Rating Ratings `protobuf:"varint,1,opt,name=rating,proto3,enum=Ratings" json:"rating,omitempty"`
}
saved in bookInfo.pb.go file
Error:
bookInfo_server/bookInfo_server.go:28:52: xr.GetArticleName().String undefined (type string has no field or method String)
bookInfo_server/bookInfo_server.go:28:84: xr.GetArticleReview().String undefined (type string has no field or method String)
bookInfo_server/bookInfo_server.go:33:26: cannot use rt (type string) as type bookInfo.Ratings in field value
答案1
得分: 1
类型字符串在第一个和第二个错误中没有xr.GetArticleName().string
的方法或字段,
你可以在代码中使用xr.GetArticleName()
来替代。
最后一个错误是关于Ratings
的类型定义。
英文:
The type string has no method or field for the xr.GetArticleName().string
in first & second errors,
Instead of that you can use xr.GetArticleName()
in the code
The last error was for the type definition of the Ratings
.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论