Go String Concatenation: Methods & Examples
In Go language, you can use the plus operator to concatenate strings, as well as the fmt.Sprintf function to format and concatenate strings. Additionally, you can also use the strings.Join function to concatenate multiple strings. Here are examples of different methods for string concatenation.
- Use the plus sign operator:
str1 := "Hello"
str2 := "World"
result := str1 + " " + str2
fmt.Println(result) // 输出:Hello World
- Utilize the fmt.Sprintf function.
str1 := "Hello"
str2 := "World"
result := fmt.Sprintf("%s %s", str1, str2)
fmt.Println(result) // 输出:Hello World
- Utilize the strings.Join function:
strs := []string{"Hello", "World"}
result := strings.Join(strs, " ")
fmt.Println(result) // 输出:Hello World