Go Strings

To concatenate strings in Go (Golang), you can use several methods. Here are the most common ones:

1. Using the + Operator #

The simplest way to concatenate two or more strings is by using the + operator.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
package main

import "fmt"

func main() {
    str1 := "Hello, "
    str2 := "World!"
    result := str1 + str2
    fmt.Println(result) // Output: Hello, World!
}

2. Using fmt.Sprintf #

You can also use fmt.Sprintf, which provides formatted strings.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
package main

import "fmt"

func main() {
    str1 := "Hello, "
    str2 := "World!"
    result := fmt.Sprintf("%s%s", str1, str2)
    fmt.Println(result) // Output: Hello, World!
}

3. Using strings.Join #

For concatenating a slice of strings, strings.Join is the most efficient method.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
package main

import (
    "fmt"
    "strings"
)

func main() {
    parts := []string{"Hello, ", "World!"}
    result := strings.Join(parts, "")
    fmt.Println(result) // Output: Hello, World!
}

4. Using bytes.Buffer #

For more complex or larger strings, consider using bytes.Buffer for optimal performance.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
package main

import (
    "bytes"
    "fmt"
)

func main() {
    var buffer bytes.Buffer
    buffer.WriteString("Hello, ")
    buffer.WriteString("World!")
    result := buffer.String()
    fmt.Println(result) // Output: Hello, World!
}

Each method has its use cases, so choose one based on your specific needs regarding simplicity, performance, and readability.