To append a string to another string in Go, you can use the +
operator or the fmt.Sprintf
function to concatenate the strings.
In this example,
str1
with a value of "Hello"
.+
operator to append the string " World"
to str1
.+
operator concatenates the two strings and returns the result.package main
import "fmt"
func main() {
str1 := "Hello"
concatenated := str1 + " World"
fmt.Println(concatenated)
}
Hello World
In this example,
str2
with a value of "banana"
.fmt.Sprintf
function to concatenate the string "apple"
to str2
.fmt.Sprintf
function formats the string and returns the result.package main
import "fmt"
func main() {
str2 := "banana"
concatenated := fmt.Sprintf("%s%s", str2, "apple")
fmt.Println(concatenated)
}
bananaapple
In this tutorial, we learned How to Append a String to another String in Go language with well detailed examples.