如何访问此 Go 结构?

我试图理解这个Go结构:


type ListClustersOutput struct {

    _ struct{} `type:"structure"`


    // A list of all of the clusters for your account in the specified Region.

    Clusters []*string `locationName:"clusters" type:"list"`


    // The nextToken value to include in a future ListClusters request. When the

    // results of a ListClusters request exceed maxResults, you can use this value

    // to retrieve the next page of results. This value is null when there are no

    // more results to return.

    NextToken *string `locationName:"nextToken" type:"string"`

}

查看文档:https://golangdocs.com/structs-in-golang#defining-a-struct-in-go


它给出了一个例子:


type Fruit struct {

    name string

}

这似乎非常不同。


在更复杂的代码中,我认为这等同于但努力解压缩它。Clusters []*string `locationName:"clusters" type:"list"`name string


我正在努力找出很多东西 - 大多数例子似乎都是指切片。他们为什么使用?type: "list"list

什么是 ?locationName

如何访问该结构中列表的第一个元素?

请注意,对于最后一个问题,如果我使用(where is of this struct类型),我会得到一个指针。例如:result.Clusters[0]result


    fmt.Println("Result: ", result.Clusters[0])

    Result:  0xc000372260

如何取消引用它?


翻过高山走不出你
浏览 124回答 2
2回答

跃然一笑

您正在与结构标记作斗争。在代码中:Clusters []*string `locationName:"clusters" type:"list"`“聚类”字段的类型为 (),声明的其余部分是 2 个结构标记,您应该使用结构标记获取标记的值。[]*string

芜湖不芜

以下是访问它的方法:fmt.Println("Result: ", *result.Clusters[0])
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Go