Skip to content

Commit fe38d42

Browse files
Anaethelionepelc
andauthored
replace readme string concat with json marshal (#493) (#494)
Co-authored-by: Ed Pelc <[email protected]>
1 parent 4f482c7 commit fe38d42

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
@@ -187,6 +187,7 @@ import (
187187
"strconv"
188188
"strings"
189189
"sync"
190+
"bytes"
190191

191192
"github.com/elastic/go-elasticsearch/v7"
192193
"github.com/elastic/go-elasticsearch/v7/esapi"
@@ -237,17 +238,17 @@ func main() {
237238
go func(i int, title string) {
238239
defer wg.Done()
239240

240-
// Build the request body.
241-
var b strings.Builder
242-
b.WriteString(`{"title" : "`)
243-
b.WriteString(title)
244-
b.WriteString(`"}`)
241+
// Build the request body.
242+
data, err := json.Marshal(struct{ Title string }{Title: title})
243+
if err != nil {
244+
log.Fatalf("Error marshaling document: %s", err)
245+
}
245246

246247
// Set up the request object.
247248
req := esapi.IndexRequest{
248249
Index: "test",
249250
DocumentID: strconv.Itoa(i + 1),
250-
Body: strings.NewReader(b.String()),
251+
Body: bytes.NewReader(data),
251252
Refresh: "true",
252253
}
253254

0 commit comments

Comments
 (0)