Convert Figma logo to code with AI

googleapis logogoogle-cloud-go

Google Cloud Client Libraries for Go.

3,736
1,282
3,736
321

Top Related Projects

This repository is for active development of the Azure SDK for Go. For consumers of the SDK we recommend visiting our public developer docs at:

AWS SDK for the Go programming language.

Terraform Provider for Google Cloud Platform

AWS SDK for the Go programming language.

Quick Overview

googleapis/google-cloud-go is the official Google Cloud client library for Go. It provides idiomatic Go packages for various Google Cloud services, enabling developers to easily integrate Google Cloud functionality into their Go applications. The library is actively maintained by Google and the open-source community.

Pros

  • Comprehensive coverage of Google Cloud services
  • Official support from Google, ensuring reliability and up-to-date features
  • Well-documented with extensive examples and API references
  • Consistent API design across different services

Cons

  • Large dependency footprint due to the extensive service coverage
  • Learning curve for developers new to Google Cloud services
  • Occasional breaking changes between major versions
  • Some services may have limited functionality compared to their REST API counterparts

Code Examples

  1. Initializing a Cloud Storage client:
import (
    "context"
    "cloud.google.com/go/storage"
)

func initializeStorageClient() (*storage.Client, error) {
    ctx := context.Background()
    return storage.NewClient(ctx)
}
  1. Uploading a file to Cloud Storage:
func uploadFile(client *storage.Client, bucketName, objectName, filePath string) error {
    ctx := context.Background()
    f, err := os.Open(filePath)
    if err != nil {
        return err
    }
    defer f.Close()

    wc := client.Bucket(bucketName).Object(objectName).NewWriter(ctx)
    if _, err = io.Copy(wc, f); err != nil {
        return err
    }
    return wc.Close()
}
  1. Querying Firestore:
import (
    "context"
    "cloud.google.com/go/firestore"
    "google.golang.org/api/iterator"
)

func queryFirestore(client *firestore.Client, collection string) error {
    ctx := context.Background()
    iter := client.Collection(collection).Where("age", ">=", 18).Documents(ctx)
    for {
        doc, err := iter.Next()
        if err == iterator.Done {
            break
        }
        if err != nil {
            return err
        }
        fmt.Println(doc.Data())
    }
    return nil
}

Getting Started

To use the Google Cloud Go client library, first install it:

go get cloud.google.com/go

Then, import the desired package in your Go code:

import "cloud.google.com/go/storage"

Authenticate your application using Google Cloud credentials:

// Set the GOOGLE_APPLICATION_CREDENTIALS environment variable
// to the path of your service account key file

Initialize a client for the desired service:

ctx := context.Background()
client, err := storage.NewClient(ctx)
if err != nil {
    // Handle error
}
defer client.Close()

Now you can use the client to interact with the Google Cloud service.

Competitor Comparisons

This repository is for active development of the Azure SDK for Go. For consumers of the SDK we recommend visiting our public developer docs at:

Pros of azure-sdk-for-go

  • More comprehensive coverage of Azure services
  • Better integration with Azure-specific features and authentication mechanisms
  • More frequent updates and active development

Cons of azure-sdk-for-go

  • Steeper learning curve due to more complex API structure
  • Less consistent API design across different services
  • Larger codebase, which may lead to longer compilation times

Code Comparison

azure-sdk-for-go:

import "github.com/Azure/azure-sdk-for-go/services/storage/mgmt/2019-06-01/storage"

client := storage.NewAccountsClient(subscriptionID)
client.Authorizer = authorizer

google-cloud-go:

import "cloud.google.com/go/storage"

client, err := storage.NewClient(ctx)
if err != nil {
    // Handle error
}

The azure-sdk-for-go example shows the creation of a storage account client with explicit authorization, while the google-cloud-go example demonstrates a simpler client creation process with implicit authentication handling.

Both SDKs provide robust support for their respective cloud platforms, but azure-sdk-for-go offers more extensive coverage of Azure services at the cost of increased complexity. google-cloud-go, on the other hand, provides a more streamlined and consistent API across services, making it easier to learn and use for developers new to the Google Cloud Platform.

AWS SDK for the Go programming language.

Pros of aws-sdk-go

  • More comprehensive coverage of AWS services
  • Better documentation and examples
  • Stronger community support and ecosystem

Cons of aws-sdk-go

  • Larger codebase and dependencies
  • Steeper learning curve for beginners
  • Less idiomatic Go code in some areas

Code Comparison

aws-sdk-go:

svc := s3.New(session.New())
input := &s3.ListObjectsInput{
    Bucket: aws.String("my-bucket"),
}
result, err := svc.ListObjects(input)

google-cloud-go:

client, err := storage.NewClient(ctx)
bucket := client.Bucket("my-bucket")
it := bucket.Objects(ctx, nil)
for {
    attrs, err := it.Next()
    // Process object attributes
}

