Go SDK
Official

Go SDK

Official Go SDK for SearchHive APIs. Built with Go's native performance, strong typing, and excellent concurrency support for high-throughput applications.

Performance Optimized
The SearchHive Go SDK is designed for high-performance applications with built-in concurrency patterns, efficient memory usage, and comprehensive error handling.

Quick Start

1. Installation

Install SearchHive Go SDK

cURL
# Install using go mod
go get github.com/searchhive/searchhive-go

# Or with specific version
go get github.com/searchhive/searchhive-go@v1.2.0

# Initialize in your project
go mod init your-project
go get github.com/searchhive/searchhive-go

2. Basic Usage

Your First Search Request

Go
package main

import (
    "context"
    "fmt"
    "log"
    "os"
    
    "github.com/searchhive/searchhive-go"
)

func main() {
    // Initialize client with API key
    client := searchhive.NewClient(&searchhive.Config{
        APIKey: os.Getenv("SEARCHHIVE_API_KEY"),
        // BaseURL: "https://www.searchhive.dev/api/v1", // optional
        // Timeout: 30 * time.Second, // optional, default 30s
        // Retries: 3, // optional, default 3
    })
    
    ctx := context.Background()
    
    // Perform a search
    searchReq := &searchhive.SearchRequest{
        Query:      "latest Go programming trends 2025",
        MaxResults: 10,
    }
    
    result, err := client.Search(ctx, searchReq)
    if err != nil {
        log.Fatalf("Search failed: %v", err)
    }
    
    fmt.Printf("Found %d results\n", len(result.SearchResults))
    fmt.Printf("Credits used: %d\n", result.CreditsUsed)
    fmt.Printf("Credits remaining: %d\n", result.RemainingCredits)
    
    // Display results
    for i, item := range result.SearchResults {
        fmt.Printf("%d. %s\n", i+1, item.Title)
        fmt.Printf("   URL: %s\n", item.Link)
        fmt.Printf("   Snippet: %s\n", item.Snippet)
        fmt.Printf("   Date: %s\n\n", item.Date)
    }
}

Key Features

High Performance

Built with Go's native performance and efficiency, optimized for concurrent operations

Type Safety

Strong typing with comprehensive struct definitions and compile-time error checking

Concurrency Ready

Native goroutine support with built-in rate limiting and worker pools

Memory Safe

Go's garbage collector and memory safety features prevent common bugs

Core Methods

MethodDescriptionReturns
Search(ctx, req)Perform real-time web search with context cancellation support(*SearchResponse, error)
Scrape(ctx, req)Extract content from single webpage with advanced parsing options(*ScrapeResponse, error)
ScrapeBulk(ctx, req)Scrape multiple URLs concurrently with progress tracking([]*ScrapeResponse, error)
Research(ctx, req)AI-powered research across multiple sources with summarization(*ResearchResponse, error)
GetUsage(ctx)Get current API usage statistics and remaining credits(*UsageResponse, error)
ValidateKey(ctx)Validate API key and check account status with timeout(bool, error)

Web Scraping Examples

Extract content from any webpage with advanced parsing and bulk operations:

Web Scraping and Bulk Operations

Go
package main

import (
    "context"
    "fmt"
    "log"
    
    "github.com/searchhive/searchhive-go"
)

func scrapeWebpage() {
    client := searchhive.NewClient(&searchhive.Config{
        APIKey: os.Getenv("SEARCHHIVE_API_KEY"),
    })
    
    ctx := context.Background()
    
    // Single page scraping
    scrapeReq := &searchhive.ScrapeRequest{
        URL: "https://example.com/article",
        ExtractOptions: []string{"title", "text", "links", "images"},
        FollowRedirects: true,
        WaitForSelector: ".content", // optional
        RemoveSelectors: []string{".ads", ".popup"}, // optional
    }
    
    result, err := client.Scrape(ctx, scrapeReq)
    if err != nil {
        log.Fatalf("Scraping failed: %v", err)
    }
    
    fmt.Printf("Page title: %s\n", result.PrimaryContent.Title)
    fmt.Printf("Content length: %d characters\n", len(result.PrimaryContent.Text))
    fmt.Printf("Found links: %d\n", len(result.ExtractedData.Links))
    fmt.Printf("Found images: %d\n", len(result.ExtractedData.Images))
}

