Skip to main content

How to Join Strings in Go with the + Operator

· 3 min read
Jagdish Kumawat
Founder @ Dewiride

The simplest way to build text from pieces in Go is the + operator: first + " " + last. Go only joins strings with strings, so a number has to become text first.

Syntax​

Syntax
<text> := <string> + <string>
<text> += <string>
PartWhat it does
<string> + <string>A new string with the second joined to the first. Both sides must be strings.
<text> += <string>Joins the string onto the end of <text> and stores the result in <text>.

The Go language specification on string concatenation: "Strings can be concatenated using the + operator or the += assignment operator".

The Example​

The example joins a first and a last name with a space, joins a sentence with a year turned into text by fmt.Sprint, and builds a greeting with +=.

Prerequisites​

  • Go. go run compiles and runs a single file. This guide uses Go 1.27.4.

Step 1: Write the Code​

Create a file named main.go:

main.go, which joins Ada, a space and Lovelace with plus, joins that with was born in and fmt.Sprint of 1815, and adds a comma and Ada to Hello with plus equals.

main.go
package main

import "fmt"

func main() {
first := "Ada"
last := "Lovelace"
year := 1815

full := first + " " + last
fmt.Println(full)
fmt.Println(full + " was born in " + fmt.Sprint(year))

greeting := "Hello"
greeting += ", " + first
fmt.Println(greeting)
}

Step 2: Run It​

Terminal
go run main.go
Output
Ada Lovelace
Ada Lovelace was born in 1815
Hello, Ada

A zsh terminal where go run main.go prints Ada Lovelace, Ada Lovelace was born in 1815, and Hello, Ada.

The command is the same in zsh, bash and PowerShell.

What Happened​

  • first + " " + last joined three strings into one.
  • fmt.Sprint(year) turned the number 1815 into the text "1815", so it could be joined.
  • greeting += ", " + first joined ", Ada" onto Hello and stored the result back in greeting.

Common Mistakes​

MistakeWhat happensDo this instead
Joining a string and a number, as in "born " + yearIt doesn't compile: invalid operation: "born " + year (mismatched types untyped string and int)Convert first: fmt.Sprint(year) or strconv.Itoa(year)
Converting a number with string(year)It gives the character with that code, ܗ, not 1815, and go vet warns about itUse strconv.Itoa(year)
Building a long string with += in a big loopEach += copies the whole string so farUse a strings.Builder

Troubleshooting​

SymptomLikely causeFix
mismatched types untyped string and intOne side of + is a numberConvert the number to a string
An odd character where a number should bestring(n) was used on an integerUse strconv.Itoa(n)

FAQ​

Is there a function for joining many strings with a separator?​

Yes: strings.Join([]string{"a", "b", "c"}, ", ") gives a, b, c.

When should I use fmt.Sprintf instead?​

When the text mixes several values and formats: fmt.Sprintf("%s was born in %d", name, year) reads more easily than a chain of +.

Conclusion​

a + b joins two strings into a new one and s += t adds to a string. Both sides must be strings, so turn numbers into text first with fmt.Sprint or strconv.Itoa.

Additional Resources​