There is such workflow of writing json objects to file :
for {
Step 1. create json object
Step 2. Save object to file
}
So I wrote such code
f, _ := os.Create("output.json")
defer f.Close()
a1 := A{Name:"John", Surname:"Black"}
a2 := A{Name:"Mary", Surname:"Brown"}
a1_json, _ := json.MarshalIndent(a1, "", "\t")
a2_json, _ := json.MarshalIndent(a2, "", "\t")
f.Write(a1_json)
f.Write(a2_json)
As a result I have:
{
"Name": "John",
"Surname": "Black"
}{
"Name": "Mary",
"Surname": "Brown"
}
Which is not correct json file, because it doesn't have opening and closing braces and comma like this:
[
{
"Name": "John",
"Surname": "Black"
},
{
"Name": "Mary",
"Surname": "Brown"
}
]
How can I write to file in an appropriate way?
golibraries which can handle such cases?