Christopher SuAbout Blog Projects Contact More

Append Strings in Go

Here are the different ways strings can be appended (i.e. concatenated) together in Go.

Strings can be concatenated using the + operator.

"first string" + "second string"

If you’re using fmt’s printing functions, you can concatenate strings by including them as additional arguments. This will automatically add spaces between the strings.

fmt.Println("first string", "second string")

For more efficient concatenation, byte buffers can be used. This is the Go equivalent to Java’s StringBuffer.

import "bytes"

func main() {
  buf := bytes.Buffer
  buf.WriteString("first string")
  buf.WriteString("second string")
  result := buf.String()
}

Dot dot dot in Go

The dot dot dot, ..., before a type is a “variadic parameter”. Essentially allows for a variable number of arguments to be passed into the function.