-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathgosearch.go
55 lines (43 loc) · 1.27 KB
/
gosearch.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package main
// @hipbot gopkg <package-name>
// Search Godoc.org's docs for <package-name> via their API
// return a text 1-sentence explanation of package or else a text no-results response
import (
"encoding/json"
"log"
"net/http"
"net/url"
)
const GO_DOC_ENDPOINT = "http://api.godoc.org/search"
type GoPackageResult struct {
Path string `json:"path"`
Synopsis string `json:"synopsis"`
}
type GoPackageResponse struct {
Results []*GoPackageResult `json:"results"`
}
// Search Godoc.org's documentation sets via their API
func goSearch(query string) string {
// Send GET request, collect response
res, err := http.Get(GO_DOC_ENDPOINT + "?q=" + url.QueryEscape(query))
if err != nil {
log.Println("Error in HTTP GET:", err)
return "error"
}
defer res.Body.Close()
// Decode JSON body
decoder := json.NewDecoder(res.Body)
response := new(GoPackageResponse)
decoder.Decode(response)
// Check for no results
if len(response.Results) == 0 {
return "I found nothing! So sorry."
} else {
// Only show first (most relevant) package
firstResult := (*(response.Results[0]))
// Split response text into "Synopsis" and "Path"
textResponse := "Synopsis: " + firstResult.Synopsis
textResponse += "\nPath: \"" + firstResult.Path + "\""
return textResponse
}
}