From ca3c998ccca81e3666dd3dd73c70fdc33e71cfc1 Mon Sep 17 00:00:00 2001 From: hau Date: Sun, 6 Sep 2020 16:35:58 +0700 Subject: [PATCH] add new concat --- Development/Go/strings.md | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/Development/Go/strings.md b/Development/Go/strings.md index 57af105..23b134c 100644 --- a/Development/Go/strings.md +++ b/Development/Go/strings.md @@ -7,3 +7,23 @@ template := "My city is {city} and my country is {country}" r := strings.NewReplacer("{city}", "Sai Gon", "{country}", "Viet Nam") fmt.Println(r.Replace(template)) ``` + +Use `Builder` to **efficently** concat string: + +```go +func oldConcat(strs ...string) string { + var result string + for _, str := range strs { + result += str + } + return result +} + +func newConcat(strs ...string) string { + var sb strings.Builder + for _, str := range strs { + sb.WriteString(str) + } + return sb.String() +} +```