← ब्लॉग पर वापस

Go में Google Search API: संपूर्ण ट्यूटोरियल (2026)

Go (Golang) में Google Search API का उपयोग करें — net/http अनुरोध, struct में JSON डिकोडिंग, retries और पेजिनेशन। serp.cheap के साथ कॉपी-पेस्ट उदाहरण।

1 मिनट पढ़ें

केवल स्टैंडर्ड लाइब्रेरी के साथ Go से Google को query करें, serp.cheap का उपयोग करते हुए — या आधिकारिक Go SDK जोड़ें (go get github.com/serpcheap/serpcheap-go)। दोनों ही तरीकों में यह एक सादा HTTP+JSON endpoint है।

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

प्रोडक्शन के लिए, एक्सपोनेंशियल बैकऑफ़ के साथ 429/5xx पर एक retry और एक context.Context टाइमआउट जोड़ें।

आगे

Frequently asked questions

मैं Go में Google Search API कैसे कॉल करूँ?+

SERP API को एक JSON body POST करने के लिए net/http का उपयोग करें और रिस्पॉन्स को एक struct में डिकोड करें। serp.cheap के साथ endpoint https://api.serp.cheap/v1/search है जिसमें एक X-API-Key हेडर होता है।

क्या मुझे किसी थर्ड-पार्टी Go SDK की आवश्यकता है?+

नहीं — स्टैंडर्ड लाइब्रेरी का net/http और encoding/json पर्याप्त हैं। एक SERP API एक सादा HTTP+JSON endpoint है।

अपनी API कुंजी पाएं

सीधे सबसे सस्ता, कार्ड या क्रिप्टो से भुगतान, कोई KYC नहीं। या मार्केटप्लेस से।