SDK for Go Cloud Shell Quick Start

This quick start shows you how to quickly get started running sample code with the Oracle Cloud Infrastructure SDK for Go using Cloud Shell.

The OCI Go SDK is pre-installed in the Cloud Shell environment and included in the $GOPATH.

  1. Sign in to the Console.
  2. Click the Cloud Shell icon in the Console header. Note that Cloud Shell runs commands against the region selected in the Console's Region selection menu when Cloud Shell was started.
  3. Create a file named main.go with the following code, which lists the availability domains in the tenancy:
    package main
      
    import (
        "context"
        "fmt"
     
        "github.com/oracle/oci-go-sdk/common"
        "github.com/oracle/oci-go-sdk/identity"
    )
      
    func main() {
        c, err := identity.NewIdentityClientWithConfigurationProvider(common.DefaultConfigProvider())
        if err != nil {
            fmt.Println("Error:", err)
            return
        }
      
        // The OCID of the tenancy containing the compartment.
        tenancyID, err := common.DefaultConfigProvider().TenancyOCID()
        if err != nil {
            fmt.Println("Error:", err)
            return
            }
      
        request := identity.ListAvailabilityDomainsRequest{
            CompartmentId: &tenancyID,
        }
      
        r, err := c.ListAvailabilityDomains(context.Background(), request)
        if err != nil {
            fmt.Println("Error:", err)
            return
        }
      
        fmt.Printf("List of available domains: %v", r.Items)
        return
    }
  4. Add the following environment variable:
    export GO111MODULE=auto
  5. Run the example:
    go run main.go