In Golang, you can concatenate two strings using the +
operator or the fmt.Sprintf
function. Here's an example using both methods:
Using the +
operator:
package main
import "fmt"
func main() {
str1 := "Hello"
str2 := "world"
result := str1 + " " + str2
fmt.Println(result)
}
Output:
Hello world
Using fmt.Sprintf
:
package main
import "fmt"
func main() {
str1 := "Hello"
str2 := "world"
result := fmt.Sprintf("%s %s", str1, str2)
fmt.Println(result)
}
Output:
Hello world
Both approaches can be used to concatenate two or more string variables or constants.