//go:build go1.20 // +build go1.20 package main import ( "os" "go.wit.com/log" ) // sent via -ldflags var VERSION string var BUILDTIME string var sortmap map[string]string var marshalKeys []string var uniqueKeys []string func main() { var pb *Fruits pb = new(Fruits) // attempt to load basket.pb fruit := &Fruit{ Brand: "mom", City: "New NewYork", UPC: "2000", } pb.Fruits = append(pb.Fruits, fruit) a := &Fruit{ Brand: "fry", City: "the moon", UPC: "2001", } pb.Fruits = append(pb.Fruits, a) pb.saveBasket() } func (pb *Fruits) saveBasket() error { data, err := pb.Marshal() if err != nil { return err } w, err := os.OpenFile("basket.pb", os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644) if err != nil { return err } w.Write(data) w.Close() log.Info("saved basket.pb ok") return nil } func badExit(err error) { log.Info("autogenpb error:", err) os.Exit(-1) }