为了账号安全,请及时绑定邮箱和手机立即绑定

未定义(类型 []DataReponse 没有字段或方法)

未定义(类型 []DataReponse 没有字段或方法)

Go
ITMISS 2022-01-17 16:52:35
我正在尝试编译以下代码    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 undefined (type []DataReponse has no field or method Commenter)这里收到错误是我的结构//DataReponse is the structure of the responsetype 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"`}我如何使用结果值?
查看完整描述

2 回答

?
慕田峪9158850

TA贡献1794条经验 获得超8个赞

正如 twotwotwo 所解释的,您正在使用切片。像这样循环它。


    for _, data := range result {

       if data.Commenter == "teacher" {

         ...   

        }

    }


查看完整回答
反对 回复 2022-01-17
?
慕斯709654

TA贡献1840条经验 获得超5个赞

[]DataReponseDataReponses 的一部分,即可能有多个响应(或没有响应)。您可以使用for 循环为每个struct DataReponse返回的代码运行一些代码。我会考虑做巡回演出。(另外,也许您的意思是 DataResponse,带有两个“s”,但当然,只要您始终使用相同的名称,这对 Go 并不重要。)


查看完整回答
反对 回复 2022-01-17
  • 2 回答
  • 0 关注
  • 174 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号