← Torna al blog

Google Search API in Go: Tutorial completo (2026)

Usa una Google Search API in Go (Golang) — richieste net/http, decodifica JSON in struct, retry e paginazione. Esempi copia-incolla con serp.cheap.

1 min di lettura

Interroga Google da Go usando solo la libreria standard, con serp.cheap — oppure aggiungi l’SDK Go ufficiale (go get github.com/serpcheap/serpcheap-go). In entrambi i casi è un semplice endpoint HTTP+JSON.

package main

import (
	"bytes"
	"encoding/json"
	"fmt"
	"net/http"
)

type Result struct {
	Position int    `json:"position"`
	Title    string `json:"title"`
	Link     string `json:"link"`
	Snippet  string `json:"snippet"`
}

type Response struct {
	Organic []Result `json:"organic"`
	Stats   struct {
		Balance int  `json:"balance"`
		Cost    int  `json:"cost"`
		Cached  bool `json:"cached"`
	} `json:"stats"`
}

func search(apiKey, q, gl string, page int) (*Response, error) {
	body, _ := json.Marshal(map[string]any{"q": q, "gl": gl, "page": page})
	req, _ := http.NewRequest("POST", "https://api.serp.cheap/v1/search", bytes.NewReader(body))
	req.Header.Set("X-API-Key", apiKey)
	req.Header.Set("Content-Type", "application/json")

	resp, err := http.DefaultClient.Do(req)
	if err != nil {
		return nil, err
	}
	defer resp.Body.Close()
	if resp.StatusCode != http.StatusOK {
		return nil, fmt.Errorf("search failed: HTTP %d", resp.StatusCode)
	}
	var out Response
	if err := json.NewDecoder(resp.Body).Decode(&out); err != nil {
		return nil, err
	}
	return &out, nil
}

func main() {
	res, err := search("YOUR_KEY", "best electric cars 2026", "us", 1)
	if err != nil {
		panic(err)
	}
	for _, r := range res.Organic {
		fmt.Printf("#%d %s — %s\n", r.Position, r.Title, r.Link)
	}
	fmt.Printf("cost: %d credits, cached: %v\n", res.Stats.Cost, res.Stats.Cached)
}

Per la produzione, aggiungi un retry su 429/5xx con backoff esponenziale e un timeout context.Context.

Avanti

Frequently asked questions

Come chiamo una Google Search API in Go?+

Usa net/http per inviare in POST un body JSON alla SERP API e decodifica la risposta in una struct. Con serp.cheap l'endpoint è https://api.serp.cheap/v1/search con un header X-API-Key.

Mi serve un SDK Go di terze parti?+

No — net/http e encoding/json della libreria standard sono sufficienti. Una SERP API è un semplice endpoint HTTP+JSON.

Ottieni la tua chiave API

Più economico diretto, paga con carta o crypto, senza KYC. O tramite un marketplace.