Skip to content

Commit 1a5ef82

Browse files
committed
examples: Add example of custom search
Fixes #222 Change-Id: I335f268bb1bcad6d649174f9371f4875b7eb07d1 Reviewed-on: https://code-review.googlesource.com/c/35992 Reviewed-by: kokoro <noreply+kokoro@google.com> Reviewed-by: Jean de Klerk <deklerk@google.com>
1 parent 4c24fd0 commit 1a5ef82

1 file changed

Lines changed: 39 additions & 0 deletions

File tree

examples/customsearch.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// Copyright 2018 Google Inc. 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.Siterestrict.List(query).Cx(cx).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+
}

0 commit comments

Comments
 (0)