Concatenate Strings in Go Image Concatenate Strings in Go

In this code snippet, we are going to look at how you can efficiently concatenate strings in Go using the string.Builder type.

main.go
package main

import (
    "fmt"
    "strings"
)

func main() {
    fmt.Println("String Concatenation Tutorial")

    var myString strings.Builder
	// we can use the WriteString method to append
	// to our existing strings.Builder string
    myString.WriteString("Hello ")
	// here we append to the end of our string
    myString.WriteString("World")
	
	// print out our concatenated string
    fmt.Println(myString.String())

}
$ go run main.go
String Concatenation Tutorial
Hello World

Further Reading

If you enjoyed this really quick and dirty code snippet on string concatenation in Go, you may also like these articles:

Elliot Forbes

Elliot Forbes
Twitter: @Elliot_f

Hey, I'm Elliot and I've been working on TutorialEdge for the last 4 years! If my work has helped you in any way, shape, or form then please consider supporting my work.

become a patron Buy Me A Coffee