如何更改从Go服务器返回数据而不保存数据的格式

I'm encoding every search result that I fetch from the channel and then sending it to the response writer and then flushing it but that sends the data like:

[{..}]
[{..}]
[{..}]

this as multiple arrays with a single value

but the format in which I require the data to be sent is like [{..},{..},{..}] this a single array with multiple values.

this can be done if I store the data before in a variable and then encode the whole data but if I store it my run time is running out of memory.

Is there any way to convert it to the desired format without storing it or how to solve my memory problem.

I'm running my go server in a 4gb ram sles12 sp3 system

ch := make(chan *ldap.SearchResult)
//result := &ldap.SearchResult{}

flusher, ok := w.(http.Flusher)

if !ok {
    http.Error(w, "Streaming unsupported!", http.StatusInternalServerError)
    return
}

wg := sync.WaitGroup{}
wg.Add(1)

go func() {
    for res := range ch {
        resp := SearchResultToObjectType(res)

        json.NewEncoder(w).Encode(resp)

        flusher.Flush()
        //result.Entries = append(result.Entries, res.Entries...)
        //result.Controls = append(result.Controls, res.Controls...)
        //result.Referrals = append(result.Referrals, res.Referrals...)
    }
    wg.Done()
}()

err = conn.SearchWithChannel(searchRequest, ch)

wg.Wait()

if err != nil {
    json.NewEncoder(w).Encode(utils.ParseErrorToJson(err))
    event.LogEventError(err, nil)
}

Assuming that resp is a slice with a single element, then use the following code. The code wraps the slice elements in a single JSON array.

go func() {
    enc := json.NewEncoder(w)
    sep := []byte("")
    comma := []byte(",")
    w.Write([]byte("[")
    for res := range ch {
        w.Write(sep)
        sep = comma
        resp := SearchResultToObjectType(res)
        enc.Encode(resp[0])
        flusher.Flush()    
    }
    w.Write([]byte("]")
    wg.Done()
}()

One option would be to construct the outer JSON array manually, using something like this:

first := true
w.Write([]byte("["))
for res := range ch {
    if not first {
        w.Write([]byte(","))
    }
    first = false
    ...
    json.NewEncoder(w).Encode(resp)
    ...
}
w.Write([]byte("]"))