'How to append data to a json file on new line using Golang
type dataOP struct {
Opcode_name string `json:"opcode_name"`
ExeTime int `json:"exeTime"`
}
func main() {
book := dataOP{Opcode_name: "ADD", ExeTime: 5}
byteArray, err := json.Marshal(book)
if err != nil {
fmt.Println(err)
}
f, err := os.OpenFile("./data.json", os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0666)
if err != nil {
fmt.Println(err)
return
}
n, err := io.WriteString(f, string(byteArray))
if err != nil {
fmt.Println(n, err)
}
}
I want to append data to a json file on new line using Golang. The above code appends the json object one after another, something like
{"opcode_name":"ADD","exeTime":5}{"opcode_name":"ADD","exeTime":5}{"opcode_name":"ADD","exeTime":5}
but i want to append these json objects in new line(json objects separated by new line).
{"opcode_name":"ADD","exeTime":5}
{"opcode_name":"ADD","exeTime":5}
{"opcode_name":"ADD","exeTime":5}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
