未定义(类型[] DataReponse没有字段或方法)
I'm trying to compile the following piece of code
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,
}
I am receiving the error result.Commenter undefined (type []DataReponse has no field or method Commenter)
here is my struct
//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"`
}
how can i use result values ?
我正在尝试编译以下代码 p>
var result [] DataReponse
if result.Commenter ==“ teacher” {
sender = models.CommentUser {
名称:result.UserName,
Email:result.UserEmail,
}
} else {
发件人=模型。CommentUser{
名称:result.ChildName,
电子邮件:result.ChildEmail,
}
code> pre>
我收到错误 result.Commenter未定义(类型[] DataReponse没有字段或方法Commenter) code>这是我的结构 p>
// DataReponse是响应的结构\ ntype DataReponse struct {
CommentText字符串`json:“ comment_text”`
Commenter字符串`json:“ commenter”`
ChildEmail core.NullString`json:“ child_email”`
ChildName core.NullString`json:“ child_name “`
UserName core.NullString`json:” user_name“`
UserEmail core.N ullString`json:“ user_email”`
}
code> pre>
如何使用结果值? p>
div>
[]DataReponse
is a slice of DataReponse
s, i.e., there may be more than one response (or none). You could use a for loop to run some code for each struct DataReponse
returned. I would consider doing the tour. (Also, perhaps you meant DataResponse, with two "s"es, but of course that doesn't matter to Go as long as you use the same name consistently.)
As explained by twotwotwo you are working with a slice. Loop over it like so.
for _, data := range result {
if data.Commenter == "teacher" {
...
}
}