OneBite.Dev - Coding blog in a bite size

concatenate a string in Go

Code snippet on how to concatenate a string in Go

package main

import "fmt"

func main() {
	str1 := "This is a "
	str2 := "concatenation."

	fmt.Println(str1 + str2)
}

This code will concatenate two strings in the Go language. First, we define two strings - str1 and str2 - and assign them each a value. We then use the Println() function from the fmt package that is already imported. The Println() function will output a line of text - in this case, the concatenated strings. We use the ”+” symbol between the two strings to join them together, essentially merging the two strings into one string. All together, the code will output “This is a concatenation.” as the final result.

go