-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
70 lines (63 loc) · 1.19 KB
/
main.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package main
import (
"net/http"
"io/ioutil"
"encoding/xml"
"fmt"
"time"
)
type QuoteResponse struct {
Status string
Name string
LastPrice float32
Change float32
ChangePercent float32
TimeStamp string
MSDate float32
MarketCap int
Volume int
ChangeYTD float32
ChangePercentYTD float32
High float32
Low float32
Open float32
}
var stocks = []string{
"googl",
"msft",
"nflx",
"tsla",
"aapl",
"bbry",
"fb",
"amzn",
"hpq",
"vz",
"t",
"tmus",
"s",
}
func main() {
defer fmt.Println("Stocks application closing...")
numComplete := 0
for {
t := time.Now()
fmt.Println("Stock prices", t.Format("02/01/06 03:04:05 PM"))
for _, symbol := range stocks {
go func(symbol string) {
resp, _ := http.Get("http://dev.markitondemand.com/MODApis/Api/v2/Quote?symbol=" + symbol)
defer resp.Body.Close()
body, _ := ioutil.ReadAll(resp.Body)
quote := new(QuoteResponse)
xml.Unmarshal(body, "e)
fmt.Printf("%s: %.2f\n", quote.Name, quote.LastPrice)
numComplete++
}(symbol)
}
// Ensure computation is carried out before main goroutine terminates
for numComplete < len(stocks) {
time.Sleep(10 * time.Millisecond)
}
time.Sleep(15 * time.Minute)
}
}