Wednesday, February 21, 2024
HomeSoftware EngineeringTips on how to Effectively Concatenate Strings in Go

Tips on how to Effectively Concatenate Strings in Go


If you’re utilizing a model of Go >= 1.10, then you ought to be utilizing the newer strings.Builder sample as follows:

package deal foremost
import (
    "strings"
    "fmt"
)
func foremost() {
    var sb strings.Builder
    for i := 0; i < 100; i++ {
        sb.WriteString("a")
    }
    fmt.Println(sb.String())
}

For older model, you ought to be trying to make use of the bytes.Buffer as a substitute:

package deal foremost
import (
    "bytes"
    "fmt"
)
func foremost() {
    var buffer bytes.Buffer
    for i := 0; i < 100; i++ {
        buffer.WriteString("a")
    }
    fmt.Println(buffer.String())
}
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments