英文:
Golang JSON Marshaling
问题
我正在尝试将一个数组转换为字符串,并用换行符分隔所有元素。我遇到了内存不足的问题,想找到一种更高效的方法来实现这个目标。
buffer := ""
for _, record := range all_data {
body, _ := json.Marshal(record)
buffer += string(body) + "\n" // 在这里我遇到了内存不足的问题
}
问题:
有没有一种方法可以将换行符追加到字节数组中?目前我是通过string(body)
进行类型转换,但我认为这个操作会分配很多内存(但也许我错了)。
英文:
I'm trying to marshal an array into a string, separating all elements with newlines. I'm running out of memory and think about a more efficient way to do this.
buffer := ""
for _, record := range all_data {
body, _ := json.Marshal(record)
buffer += string(body) + "\n" // i run out of memory here
Question:
Is there a way to append a newline character to a byte array? Right now I'm casting via string(body)
, but I think that this operation allocates a lot of memory (but maybe I'm wrong).
答案1
得分: 4
假设你的数据对计算机来说并不是太大,问题很可能是字符串构建的效率低下。相反,你应该使用bytes.buffer
,然后调用它的String()
方法。这里有一个示例:
var buffer bytes.Buffer
for _, record := range all_data {
body, _ := json.Marshal(record)
buffer.Write(body)
buffer.WriteString("\n")
}
fmt.Println(buffer.String())
假设你的数据对计算机来说并不是太大,问题很可能是字符串构建的效率低下。相反,你应该使用bytes.buffer
,然后调用它的String()
方法。这里有一个示例:
var buffer bytes.Buffer
for _, record := range all_data {
body, _ := json.Marshal(record)
buffer.Write(body)
buffer.WriteString("\n")
}
fmt.Println(buffer.String())
英文:
Assuming your data isn't inherently too big for the computer it's running on, the problem is likely the inefficient building of that string. Instead you should be using a bytes.buffer
and then callings it's String()
method. Here's an example;
var buffer bytes.Buffer
for _, record := range all_data {
body, _ := json.Marshal(record)
buffer.Write(body)
buffer.WriteString("\n")
}
fmt.Println(buffer.String())
答案2
得分: 0
添加到evanmcdonnal的答案中:你甚至不需要通过json.Marshal创建一个中间缓冲区:
var buf bytes.Buffer
enc := json.NewEncoder(&buf)
for _, record := range allData {
if err := enc.Encode(record); enc != nil {
// 处理错误
}
buf.WriteString("\n") // 可选
}
fmt.Println(buf.String())
链接:https://play.golang.org/p/5K9Oj0Xbjaa
英文:
To add to evanmcdonnal's answer: you don't even need an intermediate buffer created by json.Marshal:
var buf bytes.Buffer
enc := json.NewEncoder(&buf)
for _, record := range allData {
if err := enc.Encode(record); enc != nil {
// handle error
}
buf.WriteString("\n") // optional
}
fmt.Println(buf.String())
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论