Search code examples
gogoogle-cloud-platformlogicgoogle-cloud-identity

How to call google cloud identity APIs from an application


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:

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')
 }

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


Solution

  • 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
    }