Go (Golang) SDK
Official smallpict-go library for high-throughput Go microservices.
The Go SDK signs requests with HMAC-SHA256 without extra allocations and accepts a context.Context on every call.
Installation
Shell
go get github.com/tuxnoob/smallpict-goQuickstart
Go
package main
import ( "context" "fmt" "os" "time"
smallpict "github.com/tuxnoob/smallpict-go")
func main() { client := smallpict.NewClient( os.Getenv("SMALLPICT_API_KEY"), os.Getenv("SMALLPICT_SECRET_KEY"), smallpict.WithTimeout(30*time.Second), smallpict.WithMaxRetries(3), )
ctx := context.Background()
// 1. Lossy Optimization (Default: 70-85% size reduction) fileBytes, err := os.ReadFile("hero.jpg") if err != nil { panic(err) }
lossyResult, err := client.Optimize(ctx, fileBytes, smallpict.OptimizeOptions{ Format: smallpict.FormatAVIF, Quality: 80, MaxWidth: 1920, Lossless: false, // Default: lossy compression }) if err != nil { panic(err) } fmt.Printf("Lossy URL: %s (Saved %.2f%%)\n", lossyResult.URL, lossyResult.SavingsPercentage)
// 2. Lossless Mode (Pixel-perfect for logos & icons) logoBytes, _ := os.ReadFile("logo.png") losslessResult, err := client.Optimize(ctx, logoBytes, smallpict.OptimizeOptions{ Format: smallpict.FormatWebP, Lossless: true, // Activates pixel-perfect lossless compression }) if err != nil { panic(err) } fmt.Printf("Lossless URL: %s (Saved %.2f%%)\n", losslessResult.URL, losslessResult.SavingsPercentage)
// 3. Fetch account quota quota, err := client.GetQuota(ctx) if err == nil { fmt.Printf("Plan: %s, Quota Used: %d / %d bytes\n", quota.Plan, quota.BytesUsed, quota.QuotaLimitBytes) }}