To append a character to the beginning of a string in Go, you can use string concatenation.
In this example,
str
with a string value.char
that we want to append to the beginning of the string.+
operator.package main
import "fmt"
func main() {
str := "Hello World"
char := 'A'
newStr := string(char) + str
fmt.Println("Resulting string is:", newStr)
}
Resulting string is: AHello World
In this example,
str
with a different string value.char
that we want to append to the beginning of the string.+
operator.package main
import "fmt"
func main() {
str := "GoLang"
char := 'B'
newStr := string(char) + str
fmt.Println("Resulting string is:", newStr)
}
Resulting string is: BGoLang
In this tutorial, we learned How to Append a Character to beginning of a String in Go language with well detailed examples.