Go SDK Documentation

Go SDK Examples

Explore practical examples demonstrating how to use the Go SDK for various common tasks. These examples are designed to be easily understandable and adaptable to your specific needs.

Basic API Call

A simple example showing how to make a GET request to retrieve data.


package main

import (
	"fmt"
	"log"
	"your_sdk_package/client" // Replace with your actual SDK package
)

func main() {
	// Initialize the client (assuming default configuration or from env vars)
	c, err := client.NewClient()
	if err != nil {
		log.Fatalf("Failed to create client: %v", err)
	}

	// Example: Fetching a list of items
	items, err := c.GetItems(client.NewListItemsOptions())
	if err != nil {
		log.Fatalf("Failed to get items: %v", err)
	}

	fmt.Printf("Successfully fetched %d items:\n", len(items))
	for _, item := range items {
		fmt.Printf("- ID: %s, Name: %s\n", item.ID, item.Name)
	}
}
                

Creating a New Resource

Demonstrates how to create a new resource using a POST request.


package main

import (
	"fmt"
	"log"
	"your_sdk_package/client" // Replace with your actual SDK package
)

func main() {
	c, err := client.NewClient()
	if err != nil {
		log.Fatalf("Failed to create client: %v", err)
	}

	newItemData := client.ItemInput{
		Name:        "New Gadget",
		Description: "A very useful gadget.",
		Price:       99.99,
	}

	createdItem, err := c.CreateItem(newItemData)
	if err != nil {
		log.Fatalf("Failed to create item: %v", err)
	}

	fmt.Printf("Successfully created item: %+v\n", createdItem)
}
                

Handling Errors

Illustrates how to gracefully handle potential errors during API interactions.


package main

import (
	"fmt"
	"log"
	"your_sdk_package/client" // Replace with your actual SDK package
)

func main() {
	c, err := client.NewClient()
	if err != nil {
		log.Fatalf("Failed to create client: %v", err)
	}

	// Example of an operation that might fail (e.g., invalid ID)
	_, err = c.GetItem("invalid-item-id")
	if err != nil {
		// Check for specific error types if your SDK provides them
		if apiErr, ok := err.(client.APIError); ok {
			log.Printf("API Error occurred: Status=%d, Message=%s\n", apiErr.StatusCode, apiErr.Message)
		} else {
			log.Printf("An unexpected error occurred: %v\n", err)
		}
		// You might want to return an error or handle it differently
		return
	}

	fmt.Println("Operation succeeded (this won't be printed in case of error).")
}
                

Using Request Options

Shows how to customize requests using option parameters for filtering, sorting, or pagination.


package main

import (
	"fmt"
	"log"
	"your_sdk_package/client" // Replace with your actual SDK package
)

func main() {
	c, err := client.NewClient()
	if err != nil {
		log.Fatalf("Failed to create client: %v", err)
	}

	// Example: Fetching items with pagination and filtering
	options := client.NewListItemsOptions().
		WithPageSize(50).
		WithPageToken("next-page-token-abc").
		WithFilter("status=active")

	items, err := c.GetItems(options)
	if err != nil {
		log.Fatalf("Failed to get items with options: %v", err)
	}

	fmt.Printf("Fetched %d items with specific options.\n", len(items))
}