Azure Cosmos DB Go examples
APPLIES TO: NoSQL
Sample solutions that do CRUD operations and other common operations on Azure Cosmos DB resources are included in the azure-documentdb-go GitHub repository. This article provides:
- Links to the tasks in each of the Go example project files.
- Links to the related API reference content.
Prerequisites
- An Azure Cosmos DB Account. Your options are:
- Within an Azure active subscription:
- Create an Azure free Account or use your existing subscription
- Visual Studio Monthly Credits
- Azure Cosmos DB Free Tier
- Without an Azure active subscription:
- Try Azure Cosmos DB for free, a tests environment that lasts for 30 days.
- Azure Cosmos DB Emulator
- Within an Azure active subscription:
- go installed on your computer, and a working knowledge of Go.
- Visual Studio Code.
- The Go extension for Visual Studio Code.
- Git.
- Azure Cosmos DB for NoSQL SDK for Go
Database examples
To learn about the Azure Cosmos DB databases before running the following samples, see Working with databases, containers, and items conceptual article.
Task | API reference |
---|---|
Create a database | Client.CreateDatabase |
Read a database by ID | Client.NewDatabase |
Delete a database | DatabaseClient.Delete |
Container examples
To learn about the Azure Cosmos DB collections before running the following samples, see Working with databases, containers, and items conceptual article.
Task | API reference |
---|---|
Create a container | DatabaseClient.CreateContainer |
Get a container by its ID | DatabaseClient.NewContainer |
Delete a container | ContainerClient.Delete |
Item examples
The cosmos_container.go Go sample shows how to do the following tasks. To learn about the Azure Cosmos DB documents before running the following samples, see Working with databases, containers, and items conceptual article.
Task | API reference |
---|---|
Create a item in a container | ContainerClient.CreateItem |
Read an item by its ID | ContainerClient.ReadItem |
Query items | ContainerClient.NewQueryItemsPager |
Replace an item | ContainerClient.ReplaceItem |
Upsert an item | ContainerClient.UpsertIitem |
Delete an item | ContainerClient.DeleteItem |
Next steps
Trying to do capacity planning for a migration to Azure Cosmos DB? You can use information about your existing database cluster for capacity planning.
- If all you know is the number of vcores and servers in your existing database cluster, read about estimating request units using vCores or vCPUs
- If you know typical request rates for your current database workload, read about estimating request units using Azure Cosmos DB capacity planner