add new concat
parent
35f86b8c87
commit
ca3c998ccc
|
@ -7,3 +7,23 @@ template := "My city is {city} and my country is {country}"
|
||||||
r := strings.NewReplacer("{city}", "Sai Gon", "{country}", "Viet Nam")
|
r := strings.NewReplacer("{city}", "Sai Gon", "{country}", "Viet Nam")
|
||||||
fmt.Println(r.Replace(template))
|
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()
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
Loading…
Reference in New Issue