I want to continuously write json objects to a file. To be able to read it, I need to wrap them into an array. I don't want to read the whole file, for simple appending. So what I' doing now:
comma := []byte(", ") file, err := os.OpenFile(erp.TransactionsPath, os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0666) if err != nil { return err } transaction, err := json.Marshal(t) if err != nil { return err } transaction = append(transaction, comma...) file.Write(transaction)
But with this implementation I will need to add []
scopes by hand(or via some script) before reading. How can I add an object before closing scope on each writing?
#json #go