Understanding the Difference Between , and + in Go (Golang)

Prasetyo Putra Pratama
Google Developer Indonesia
2 min readOct 4, 2023

--

Photo by Mitchell Luo on Unsplash

In Go, the `fmt.Println` function is commonly used to print output to the console. Understanding how the `,` and `+` operators are used within this function can significantly impact how you format and display your output. Let’s dive into the distinctions between them with the following code example:

package main
import "fmt"
func main() {
var name string
name = "Prasetyo Putra Pratama"
var position = "Backend Developer"
fmt.Println("Hello", name+",", position)
}

1. **`,` (Comma Operator):**
— In Go’s `fmt.Println` function, the `,` operator is used to separate multiple values or expressions that you want to print.
— In the code example, `fmt.Println(“Hello”, name+”,”, position)` uses the `,` operator to print three distinct values sequentially: `”Hello”`, `name+”,”,` and `position`.
— The `,` operator inserts a space between each value, resulting in the output: `”Hello Prasetyo Putra Pratama, Backend Developer”`.

2. **`+` (Plus Operator):**
— The `+` operator is used for string concatenation in Go. It joins two or more strings together into a single string.
— If you were to use the `+` operator in the `fmt.Println` statement, like this: `fmt.Println(“Hello” + name + “,” + position)`, it would concatenate the strings without adding any spaces.
— The output in this case would be: `”HelloPrasetyo Putra Pratama,Backend Developer”`, with no spaces between the words.

In summary, the `,` operator in `fmt.Println` separates values with spaces, making it useful for creating human-readable output with proper spacing. On the other hand, the `+` operator is used for string concatenation, which joins strings together without spaces. Choosing the appropriate operator depends on how you want to format and display your output in your Go programs.

--

--

Prasetyo Putra Pratama
Google Developer Indonesia

Hi, I'm a Software Engineer and blockchain enthusiast, and I love to talk about the latest developments in technology