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

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

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
	}

	_, err = f.WriteString(string(byteArray) + "\n")
	if err != nil {
		fmt.Println(err)
	}

	err = f.Close()
	if err != nil {
		fmt.Println(err)
	}
}

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

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

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

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

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}

答案1

得分: 3

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

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

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

f, err := os.OpenFile("./data.json", os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0666)
if err != nil {
    fmt.Println(err)
    return
}
defer f.Close()

n, err := f.Write(byteArray)
if err != nil {
    fmt.Println(n, err)
}

if n, err = f.WriteString("\n"); err != nil {
    fmt.Println(n, err)
}

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

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

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:

f, err := os.OpenFile("./data.json", os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0666)
if err != nil {
	fmt.Println(err)
	return
}
defer f.Close()

n, err := f.Write(byteArray)
if err != nil {
	fmt.Println(n, err)
}

if n, err = f.WriteString("\n"); err != nil {
	fmt.Println(n, err)
}

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

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

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:

确定