diff options
| author | Jeff Carr <[email protected]> | 2025-01-11 02:44:21 -0600 |
|---|---|---|
| committer | Jeff Carr <[email protected]> | 2025-01-11 02:44:21 -0600 |
| commit | 9349ab95fc91f74f2c242a7e3ccc0b630c3d95f3 (patch) | |
| tree | caaf75c3d0da4392a57e0d32b137435160b67d0e /example/main.go | |
| parent | 3f2909aa0d95acc5b00642d082013afd40108a30 (diff) | |
small app works, example core dumps on Marshal()
Diffstat (limited to 'example/main.go')
| -rw-r--r-- | example/main.go | 58 |
1 files changed, 18 insertions, 40 deletions
diff --git a/example/main.go b/example/main.go index a248fed..078dbce 100644 --- a/example/main.go +++ b/example/main.go @@ -19,20 +19,18 @@ var sortmap map[string]string var marshalKeys []string var uniqueKeys []string -var pb *Fruits +var counter int = rand.Intn(100) func main() { - pb = NewFruits() + var pb *Fruits + // pb = NewFruits() + pb = new(Fruits) // attempt to load basket.pb if err := pb.loadBasket(); err != nil { log.Info("load basket failed. running this for the first time?") - fruit := &Fruit{ - Brand: "mom", - City: "New NewYork", - UPC: "2000", - } - testAppend(fruit) - appendByUPC() + a := pb.getNewFruit() + pb.Fruits = append(pb.Fruits, a) + pb.printTable() } if err := pb.saveBasket(); err != nil { badExit(err) @@ -40,7 +38,6 @@ func main() { if pb == nil { badExit(fmt.Errorf("pb == nil This should not have happened")) } - pb.printTable() pb.addThings() pb.printTable() pb.saveBasket() @@ -52,6 +49,17 @@ func badExit(err error) { os.Exit(-1) } +func (pb *Fruits) getNewFruit() *Fruit { + counter += rand.Intn(100) + upc := fmt.Sprintf("%d", counter) + fut := &Fruit{ + Brand: "mom", + City: "New NewYork", + UPC: upc, + } + return fut +} + /* x := new(Fruit) x = &Fruit{ @@ -130,36 +138,6 @@ func (pb *Fruits) addThings() { } } -func testAppend(fruit *Fruit) { - if pb.AppendUnique(fruit) { - log.Info("AppendUnique() test1 ok", fruit.Brand, fruit.City) - } else { - badExit(fmt.Errorf("AppendUnique test1 failed %s %s %d\n", fruit.Brand, fruit.City, pb.Len())) - } - if pb.AppendUnique(fruit) { - badExit(fmt.Errorf("AppendUnique() test2 worked but should not have %s %s", fruit.Brand, fruit.City)) - } else { - log.Info("AppendUnique() test2 failed ok", fruit.Brand, fruit.City) - } -} - -func appendByUPC() { - fruit := new(Fruit) - fruit.UPC = "blah331" - fruit.City = "fry" - fruit.City = "paris" - if pb.AppendUniqueByUPC(fruit) { - log.Info("AppendUnique() test1 ok", fruit.Brand, fruit.City) - } else { - badExit(fmt.Errorf("AppendUniqueByUPC test1 failed %s %s %d\n", fruit.Brand, fruit.City, pb.Len())) - } - if pb.AppendUniqueByUPC(fruit) { - badExit(fmt.Errorf("AppendUniqueByUPC() test2 worked but should not have %s %s", fruit.Brand, fruit.City)) - } else { - log.Info("AppendUnique() test2 failed ok", fruit.Brand, fruit.City) - } -} - func (pb *Fruits) saveBasket() error { data, err := pb.Marshal() if err != nil { |
