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

我正在尝试编译以下代码


    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 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"`

}

我如何使用结果值?


ITMISS
浏览 175回答 2
2回答

慕田峪9158850

正如 twotwotwo 所解释的,您正在使用切片。像这样循环它。    for _, data := range result {       if data.Commenter == "teacher" {         ...           }    }

慕斯709654

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

相关分类

Go