Both SDKs provide Go-native interfaces for interacting with their respective cloud services. aws-sdk-go tends to use more verbose input structs and explicit service clients, while google-cloud-go often employs a more idiomatic Go approach with context-aware methods and iterators.

The aws-sdk-go repository has a larger codebase and covers more services, which can lead to a steeper learning curve but offers more comprehensive functionality. google-cloud-go, on the other hand, provides a more streamlined experience for common Google Cloud operations but may have less extensive coverage of all available services.

Terraform Provider for Google Cloud Platform

Pros of terraform-provider-google

  • Focused on infrastructure-as-code for Google Cloud resources
  • Integrates seamlessly with Terraform ecosystem
  • Provides a declarative approach to managing GCP resources

Cons of terraform-provider-google

  • Limited to infrastructure management, not general-purpose SDK
  • Requires knowledge of Terraform syntax and concepts
  • May have a steeper learning curve for those unfamiliar with IaC

Code Comparison

terraform-provider-google:

resource "google_compute_instance" "default" {
  name         = "test"
  machine_type = "e2-medium"
  zone         = "us-central1-a"
}

google-cloud-go:

instance := &computepb.Instance{
    Name:        proto.String("test"),
    MachineType: proto.String("e2-medium"),
    Zone:        proto.String("us-central1-a"),
}

Summary

terraform-provider-google is specifically designed for managing Google Cloud infrastructure using Terraform, offering a declarative approach to resource management. It excels in infrastructure-as-code scenarios but is limited to this use case. google-cloud-go, on the other hand, is a more general-purpose SDK for interacting with Google Cloud services in Go applications, providing greater flexibility but requiring more programming knowledge. The choice between the two depends on the specific needs of the project and the preferred approach to managing cloud resources.

AWS SDK for the Go programming language.

Pros of aws-sdk-go-v2

  • Modular architecture allowing for smaller dependency footprint
  • Improved performance with reduced memory allocations
  • Better support for AWS-specific features like IAM roles and VPC endpoints

Cons of aws-sdk-go-v2

  • Steeper learning curve due to more complex API design
  • Less comprehensive documentation compared to google-cloud-go
  • Fewer integrated services and features out-of-the-box

Code Comparison

aws-sdk-go-v2:

cfg, err := config.LoadDefaultConfig(context.TODO())
client := s3.NewFromConfig(cfg)
output, err := client.GetObject(context.TODO(), &s3.GetObjectInput{
    Bucket: aws.String("my-bucket"),
    Key:    aws.String("my-object"),
})

google-cloud-go:

client, err := storage.NewClient(context.Background())
bucket := client.Bucket("my-bucket")
obj := bucket.Object("my-object")
reader, err := obj.NewReader(context.Background())

The aws-sdk-go-v2 code demonstrates its more explicit configuration and input structure, while google-cloud-go shows a more straightforward and intuitive API. The AWS SDK requires more setup but offers finer control over the request, whereas the Google Cloud SDK provides a simpler interface for common operations.

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

Google Cloud Client Libraries for Go

Go Reference

Go packages for Google Cloud Platform services.

import "cloud.google.com/go"

To install the packages on your system, do not clone the repo. Instead:

  1. Change to your project directory: cd /my/cloud/project
  2. Get the package you want to use. Some products have their own module, so it's best to go get the package(s) you want to use:
go get cloud.google.com/go/firestore # Replace with the package you want to use.

NOTE: Some of these packages are under development, and may occasionally make backwards-incompatible changes.

Supported APIs

For an updated list of all of our released APIs please see our reference docs.

Go Versions Supported

Note: As of Jan 1, 2025 the Cloud Client Libraries for Go will support the two most-recent major Go releases -- the same policy the Go programming language follows.

Our libraries are compatible with at least the three most recent, major Go releases. They are currently compatible with:

  • Go 1.23
  • Go 1.22
  • Go 1.21

Authorization

By default, each API will use Google Application Default Credentials for authorization credentials used in calling the API endpoints. This will allow your application to run in many environments without requiring explicit configuration.

client, err := storage.NewClient(ctx)

To authorize using a JSON key file, pass option.WithCredentialsFile to the NewClient function of the desired package. For example:

client, err := storage.NewClient(ctx, option.WithCredentialsFile("path/to/keyfile.json"))

You can exert more control over authorization by using the golang.org/x/oauth2 package to create an oauth2.TokenSource. Then pass option.WithTokenSource to the NewClient function:

tokenSource := ...
client, err := storage.NewClient(ctx, option.WithTokenSource(tokenSource))

Contributing

Contributions are welcome. Please, see the CONTRIBUTING document for details.

Please note that this project is released with a Contributor Code of Conduct. By participating in this project you agree to abide by its terms. See Contributor Code of Conduct for more information.

Links