英文:
How to call google cloud identity APIs from an application
问题
我正在尝试从我的基于Go的应用程序中调用Google Cloud的组成员资格API。我查看了Go的文档这里的文档,但我不确定如何在Go中编写一个简单的代码来实现这一点。
这是我尝试编写的代码:
package main
import (
"context"
"google.golang.org/api/cloudidentity/v1beta1"
)
func main() {
ctx := context.Background()
cloudidentityService, err := cloudidentity.NewService(ctx)
res, err := cloudidentity.Groups.Memberships.Lookup('placeholder-value', 'memberKey.namespace')
}
运行时,我收到错误消息"cannot refer to unexported name cloudidentity.groups"。
我对Google Cloud和Go都是新手。我的任务是查找用户是否是Google Cloud中给定组的一部分。
<details>
<summary>英文:</summary>
I am trying to call google cloud [groups memberships apis][1] from my go based application. I looked at the documentation in go [document here][2] but I am not sure how to write a simple code to do that in go.
here is what I tried to write:
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
package main
import (
"context"
"google.golang.org/api/cloudidentity/v1beta1"
)
func main() {
ctx := context.Background()
cloudidentityService, err := cloudidentity.NewService(ctx)
res, err := cloudidentity.groups.memberships.lookup('placeholder-value', 'memberKey.namespace')
}
<!-- end snippet -->
On running this i get error "cannot refer to unexported name cloudidentity.groups"
I am new to Google cloud as well as golang. My task is to find if the user is part of a given group in google cloud.
[1]: https://cloud.google.com/identity/docs/reference/rest/v1beta1/groups.memberships
[2]: https://pkg.go.dev/google.golang.org/api/cloudidentity/v1beta1#GroupsMembershipsLookupCall
</details>
# 答案1
**得分**: 2
在Go语言中,如果一个名称以大写字母开头,那么它就是可导出的。这意味着你从导入的包中调用的任何方法都必须由这些包导出,并且必须以大写字母开头。这应该解决你的问题。请注意以下两者之间的区别:
- `.Groups.Memberships.Lookup`
- `.groups.memberships.lookup`
```go
package main
import (
"context"
"google.golang.org/api/cloudidentity/v1"
)
func main() {
ctx := context.Background()
svc, err := cloudidentity.NewService(ctx)
if err != nil {
panic(err)
}
res, err := svc.Groups.Memberships.Lookup("...").Context(ctx).Do()
if err != nil {
panic(err)
}
_ = res
}
希望对你有帮助!
英文:
In Go, a name is exported if it begins with a capital letter. This means that any methods you call from imported packages must be exported by those packages and therefore must start with capital letters. This should resolve your problem. Note the difference between
.Groups.Memberships.Lookup
.groups.memberships.lookup
package main
import (
"context"
"google.golang.org/api/cloudidentity/v1"
)
func main() {
ctx := context.Background()
svc, err := cloudidentity.NewService(ctx)
if err != nil {
panic(err)
}
res, err := svc.Groups.Memberships.Lookup("...").Context(ctx).Do()
if err != nil {
panic(err)
}
_ = res
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论