Skip to content

Commit db678f9

Browse files
authored
replace readme string concat with json marshal (#493)
1 parent ee9f57d commit db678f9

File tree

1 file changed

+7
-6
lines changed

1 file changed

+7
-6
lines changed

README.md

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,7 @@ import (
196196
"strconv"
197197
"strings"
198198
"sync"
199+
"bytes"
199200

200201
"github.com/elastic/go-elasticsearch/v8"
201202
"github.com/elastic/go-elasticsearch/v8/esapi"
@@ -246,17 +247,17 @@ func main() {
246247
go func(i int, title string) {
247248
defer wg.Done()
248249

249-
// Build the request body.
250-
var b strings.Builder
251-
b.WriteString(`{"title" : "`)
252-
b.WriteString(title)
253-
b.WriteString(`"}`)
250+
// Build the request body.
251+
data, err := json.Marshal(struct{ Title string }{Title: title})
252+
if err != nil {
253+
log.Fatalf("Error marshaling document: %s", err)
254+
}
254255

255256
// Set up the request object.
256257
req := esapi.IndexRequest{
257258
Index: "test",
258259
DocumentID: strconv.Itoa(i + 1),
259-
Body: strings.NewReader(b.String()),
260+
Body: bytes.NewReader(data),
260261
Refresh: "true",
261262
}
262263

0 commit comments

Comments
 (0)