登录
首页 >  Golang >  Go问答

如何获取这个 Go 结构体的访问权限?

来源:stackoverflow

时间:2024-02-17 08:48:24 316浏览 收藏

从现在开始,努力学习吧!本文《如何获取这个 Go 结构体的访问权限?》主要讲解了等等相关知识点,我会在golang学习网中持续更新相关的系列文章,欢迎大家关注并积极留言建议。下面就先一起来看一下本篇正文内容吧,希望能帮到你!

问题内容

我正在尝试理解这个 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
  • 如何访问该结构中列表的第一个元素?

注意,对于最后一个问题,如果我使用 result.clusters[0] (其中 result 属于这种结构类型),我会得到一个指针。例如

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

如何取消引用它?

看看这个:

go 中指针解引用是如何工作的?

您似乎需要一个星号或一个&符号。不清楚你使用哪一个,或者你是把它放在开头还是结尾。


正确答案


您正在与结构标签作斗争。 在您的代码中:

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

clusters 字段有一个类型 ([]*string),声明的其余部分是 2 个结构标记,您应该使用 struct tag. 获取标记的值

以上就是本文的全部内容了,是否有顺利帮助你解决问题?若是能给你带来学习上的帮助,请大家多多支持golang学习网!更多关于Golang的相关知识,也可关注golang学习网公众号。

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>