Documentation
¶
Overview ¶
Package trades provides an SQLite based trades database.
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type DB ¶
type DB struct {
// contains filtered or unexported fields
}
DB is a database of stock trades.
Example ¶
package main
import (
"fmt"
"math/rand"
"time"
"github.com/ardanlabs/python-go/sqlite/trades"
)
func main() {
dbFile := "/tmp/db-test" + time.Now().Format(time.RFC3339)
db, err := trades.NewDB(dbFile)
if err != nil {
fmt.Println("ERROR: create -", err)
return
}
defer db.Close()
const count = 10_000
for i := 0; i < count; i++ {
trade := trades.Trade{
Time: time.Now(),
Symbol: "AAPL",
Price: rand.Float64() * 200,
IsBuy: i%2 == 0,
}
if err := db.Add(trade); err != nil {
fmt.Println("ERROR: insert - ", err)
return
}
}
fmt.Printf("inserted %d records\n", count)
}
Output: inserted 10000 records
func NewDB ¶
NewDB constructs a Trades value for managing stock trades in a SQLite database. This API is not thread safe.
func (*DB) Add ¶
Add stores a trade into the buffer. Once the buffer is full, the trades are flushed to the database.
Click to show internal directories.
Click to hide internal directories.