orderbook: extract String() from Print()

This commit is contained in:
ycdesu 2021-03-04 08:55:33 +08:00
parent 43275d08bc
commit cd6457f5c0

View File

@ -3,6 +3,7 @@ package types
import ( import (
"fmt" "fmt"
"sort" "sort"
"strings"
"sync" "sync"
"github.com/pkg/errors" "github.com/pkg/errors"
@ -227,16 +228,30 @@ func (b *OrderBook) Update(book OrderBook) {
} }
func (b *OrderBook) Print() { func (b *OrderBook) Print() {
fmt.Printf("BOOK %s\n", b.Symbol) fmt.Printf(b.String())
fmt.Printf("ASKS:\n")
for i := len(b.Asks) - 1; i >= 0; i-- {
fmt.Printf("- ASK: %s\n", b.Asks[i].String())
} }
fmt.Printf("BIDS:\n") func (b *OrderBook) String() string {
for _, bid := range b.Bids { sb := strings.Builder{}
fmt.Printf("- BID: %s\n", bid.String())
sb.WriteString("BOOK ")
sb.WriteString(b.Symbol)
sb.WriteString("\n")
sb.WriteString("ASKS:\n")
for i := len(b.Asks) - 1; i >= 0; i-- {
sb.WriteString("- ASK: ")
sb.WriteString(b.Asks[i].String())
sb.WriteString("\n")
} }
sb.WriteString("BIDS:\n")
for _, bid := range b.Bids {
sb.WriteString("- BID: ")
sb.WriteString(bid.String())
sb.WriteString("\n")
}
return sb.String()
} }
type MutexOrderBook struct { type MutexOrderBook struct {