|
| 1 | +--- |
| 2 | +title: API for NoSQL Go examples for Azure Cosmos DB |
| 3 | +description: Find Go examples on GitHub for common tasks in Azure Cosmos DB, including CRUD operations. |
| 4 | +author: soferreira |
| 5 | +ms.service: cosmos-db |
| 6 | +ms.subservice: nosql |
| 7 | +ms.devlang: go |
| 8 | +ms.topic: sample |
| 9 | +ms.date: 10/17/2022 |
| 10 | +ms.author: soferreira |
| 11 | +--- |
| 12 | +# Azure Cosmos DB Go examples |
| 13 | +[!INCLUDE[NoSQL](../includes/appliesto-nosql.md)] |
| 14 | + |
| 15 | +> [!div class="op_single_selector"] |
| 16 | +> * [.NET SDK Examples](samples-dotnet.md) |
| 17 | +> * [Java V4 SDK Examples](samples-java.md) |
| 18 | +> * [Spring Data V3 SDK Examples](samples-java-spring-data.md) |
| 19 | +> * [Node.js Examples](samples-nodejs.md) |
| 20 | +> * [Python Examples](samples-python.md) |
| 21 | +> * [Go Examples](samples-go.md) |
| 22 | +> * [Azure Code Sample Gallery](https://azure.microsoft.com/resources/samples/?sort=0&service=cosmos-db) |
| 23 | +
|
| 24 | +Sample solutions that do CRUD operations and other common operations on Azure Cosmos DB resources are included in the [azure-documentdb-go](https://github.com/Azure/azure-sdk-for-go) GitHub repository. This article provides: |
| 25 | + |
| 26 | +* Links to the tasks in each of the Go example project files. |
| 27 | +* Links to the related API reference content. |
| 28 | + |
| 29 | +## Prerequisites |
| 30 | + |
| 31 | +- An Azure Cosmos DB Account. Your options are: |
| 32 | + * Within an Azure active subscription: |
| 33 | + * [Create an Azure free Account](https://azure.microsoft.com/free) or use your existing subscription |
| 34 | + * [Visual Studio Monthly Credits](https://azure.microsoft.com/pricing/member-offers/credit-for-visual-studio-subscribers) |
| 35 | + * [Azure Cosmos DB Free Tier](../free-tier.md) |
| 36 | + * Without an Azure active subscription: |
| 37 | + * [Try Azure Cosmos DB for free](../try-free.md), a tests environment that lasts for 30 days. |
| 38 | + * [Azure Cosmos DB Emulator](https://aka.ms/cosmosdb-emulator) |
| 39 | +- [go](https://go.dev/) installed on your computer, and a working knowledge of Go. |
| 40 | +- [Visual Studio Code](https://code.visualstudio.com/). |
| 41 | +- The [Go extension for Visual Studio Code](https://marketplace.visualstudio.com/items?itemName=golang.Go). |
| 42 | +- [Git](https://www.git-scm.com/downloads). |
| 43 | +- [Azure Cosmos DB for NoSQL SDK for Go](https://github.com/Azure/azure-sdk-for-go/tree/main/sdk/data/azcosmos) |
| 44 | + |
| 45 | +## Database examples |
| 46 | + |
| 47 | +The [cosmos_client.go](https://github.com/Azure/azure-sdk-for-go/blob/sdk/data/azcosmos/v0.3.2/sdk/data/azcosmos/cosmos_client.go) Go sample shows how to do the following tasks. To learn about the Azure Cosmos DB databases before running the following samples, see [Working with databases, containers, and items](../account-databases-containers-items.md) conceptual article. |
| 48 | + |
| 49 | +| Task | API reference | |
| 50 | +| --- | --- | |
| 51 | +| [Create a database](https://github.com/Azure/azure-sdk-for-go/blob/sdk/data/azcosmos/v0.3.2/sdk/data/azcosmos/cosmos_client.go#L151) |Client.CreateDatabase | |
| 52 | +| [Read a database by ID](https://github.com/Azure/azure-sdk-for-go/blob/sdk/data/azcosmos/v0.3.2/sdk/data/azcosmos/cosmos_client.go#L119) |Client.NewDatabase| |
| 53 | +| [Delete a database](https://github.com/Azure/azure-sdk-for-go/blob/sdk/data/azcosmos/v0.3.2/sdk/data/azcosmos/cosmos_database.go#L155) |DatabaseClient.Delete| |
| 54 | + |
| 55 | +## Container examples |
| 56 | + |
| 57 | +The [cosmos_database.go](https://github.com/Azure/azure-sdk-for-go/blob/sdk/data/azcosmos/v0.3.2/sdk/data/azcosmos/cosmos_database.go) Go sample shows how to do the following tasks. To learn about the Azure Cosmos DB collections before running the following samples, see [Working with databases, containers, and items](../account-databases-containers-items.md) conceptual article. |
| 58 | + |
| 59 | +| Task | API reference | |
| 60 | +| --- | --- | |
| 61 | +| [Create a container](https://github.com/Azure/azure-sdk-for-go/blob/sdk/data/azcosmos/v0.3.2/sdk/data/azcosmos/cosmos_database.go#L47) |DatabaseClient.CreateContainer | |
| 62 | +| [Get a container by its ID](https://github.com/Azure/azure-sdk-for-go/blob/sdk/data/azcosmos/v0.3.2/sdk/data/azcosmos/cosmos_database.go#L35) |DatabaseClient.NewContainer | |
| 63 | +| [Delete a container](https://github.com/Azure/azure-sdk-for-go/blob/sdk/data/azcosmos/v0.3.2/sdk/data/azcosmos/cosmos_container.go#L109) |ContainerClient.Delete | |
| 64 | + |
| 65 | +## Item examples |
| 66 | + |
| 67 | +The [cosmos_container.go](https://github.com/Azure/azure-sdk-for-go/blob/sdk/data/azcosmos/v0.3.2/sdk/data/azcosmos/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](../account-databases-containers-items.md) conceptual article. |
| 68 | + |
| 69 | +| Task | API reference | |
| 70 | +| --- | --- | |
| 71 | +| [Create a item in a container](https://github.com/Azure/azure-sdk-for-go/blob/sdk/data/azcosmos/v0.3.2/sdk/data/azcosmos/cosmos_container.go#L184) |ContainerClient.CreateItem | |
| 72 | +| [Read an item by its ID](https://github.com/Azure/azure-sdk-for-go/blob/sdk/data/azcosmos/v0.3.2/sdk/data/azcosmos/cosmos_container.go#L325) |ContainerClient.ReadItem | |
| 73 | +| [Query items](https://github.com/Azure/azure-sdk-for-go/blob/sdk/data/azcosmos/v0.3.2/sdk/data/azcosmos/cosmos_container.go#L410) |ContainerClient.NewQueryItemsPager | |
| 74 | +| [Replace an item](https://github.com/Azure/azure-sdk-for-go/blob/sdk/data/azcosmos/v0.3.2/sdk/data/azcosmos/cosmos_container.go#L279) |ContainerClient.ReplaceItem | |
| 75 | +| [Upsert an item](https://github.com/Azure/azure-sdk-for-go/blob/sdk/data/azcosmos/v0.3.2/sdk/data/azcosmos/cosmos_container.go#L229) |ContainerClient.UpsertIitem | |
| 76 | +| [Delete an item](https://github.com/Azure/azure-sdk-for-go/blob/sdk/data/azcosmos/v0.3.2/sdk/data/azcosmos/cosmos_container.go#L366) |ContainerClient.DeleteItem | |
| 77 | + |
| 78 | + |
| 79 | +## Next steps |
| 80 | + |
| 81 | +Trying to do capacity planning for a migration to Azure Cosmos DB? You can use information about your existing database cluster for capacity planning. |
| 82 | +* 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](../convert-vcore-to-request-unit.md) |
| 83 | +* If you know typical request rates for your current database workload, read about [estimating request units using Azure Cosmos DB capacity planner](estimate-ru-with-capacity-planner.md) |
0 commit comments