Go 未定义(类型[]DataResponse没有字段或方法)
我正在尝试编译下面的代码Go 未定义(类型[]DataResponse没有字段或方法),go,Go,我正在尝试编译下面的代码 var result []DataReponse if result.Commenter == "teacher"{ sender = models.CommentUser{ Name:result.UserName, Email:result.UserEmail, } }else{ sender = models.CommentUser{
var result []DataReponse
if result.Commenter == "teacher"{
sender = models.CommentUser{
Name:result.UserName,
Email:result.UserEmail,
}
}else{
sender = models.CommentUser{
Name:result.ChildName,
Email:result.ChildEmail,
}
我收到错误result.Commenter未定义(type[]DataReponse没有字段或方法Commenter)
这是我的结构
//DataReponse is the structure of the response
type DataReponse struct{
CommentText string `json:"comment_text"`
Commenter string `json:"commenter"`
ChildEmail core.NullString `json:"child_email"`
ChildName core.NullString `json:"child_name"`
UserName core.NullString `json:"user_name"`
UserEmail core.NullString `json:"user_email"`
}
如何使用结果值?
[]DataReponse
是DataReponse
的一部分,即可能有多个响应(或无响应)。对于返回的每个struct DataReponse
,可以使用运行一些代码。我会考虑去做。(另外,您可能指的是DataResponse,有两个“s”,但只要您始终使用相同的名称,当然这并不重要。)[]DataResponse
是DataResponse
的一部分,即可能有多个响应(或没有)。对于返回的每个struct DataReponse
,可以使用运行一些代码。我会考虑去做。(另外,您可能指的是带有两个“s”的DataResponse,但当然,只要您始终使用相同的名称,这并不重要。)正如TwoTwo所解释的,您使用的是一个切片。像这样绕过去
for _, data := range result {
if data.Commenter == "teacher" {
...
}
}
正如Two所解释的,您正在处理一个切片。像这样绕过去
for _, data := range result {
if data.Commenter == "teacher" {
...
}
}