func bulkScrape() {
    client := searchhive.NewClient(&searchhive.Config{
        APIKey: os.Getenv("SEARCHHIVE_API_KEY"),
    })
    
    ctx := context.Background()
    
    urls := []string{
        "https://example.com/page1",
        "https://example.com/page2",
        "https://example.com/page3",
    }
    
    bulkReq := &searchhive.BulkScrapeRequest{
        URLs: urls,
        ExtractOptions: []string{"title", "text"},
        Concurrent: 2, // Process 2 URLs concurrently
    }
    
    results, err := client.ScrapeBulk(ctx, bulkReq)
    if err != nil {
        log.Fatalf("Bulk scraping failed: %v", err)
    }
    
    for i, result := range results {
        if result.Success {
            fmt.Printf("%s: %s\n", urls[i], result.Data.PrimaryContent.Title)
        } else {
            fmt.Printf("%s failed: %s\n", urls[i], result.Error)
        }
    }
}

AI-Powered Research

Leverage AI to research topics across multiple sources with automatic summarization:

DeepDive Research API

Go
package main

import (
    "context"
    "fmt"
    "log"
    
    "github.com/searchhive/searchhive-go"
)

func researchTopic() {
    client := searchhive.NewClient(&searchhive.Config{
        APIKey: os.Getenv("SEARCHHIVE_API_KEY"),
    })
    
    ctx := context.Background()
    
    researchReq := &searchhive.ResearchRequest{
        Topic:         "sustainable energy solutions 2025",
        MaxSources:    10,
        IncludeImages: true,
        Language:      "en",
    }
    
    result, err := client.Research(ctx, researchReq)
    if err != nil {
        log.Fatalf("Research failed: %v", err)
    }
    
    fmt.Println("Research summary:")
    fmt.Println(result.Summary)
    
    fmt.Println("\nKey insights:")
    for i, insight := range result.KeyInsights {
        fmt.Printf("%d. %s\n", i+1, insight)
    }
    
    fmt.Println("\nSources analyzed:")
    for i, source := range result.Sources {
        fmt.Printf("%d. %s\n", i+1, source.Title)
        fmt.Printf("   URL: %s\n", source.URL)
        fmt.Printf("   Credibility: %.2f\n\n", source.CredibilityScore)
    }
}

Advanced Configuration

Customize the SDK behavior with advanced configuration options:

Advanced SDK Configuration

Go
package main

import (
    "context"
    "net/http"
    "net/url"
    "time"
    
    "github.com/searchhive/searchhive-go"
)

func advancedConfiguration() {
    // Custom HTTP client with proxy
    proxyURL, _ := url.Parse("http://proxy.example.com:8080")
    transport := &http.Transport{
        Proxy: http.ProxyURL(proxyURL),
    }
    
    httpClient := &http.Client{
        Transport: transport,
        Timeout:   60 * time.Second,
    }
    
    // Advanced client configuration
    client := searchhive.NewClient(&searchhive.Config{
        APIKey:     os.Getenv("SEARCHHIVE_API_KEY"),
        BaseURL:    "https://www.searchhive.dev/api/v1",
        HTTPClient: httpClient,
        Timeout:    60 * time.Second,
        Retries:    5,
        RetryDelay: 1 * time.Second,
        
        // Rate limiting
        RateLimit: &searchhive.RateLimit{
            RequestsPerMinute: 100,
            BurstLimit:        10,
        },
        
        // Custom headers
        Headers: map[string]string{
            "User-Agent":      "MyApp/1.0.0",
            "X-Custom-Header": "value",
        },
        
        // Response format
        ResponseFormat: searchhive.ResponseFormatDetailed,
        
        // Error handling
        ThrowOnError: true,
        
        // Debug logging
        Debug: true,
    })
    
    ctx := context.Background()
    
    // Override settings per request with context
    requestCtx := searchhive.WithRequestConfig(ctx, &searchhive.RequestConfig{
        Timeout: 10 * time.Second,
        Retries: 1,
    })
    
    result, err := client.Search(requestCtx, &searchhive.SearchRequest{
        Query:      "test query",
        MaxResults: 5,
    })
    
    if err != nil {
        log.Printf("Search failed: %v", err)
        return
    }
    
    fmt.Printf("Search completed: %d results\n", len(result.SearchResults))
}

Error Handling

Robust error handling with Go's native error interface and specific error types:

Comprehensive Error Handling

Go
package main

import (
    "context"
    "errors"
    "fmt"
    "log"
    "time"
    
    "github.com/searchhive/searchhive-go"
)

