Skip to content

Commit

Permalink
Small clean ups
Browse files Browse the repository at this point in the history
  • Loading branch information
amscotti committed Dec 24, 2021
1 parent 553efa9 commit 28b7de1
Showing 1 changed file with 16 additions and 9 deletions.
25 changes: 16 additions & 9 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,15 @@ import (
"flag"
"fmt"
"io/ioutil"
"log"
"net/http"
"sync"
)

const STORIES_URL = "https://hacker-news.firebaseio.com/v0/topstories.json"
const ITEM_URL_BASE = "https://hacker-news.firebaseio.com/v0/item"
const (
STORIES_URL = "https://hacker-news.firebaseio.com/v0/topstories.json"
ITEM_URL_BASE = "https://hacker-news.firebaseio.com/v0/item"
)

type Story struct {
Title string
Expand Down Expand Up @@ -39,23 +42,24 @@ func downloadStory(id int, wg *sync.WaitGroup, m *sync.Mutex, stories map[int]St

rsp, err := http.Get(url)
if err != nil {
panic(err)
log.Fatal(err)
}
defer rsp.Body.Close()

data, err := ioutil.ReadAll(rsp.Body)
if err != nil {
panic(err)
log.Fatal(err)
}

var story Story
if err := json.Unmarshal(data, &story); err != nil {
panic(err)
log.Fatal(err)
}

m.Lock()
stories[id] = story
m.Unlock()

wg.Done()
}

Expand All @@ -64,16 +68,18 @@ func fetch(number int, showSourceUrls bool) {

rsp, err := http.Get(STORIES_URL)
if err != nil {
panic(err)
log.Fatal(err)
}
defer rsp.Body.Close()

data, err := ioutil.ReadAll(rsp.Body)
if err != nil {
panic(err)
log.Fatal(err)
}

var ids []int
if err := json.Unmarshal(data, &ids); err != nil {
panic(err)
log.Fatal(err)
}

if len(ids) > number {
Expand All @@ -85,8 +91,9 @@ func fetch(number int, showSourceUrls bool) {
var m sync.Mutex
var wg sync.WaitGroup

wg.Add(len(ids))

for _, id := range ids {
wg.Add(1)
go downloadStory(id, &wg, &m, storyDetails)
}

Expand Down

0 comments on commit 28b7de1

Please sign in to comment.