← Retour au blog

API Google Search en Go : tutoriel complet (2026)

Utilisez une API Google Search en Go (Golang) — requêtes net/http, décodage JSON vers des structs, retries et pagination. Exemples prêts à copier avec serp.cheap.

1 min de lecture

Interrogez Google depuis Go avec uniquement la bibliothèque standard, en utilisant serp.cheap — ou installez le SDK Go officiel (go get github.com/serpcheap/serpcheap-go). C’est un simple endpoint HTTP+JSON dans les deux cas.

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)
}

En production, ajoutez un retry sur 429/5xx avec un backoff exponentiel et un timeout context.Context.

Suite

Questions fréquentes

Comment appeler une API Google Search en Go ?+

Utilisez net/http pour envoyer un corps JSON en POST à l'API SERP et décodez la réponse dans une struct. Avec serp.cheap, l'endpoint est https://api.serp.cheap/v1/search avec un en-tête X-API-Key.

Ai-je besoin d'un SDK Go tiers ?+

Non — net/http et encoding/json de la bibliothèque standard suffisent. Une API SERP est un simple endpoint HTTP+JSON.

Obtenez votre clé API

Le moins cher en direct, payez par carte ou crypto, sans KYC. Ou via une marketplace.