func robustAPICall() (*searchhive.SearchResponse, error) {
    client := searchhive.NewClient(&searchhive.Config{
        APIKey: os.Getenv("SEARCHHIVE_API_KEY"),
    })
    
    ctx := context.Background()
    
    result, err := client.Search(ctx, &searchhive.SearchRequest{
        Query:      "example query",
        MaxResults: 10,
    })
    
    if err != nil {
        // Handle specific error types
        var rateLimitErr *searchhive.RateLimitError
        var authErr *searchhive.AuthenticationError
        var creditsErr *searchhive.InsufficientCreditsError
        var apiErr *searchhive.APIError
        
        switch {
        case errors.As(err, &rateLimitErr):
            fmt.Printf("Rate limited. Retry after %d seconds\n", rateLimitErr.RetryAfter)
            
            // Wait and retry
            time.Sleep(time.Duration(rateLimitErr.RetryAfter) * time.Second)
            return robustAPICall() // Recursive retry
            
        case errors.As(err, &authErr):
            log.Printf("Authentication failed: %v", authErr)
            return nil, fmt.Errorf("check your API key: %w", authErr)
            
        case errors.As(err, &creditsErr):
            log.Printf("No credits remaining: %v", creditsErr)
            return nil, fmt.Errorf("upgrade your plan: %w", creditsErr)
            
        case errors.As(err, &apiErr):
            log.Printf("API error (%d): %v", apiErr.StatusCode, apiErr)
            
            // Log additional context
            log.Printf("Request ID: %s", apiErr.RequestID)
            log.Printf("Response: %s", string(apiErr.Response))
            
            return nil, fmt.Errorf("api error: %w", apiErr)
            
        default:
            log.Printf("Unexpected error: %v", err)
            return nil, fmt.Errorf("unexpected error: %w", err)
        }
    }
    
    return result, nil
}

func main() {
    result, err := robustAPICall()
    if err != nil {
        log.Fatalf("API call failed: %v", err)
    }
    
    fmt.Printf("Success: Found %d results\n", len(result.SearchResults))
}

Concurrency & Performance

Leverage Go's powerful concurrency features for high-performance applications:

Concurrent Processing with Worker Pools

Go
package main

import (
    "context"
    "fmt"
    "sync"
    "time"
    
    "github.com/searchhive/searchhive-go"
)

// Worker pool for concurrent API requests
func concurrentSearch() {
    client := searchhive.NewClient(&searchhive.Config{
        APIKey: os.Getenv("SEARCHHIVE_API_KEY"),
    })
    
    queries := []string{
        "Go programming best practices",
        "microservices architecture patterns",
        "cloud native development",
        "container orchestration",
        "API security guidelines",
    }
    
    // Create channels for work distribution
    jobs := make(chan string, len(queries))
    results := make(chan *searchhive.SearchResponse, len(queries))
    errors := make(chan error, len(queries))
    
    // Start worker goroutines
    numWorkers := 3
    var wg sync.WaitGroup
    
    for i := 0; i < numWorkers; i++ {
        wg.Add(1)
        go func(workerID int) {
            defer wg.Done()
            
            for query := range jobs {
                fmt.Printf("Worker %d processing: %s\n", workerID, query)
                
                ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
                result, err := client.Search(ctx, &searchhive.SearchRequest{
                    Query:      query,
                    MaxResults: 5,
                })
                cancel()
                
                if err != nil {
                    errors <- fmt.Errorf("query '%s' failed: %w", query, err)
                } else {
                    results <- result
                }
            }
        }(i)
    }
    
    // Send jobs to workers
    for _, query := range queries {
        jobs <- query
    }
    close(jobs)
    
    // Wait for completion
    go func() {
        wg.Wait()
        close(results)
        close(errors)
    }()
    
    // Collect results
    var allResults []*searchhive.SearchResponse
    var allErrors []error
    
    for {
        select {
        case result, ok := <-results:
            if !ok {
                results = nil
            } else {
                allResults = append(allResults, result)
            }
        case err, ok := <-errors:
            if !ok {
                errors = nil
            } else {
                allErrors = append(allErrors, err)
            }
        }
        
        if results == nil && errors == nil {
            break
        }
    }
    
    fmt.Printf("\nCompleted: %d successful, %d errors\n", len(allResults), len(allErrors))
    
    for _, err := range allErrors {
        fmt.Printf("Error: %v\n", err)
    }
}

