错误函数().String未定义(类型string没有字段或方法String)

huangapple go评论92阅读模式
英文:

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.

huangapple
  • 本文由 发表于 2021年10月7日 02:23:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/69470841.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定