2 回答

TA貢獻1811條經驗 獲得超6個贊
一種選擇是手動構造外部 JSON 數組,使用如下內容:
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("]"))

TA貢獻1801條經驗 獲得超16個贊
假設這resp是一個包含單個元素的切片,則使用以下代碼。該代碼將切片元素包裝在單個 JSON 數組中。
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()
}()
- 2 回答
- 0 關注
- 184 瀏覽
添加回答
舉報