Skip to content
smallPict

Go (Golang) SDK

Thư viện smallpict-go chính thức cho các microservices Go hiệu năng và thông lượng cao.

SDK Go chính thức của SmallPict mang lại trải nghiệm chuẩn mực, cơ chế tính toán chữ ký HMAC không cấp phát bộ nhớ và tích hợp với Context trong HTTP.


Cài đặt

Shell
go get github.com/tuxnoob/smallpict-go

Ví dụ bắt đầu nhanh

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. Tối ưu hóa mảng byte hình ảnh	fileBytes, err := os.ReadFile("hero.png")	if err != nil {		panic(err)	}
	result, err := client.Optimize(ctx, fileBytes, smallpict.OptimizeOptions{		Format:   "avif",		Quality:  85,		MaxWidth: 1920,	})	if err != nil {		panic(err)	}
	fmt.Printf("URL tối ưu: %s (Tiết kiệm %.2f%%)\n", result.URL, result.SavingsPercentage)
	// 2. Lấy thông tin hạn ngạch tài khoản	quota, err := client.GetQuota(ctx)	if err == nil {		fmt.Printf("Gói: %s, Đã dùng: %d / %d bytes\n", quota.Plan, quota.BytesUsed, quota.QuotaLimitBytes)	}}