Interact with Azure Table Storage efficiently using Go.
The aztable
module provides a robust and idiomatic Go client for interacting with Azure Table Storage. It simplifies operations such as creating, querying, inserting, updating, and deleting entities in your tables.
To use the aztable
module, first ensure you have the Azure SDK for Go installed.
go get github.com/Azure/azure-sdk-for-go/sdk/data/aztable
You can authenticate using various methods, including connection strings, Azure Identity, or service principals. Here's an example using a connection string:
Example:
package main
import (
"context"
"fmt"
"log"
"time"
"github.com/Azure/azure-sdk-for-go/sdk/data/aztable"
)
func main() {
connectionString := "YOUR_AZURE_TABLE_STORAGE_CONNECTION_STRING"
tableName := "mytable"
client, err := aztable.NewClientFromConnectionString(connectionString, tableName, nil)
if err != nil {
log.Fatalf("failed to create client: %v", err)
}
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
// Example: Insert an entity
entity := map[string]interface{}{
"PartitionKey": "user1",
"RowKey": "profile",
"Name": "Alice",
"Age": 30,
}
_, err = client.InsertEntity(ctx, aztable.Entity(entity), nil)
if err != nil {
log.Fatalf("failed to insert entity: %v", err)
}
fmt.Println("Entity inserted successfully!")
// Example: Query entities
queryFilter := "Name eq 'Alice'"
pager := client.QueryEntities(ctx, &aztable.TableClientQueryEntitiesOptions{
Filter: &queryFilter,
})
fmt.Println("Querying entities:")
for pager.More() {
page, err := pager.NextPage(ctx)
if err != nil {
log.Fatalf("failed to advance page: %v", err)
}
for _, entity := range page.Entities {
fmt.Printf(" Found Entity: %+v\n", entity)
}
}
}
aztable.NewClientFromConnectionString
aztable.NewClientFromTokenCredential
aztable.NewClientWithSharedKeyCredential
For detailed information, examples, and advanced usage, please refer to the official documentation and the GitHub repository: