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 关注
- 193 浏览
添加回答
举报