-
Notifications
You must be signed in to change notification settings - Fork 0
/
fetch.go
48 lines (37 loc) · 819 Bytes
/
fetch.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package main
import (
"encoding/json"
"io"
"log"
"net/http"
"os"
"sync"
"github.com/joho/godotenv"
)
func fetchStockData(c chan StockData, stock string, wg *sync.WaitGroup) {
// The counter is decremented by calling wg.Done()
defer wg.Done()
// load .env file
err := godotenv.Load()
if err != nil {
log.Fatal("Error loading .env file")
}
key := os.Getenv("API_KEY")
resp, err := http.Get("https://www.alphavantage.co/query?function=TIME_SERIES_INTRADAY&symbol=" + stock + "&interval=5min&apikey=" + key)
if err != nil {
log.Println(err)
return
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
log.Println(err)
return
}
var data StockData
if err := json.Unmarshal(body, &data); err != nil {
log.Println(err)
return
}
c <- data
}