How to Join Two Strings Together in Golang
If you need to join two (2) strings together in Golang, you can do the following, using the + concatenation operator:
package main
import ( "fmt")
func main() {
message1 := "Join Strings"
message2 := "Together"
result := message1 + " " + message2
fmt.Println(result)
}
The + operator is the simplest way to concatenate strings in Go and works fine for joining a small number of values. If you’re building strings in a loop or concatenating many pieces, use strings.Builder instead — it’s much more efficient because it avoids creating a new string allocation on every concatenation. You can also use fmt.Sprintf for formatted string building, or strings.Join when combining a slice of strings with a separator.