Skip to content

Commit 3e7c29c

Browse files
Saving Changes
1 parent 2b5df81 commit 3e7c29c

3 files changed

Lines changed: 17 additions & 19 deletions

File tree

‎src/chapter2/search/feeds/feed.go‎

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,25 +19,24 @@ type (
1919
)
2020

2121
// Load retrieves and unmarshals the data for the program.
22-
func Load() ([]Site, error) {
22+
func Load(sites *[]Site) error {
2323
// Get the current directory we are running inside.
2424
pwd, err := os.Getwd()
2525
if err != nil {
26-
return nil, err
26+
return err
2727
}
2828

2929
// Read the entire file.
3030
data, err := ioutil.ReadFile(pwd + "/" + dataFile)
3131
if err != nil {
32-
return nil, err
32+
return err
3333
}
3434

3535
// Unmarshal the json document into a slice of sites.
36-
var sites []Site
37-
err = json.Unmarshal(data, &sites)
36+
err = json.Unmarshal(data, sites)
3837
if err != nil {
39-
return nil, err
38+
return err
4039
}
4140

42-
return sites, nil
41+
return nil
4342
}

‎src/chapter2/search/main.go‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ func main() {
1313
searchTerm := "president"
1414

1515
// Load the feeds for the data file.
16-
log.Printf("Loading site feed.\n")
17-
sites, err := feeds.Load()
16+
var sites []feeds.Site
17+
err := feeds.Load(&sites)
1818
if err != nil {
1919
log.Fatal(err)
2020
}
@@ -60,7 +60,8 @@ func find(searchTerm string, site feeds.Site, captureResults chan []rss.SearchRe
6060
}
6161

6262
// Search the document for the search term.
63-
searchResults, err := rss.Search(document, searchTerm)
63+
var searchResults []rss.SearchResult
64+
err = rss.Search(document, searchTerm, &searchResults)
6465
if err != nil {
6566
log.Printf("%s : %s", site.Uri, err)
6667
return

‎src/chapter2/search/rss/rss.go‎

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -83,23 +83,21 @@ func Retrieve(uri string, document *Document) error {
8383
// Save the uri we used to retrieve this document.
8484
document.Uri = uri
8585

86-
return err
86+
return nil
8787
}
8888

8989
// Search looks at the document for the specified search term.
90-
func Search(document Document, searchTerm string) ([]SearchResult, error) {
91-
var searchResults []SearchResult
92-
90+
func Search(document Document, searchTerm string, searchResults *[]SearchResult) error {
9391
for _, item := range document.Channel.Item {
9492
// Check the title for the search term.
9593
matched, err := regexp.MatchString(searchTerm, item.Title)
9694
if err != nil {
97-
return nil, err
95+
return err
9896
}
9997

10098
// If we found as match save the result.
10199
if matched {
102-
searchResults = append(searchResults, SearchResult{
100+
*searchResults = append(*searchResults, SearchResult{
103101
Field: "Title",
104102
Document: item,
105103
})
@@ -108,17 +106,17 @@ func Search(document Document, searchTerm string) ([]SearchResult, error) {
108106
// Check the description for the search term.
109107
matched, err = regexp.MatchString(searchTerm, item.Description)
110108
if err != nil {
111-
return nil, err
109+
return err
112110
}
113111

114112
// If we found a match so save the result.
115113
if matched {
116-
searchResults = append(searchResults, SearchResult{
114+
*searchResults = append(*searchResults, SearchResult{
117115
Field: "Description",
118116
Document: item,
119117
})
120118
}
121119
}
122120

123-
return searchResults, nil
121+
return nil
124122
}

0 commit comments

Comments
 (0)