How to append data to a json file on new line using Golang

huangapple go评论117阅读模式
英文:

How to append data to a json file on new line using Golang

问题

  1. type dataOP struct {
  2. Opcode_name string `json:"opcode_name"`
  3. ExeTime int `json:"exeTime"`
  4. }
  5. func main() {
  6. book := dataOP{Opcode_name: "ADD", ExeTime: 5}
  7. byteArray, err := json.Marshal(book)
  8. if err != nil {
  9. fmt.Println(err)
  10. }
  11. f, err := os.OpenFile("./data.json", os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0666)
  12. if err != nil {
  13. fmt.Println(err)
  14. return
  15. }
  16. _, err = f.WriteString(string(byteArray) + "\n")
  17. if err != nil {
  18. fmt.Println(err)
  19. }
  20. err = f.Close()
  21. if err != nil {
  22. fmt.Println(err)
  23. }
  24. }

我想使用Golang将数据追加到一个新行的JSON文件中。上面的代码将JSON对象依次追加在一起,类似于:

{"opcode_name":"ADD","exeTime":5}{"opcode_name":"ADD","exeTime":5}{"opcode_name":"ADD","exeTime":5}

但是我想要将这些JSON对象以新行的形式追加在一起(JSON对象之间用新行分隔):

  1. {"opcode_name":"ADD","exeTime":5}
  2. {"opcode_name":"ADD","exeTime":5}
  3. {"opcode_name":"ADD","exeTime":5}
  4. {"opcode_name":"ADD","exeTime":5}
英文:
  1. type dataOP struct {
  2. Opcode_name string `json:"opcode_name"`
  3. ExeTime int `json:"exeTime"`
  4. }
  5. func main() {
  6. book := dataOP{Opcode_name: "ADD", ExeTime: 5}
  7. byteArray, err := json.Marshal(book)
  8. if err != nil {
  9. fmt.Println(err)
  10. }
  11. f, err := os.OpenFile("./data.json", os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0666)
  12. if err != nil {
  13. fmt.Println(err)
  14. return
  15. }
  16. n, err := io.WriteString(f, string(byteArray))
  17. if err != nil {
  18. fmt.Println(n, err)
  19. }
  20. }

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).

  1. {"opcode_name":"ADD","exeTime":5}
  2. {"opcode_name":"ADD","exeTime":5}
  3. {"opcode_name":"ADD","exeTime":5}

答案1

得分: 3

如果你想让每个JSON对象都从新的一行开始,只需在每个对象后面写一个换行符。

另外请注意,os.File有一个File.Write()方法可以直接写入[]byte,所以不需要将其转换为string

还要记得关闭文件,最好使用延迟关闭:

  1. f, err := os.OpenFile("./data.json", os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0666)
  2. if err != nil {
  3. fmt.Println(err)
  4. return
  5. }
  6. defer f.Close()
  7. n, err := f.Write(byteArray)
  8. if err != nil {
  9. fmt.Println(n, err)
  10. }
  11. if n, err = f.WriteString("\n"); err != nil {
  12. fmt.Println(n, err)
  13. }

另外请注意,你可以使用一次调用同时写入字节切片和换行符,像这样:

  1. if _, err := fmt.Fprintf(f, "%s\n", byteArray); err != nil {
  2. fmt.Println(err)
  3. }
英文:

If you want each JSON object to start on a new line, simple write a newline after each object.

Also note that os.File has a File.Write() method to write a []byte, so no need to convert it to string.

Also don't forget to close the file, preferably deferred:

  1. f, err := os.OpenFile("./data.json", os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0666)
  2. if err != nil {
  3. fmt.Println(err)
  4. return
  5. }
  6. defer f.Close()
  7. n, err := f.Write(byteArray)
  8. if err != nil {
  9. fmt.Println(n, err)
  10. }
  11. if n, err = f.WriteString("\n"); err != nil {
  12. fmt.Println(n, err)
  13. }

Also note that you could write the byte slice and a newline with a single call like this:

  1. if _, err := fmt.Fprintf(f, "%s\n", byteArray); err != nil {
  2. fmt.Println(err)
  3. }

huangapple
  • 本文由 发表于 2022年1月28日 15:58:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/70890468.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定