Golang JSON封送处理

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) + "
" // 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).

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("
")
}

fmt.Println(buffer.String())

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("
") // optional
}
fmt.Println(buf.String())

https://play.golang.org/p/5K9Oj0Xbjaa