← Volver al blog

API de Google Search en Go: Tutorial completo (2026)

Usa una API de Google Search en Go (Golang): solicitudes net/http, decodificación de JSON en structs, reintentos y paginación. Ejemplos listos con serp.cheap.

1 min de lectura

Consulta Google desde Go con solo la biblioteca estándar, usando serp.cheap — o incorpora el SDK oficial de Go (go get github.com/serpcheap/serpcheap-go). En cualquier caso es un simple 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)
}

Para producción, añade un reintento en 429/5xx con backoff exponencial y un timeout con context.Context.

Siguiente

Preguntas frecuentes

¿Cómo llamo a una API de Google Search en Go?+

Usa net/http para hacer un POST con un cuerpo JSON a la SERP API y decodifica la respuesta en un struct. Con serp.cheap el endpoint es https://api.serp.cheap/v1/search con una cabecera X-API-Key.

¿Necesito un SDK de Go de terceros?+

No: net/http y encoding/json de la biblioteca estándar son suficientes. Una SERP API es un simple endpoint HTTP+JSON.

Obtén tu clave de API

Más barato directo, paga con tarjeta o cripto, sin KYC. O usa un marketplace.