// Rate-limited batch processing
func batchProcessing() {
    client := searchhive.NewClient(&searchhive.Config{
        APIKey: os.Getenv("SEARCHHIVE_API_KEY"),
        RateLimit: &searchhive.RateLimit{
            RequestsPerMinute: 60, // 1 request per second
            BurstLimit:        5,  // Allow 5 requests in burst
        },
    })
    
    urls := []string{
        "https://example.com/page1",
        "https://example.com/page2",
        // ... more URLs
    }
    
    // Process URLs with rate limiting
    rateLimiter := time.NewTicker(1 * time.Second) // 1 request per second
    defer rateLimiter.Stop()
    
    ctx := context.Background()
    
    for i, url := range urls {
        <-rateLimiter.C // Wait for rate limiter
        
        fmt.Printf("Processing %d/%d: %s\n", i+1, len(urls), url)
        
        result, err := client.Scrape(ctx, &searchhive.ScrapeRequest{
            URL:            url,
            ExtractOptions: []string{"title", "text"},
        })
        
        if err != nil {
            fmt.Printf("Failed to scrape %s: %v\n", url, err)
            continue
        }
        
        fmt.Printf("Success: %s\n", result.PrimaryContent.Title)
    }
}

Custom Middleware

Extend functionality with custom middleware for logging, authentication, and more:

Request/Response Middleware

Go
package main

import (
    "context"
    "fmt"
    "log"
    "time"
    
    "github.com/searchhive/searchhive-go"
)

// Custom middleware for request/response processing
type LoggingMiddleware struct{}

func (m *LoggingMiddleware) ProcessRequest(ctx context.Context, req *searchhive.Request) (*searchhive.Request, error) {
    start := time.Now()
    
    // Add custom headers
    req.Headers["X-Request-Start"] = start.Format(time.RFC3339)
    req.Headers["X-Client-Version"] = "go-sdk/1.0.0"
    
    log.Printf("Making request to %s %s", req.Method, req.URL)
    
    // Store start time in context for response middleware
    ctx = context.WithValue(ctx, "start_time", start)
    req.Context = ctx
    
    return req, nil
}

func (m *LoggingMiddleware) ProcessResponse(ctx context.Context, resp *searchhive.Response) (*searchhive.Response, error) {
    if startTime, ok := ctx.Value("start_time").(time.Time); ok {
        duration := time.Since(startTime)
        log.Printf("Request completed in %v (status: %d)", duration, resp.StatusCode)
    }
    
    return resp, nil
}

func (m *LoggingMiddleware) ProcessError(ctx context.Context, err error) error {
    log.Printf("Request failed: %v", err)
    return err // Don't modify the error
}

// Custom authentication middleware
type CustomAuthMiddleware struct {
    Token string
}

func (m *CustomAuthMiddleware) ProcessRequest(ctx context.Context, req *searchhive.Request) (*searchhive.Request, error) {
    // Add custom authentication
    req.Headers["X-Custom-Auth"] = m.generateCustomAuth()
    return req, nil
}

func (m *CustomAuthMiddleware) generateCustomAuth() string {
    // Custom authentication logic
    return fmt.Sprintf("Bearer %s", m.Token)
}

func middlewareExample() {
    client := searchhive.NewClient(&searchhive.Config{
        APIKey: os.Getenv("SEARCHHIVE_API_KEY"),
    })
    
    // Add middleware
    client.Use(&LoggingMiddleware{})
    client.Use(&CustomAuthMiddleware{Token: "custom-token"})
    
    ctx := context.Background()
    
    result, err := client.Search(ctx, &searchhive.SearchRequest{
        Query:      "middleware example",
        MaxResults: 5,
    })
    
    if err != nil {
        log.Printf("Search failed: %v", err)
        return
    }
    
    fmt.Printf("Search completed: %d results\n", len(result.SearchResults))
}

Package Structure

Core Package
github.com/searchhive/searchhive-go
├── client.go
├── config.go
├── types.go
├── errors.go
├── middleware.go
└── ratelimit.go
Import Examples
import "github.com/searchhive/searchhive-go"

Main package

import "github.com/searchhive/searchhive-go/middleware"

Middleware utilities

import "github.com/searchhive/searchhive-go/examples"

Code examples

Environment Support

Go 1.18+

Full support for Go 1.18+ with generics and latest language features.

  • • Generic type constraints
  • • Context-aware operations
  • • Module support
  • • Build constraints
Cross-Platform

Works across all platforms supported by Go runtime.

  • • Linux (amd64, arm64)
  • • macOS (amd64, arm64)
  • • Windows (amd64, 386)
  • • FreeBSD, OpenBSD
Deployment

Optimized for various deployment environments and patterns.

  • • Docker containers
  • • Kubernetes pods
  • • AWS Lambda
  • • Cloud Run
Ready to Build?

Start building with the SearchHive Go SDK and explore our comprehensive API documentation.

Need Help?

Get support, report issues, or contribute to the SearchHive Go SDK development.