Skip to content

Commit 427714e

Browse files
committed
add Product.reset for use with stream.go
1 parent 673c093 commit 427714e

File tree

2 files changed

+23
-3
lines changed

2 files changed

+23
-3
lines changed

product.go

+16
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,22 @@ type Product struct {
3434
Row string
3535
}
3636

37+
func (p Product) reset() {
38+
p.Type = ""
39+
p.ID = ""
40+
p.Price = ""
41+
p.Size = ""
42+
p.High = ""
43+
p.Low = ""
44+
p.Open = ""
45+
p.Volume = ""
46+
p.Bid = ""
47+
p.Ask = ""
48+
p.Change = ""
49+
p.Color = nil
50+
p.Row = ""
51+
}
52+
3753
// fmtRow formats all Product data into a single row so it can be printed
3854
func (p Product) fmtRow() string {
3955
b := strings.Builder{}

stream.go

+7-3
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,9 @@ func quoteStream(state map[string]Product, pairs []string, max *MaxLengths) {
3131
}
3232
conn.WriteJSON(wsSub)
3333

34+
msg := Product{}
35+
product := Product{}
3436
for {
35-
msg := Product{}
3637

3738
err := conn.ReadJSON(&msg)
3839
if err != nil {
@@ -42,7 +43,7 @@ func quoteStream(state map[string]Product, pairs []string, max *MaxLengths) {
4243
}
4344

4445
if msg.Type == "match" {
45-
product := state[msg.ID]
46+
product = state[msg.ID]
4647
product.Price = setSpcStrm(max.Price, rndPrice(msg.Price))
4748
product.Size = setSpcStrm(max.Size, rndSize(msg.Size))
4849
product.Change = setDelta(strings.TrimSpace(product.Price), strings.TrimSpace(product.Open))
@@ -51,7 +52,7 @@ func quoteStream(state map[string]Product, pairs []string, max *MaxLengths) {
5152
state[msg.ID] = product
5253

5354
} else if msg.Type == "ticker" {
54-
product := state[msg.ID]
55+
product = state[msg.ID]
5556
product.Bid = setSpcStrm(max.Bid, rndPrice(msg.Bid))
5657
product.Ask = setSpcStrm(max.Ask, rndPrice(msg.Ask))
5758
product.High = setSpcStrm(max.High, rndPrice(msg.High))
@@ -69,5 +70,8 @@ func quoteStream(state map[string]Product, pairs []string, max *MaxLengths) {
6970
}
7071
clearScr()
7172
print(state, format)
73+
74+
msg.reset()
75+
product.reset()
7276
}
7377
}

0 commit comments

Comments
 (0)