Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion chapter2/sample/search/feed.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package search

import (
"encoding/json"
"log"
"os"
)

Expand All @@ -24,7 +25,12 @@ func RetrieveFeeds() ([]*Feed, error) {

// Schedule the file to be closed once
// the function returns.
defer file.Close()
defer func(file *os.File) {
err := file.Close()
if err != nil {
log.Fatal(err)
}
}(file)

// Decode the file into a slice of pointers
// to Feed values.
Expand Down
56 changes: 39 additions & 17 deletions chapter8/listing24/listing24.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,41 +5,63 @@ package main
import (
"encoding/json"
"fmt"
"io"
"log"
"net/http"
)

type (
// gResult maps to the result document received from the search.
gResult struct {
GsearchResultClass string `json:"GsearchResultClass"`
UnescapedURL string `json:"unescapedUrl"`
URL string `json:"url"`
VisibleURL string `json:"visibleUrl"`
CacheURL string `json:"cacheUrl"`
Title string `json:"title"`
TitleNoFormatting string `json:"titleNoFormatting"`
Content string `json:"content"`
}
//gResult struct {
// GsearchResultClass string `json:"GsearchResultClass"`
// UnescapedURL string `json:"unescapedUrl"`
// URL string `json:"url"`
// VisibleURL string `json:"visibleUrl"`
// CacheURL string `json:"cacheUrl"`
// Title string `json:"title"`
// TitleNoFormatting string `json:"titleNoFormatting"`
// Content string `json:"content"`
//}

// gResponse contains the top level document.
gResponse struct {
ResponseData struct {
Results []gResult `json:"results"`
} `json:"responseData"`
Error struct {
Code int64 `json:"code"`
Errors []struct {
Domain string `json:"domain"`
Message string `json:"message"`
Reason string `json:"reason"`
} `json:"errors"`
Message string `json:"message"`
Status string `json:"status"`
} `json:"error"`
}
)

func main() {
uri := "http://ajax.googleapis.com/ajax/services/search/web?v=1.0&rsz=8&q=golang"
uri := "https://www.googleapis.com/customsearch/v1?q=golang"

// Issue the search against Google.
resp, err := http.Get(uri)
if err != nil {
log.Println("ERROR:", err)
return
}
defer resp.Body.Close()

//body, err := ioutil.ReadAll(resp.Body)
//if err != nil {
// log.Fatal(err)
//}
//
//fmt.Printf("Code: %d\n", resp.StatusCode)
//fmt.Printf("Body: %s\n", body)

defer func(Body io.ReadCloser) {
err := Body.Close()
if err != nil {

}
}(resp.Body)

// Decode the JSON response into our struct type.
var gr gResponse
Expand All @@ -49,7 +71,7 @@ func main() {
return
}

fmt.Println(gr)
fmt.Println("Non-Pretty Format: \n", gr)

// Marshal the struct type into a pretty print
// version of the JSON document.
Expand All @@ -59,5 +81,5 @@ func main() {
return
}

fmt.Println(string(pretty))
fmt.Println("\nPretty Format: \n", string(pretty))
}