diff --git a/chapter2/sample/search/feed.go b/chapter2/sample/search/feed.go index ee254ae7..5f8f1a87 100644 --- a/chapter2/sample/search/feed.go +++ b/chapter2/sample/search/feed.go @@ -2,6 +2,7 @@ package search import ( "encoding/json" + "log" "os" ) @@ -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. diff --git a/chapter8/listing24/listing24.go b/chapter8/listing24/listing24.go index 8c2d767e..b4028aab 100644 --- a/chapter8/listing24/listing24.go +++ b/chapter8/listing24/listing24.go @@ -5,33 +5,41 @@ 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) @@ -39,7 +47,21 @@ func main() { 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 @@ -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. @@ -59,5 +81,5 @@ func main() { return } - fmt.Println(string(pretty)) + fmt.Println("\nPretty Format: \n", string(pretty)) }