File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -2,7 +2,6 @@ package data
22
33import (
44 "encoding/json"
5- "io/ioutil"
65 "os"
76)
87
@@ -21,21 +20,17 @@ type (
2120
2221// Load retrieves and unmarshals the data for the program.
2322func Load () ([]Feed , error ) {
24- // Get the current directory we are running inside.
25- pwd , err := os .Getwd ()
26- if err != nil {
27- return nil , err
28- }
29-
3023 // Read the entire file.
31- data , err := ioutil . ReadFile ( pwd + "/" + dataFile )
24+ file , err := os . Open ( dataFile )
3225 if err != nil {
3326 return nil , err
3427 }
3528
29+ defer file .Close ()
30+
3631 // Unmarshal the json document into a slice of feeds.
3732 var feeds []Feed
38- err = json .Unmarshal ( data , & feeds )
33+ err = json .NewDecoder ( file ). Decode ( & feeds )
3934 if err != nil {
4035 return nil , err
4136 }
Original file line number Diff line number Diff line change @@ -2,13 +2,18 @@ package main
22
33import (
44 "log"
5+ "os"
56 "sync"
67
78 "github.com/goinaction/code/src/chapter2/search/data"
89 search "github.com/goinaction/code/src/chapter2/search/feed"
910 "github.com/goinaction/code/src/chapter2/search/rss"
1011)
1112
13+ func init () {
14+ log .SetOutput (os .Stdout )
15+ }
16+
1217// NewMatcher is a factory that creates matcher values based
1318// on the type of feed specified.
1419func NewMatcher (feed data.Feed ) search.Matcher {
Original file line number Diff line number Diff line change 2626 // image defines the fields associated with the image tag in the rss document.
2727 image struct {
2828 XMLName xml.Name `xml:"image"`
29- Url string `xml:"url"`
29+ URL string `xml:"url"`
3030 Title string `xml:"title"`
3131 Link string `xml:"link"`
3232 }
@@ -116,7 +116,7 @@ func (m *matcher) Match(searchTerm string) ([]feed.Result, error) {
116116// retrieve performs a HTTP Get request for the rss feed and unmarshals the results.
117117func (m * matcher ) retrieve () (* document , error ) {
118118 if m .Feed .Uri == "" {
119- return nil , errors .New ("No RSS Feed Uri Provided " )
119+ return nil , errors .New ("No rss feed uri provided " )
120120 }
121121
122122 // Retrieve the rss feed document from the web.
You can’t perform that action at this time.
0 commit comments