...

Source file src/google.golang.org/api/examples/customsearch.go

Documentation: google.golang.org/api/examples

     1  // Copyright 2018 Google LLC. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package main
     6  
     7  import (
     8  	"fmt"
     9  	"log"
    10  	"net/http"
    11  
    12  	customsearch "google.golang.org/api/customsearch/v1"
    13  	"google.golang.org/api/googleapi/transport"
    14  )
    15  
    16  const (
    17  	apiKey = "some-api-key"
    18  	cx     = "some-custom-search-engine-id"
    19  	query  = "some-custom-query"
    20  )
    21  
    22  func customSearchMain() {
    23  	client := &http.Client{Transport: &transport.APIKey{Key: apiKey}}
    24  
    25  	svc, err := customsearch.New(client)
    26  	if err != nil {
    27  		log.Fatal(err)
    28  	}
    29  
    30  	resp, err := svc.Cse.List().Cx(cx).Q(query).Do()
    31  	if err != nil {
    32  		log.Fatal(err)
    33  	}
    34  
    35  	for i, result := range resp.Items {
    36  		fmt.Printf("#%d: %s\n", i+1, result.Title)
    37  		fmt.Printf("\t%s\n", result.Snippet)
    38  	}
    39  }
    40  

View as plain text