Strings and string operations in Go:

In Go, string is a primitive type [1] as stated earlier in the report. The official blog [2] explains that

In Go, a string is in effect a read-only slice of bytes.

Like Python, string is immutable in Go and can be represented as the example below

const sample = "\xbd\xb2\x3d\xbc\x20\xe2\x8c\x98"

GO only allows limited operations on string as it is immutable [3] and represented as a byte-slice. Unlike C/C++, string in Go is not null-terminated. The runtime representation of string, StringHeader [4], has two fields to represent the string.

type StringHeader struct {
        Data uintptr
        Len  int
}

The following example shows the limited operation on string.

// go run string.go
package main
import "fmt"

func main(){
    var s1 = "hello"
    var s2 = " world"
    var s3 = s1 + s2
    // var s4 = s1 * 2  // this line won't compile
    // var s4 = s1 * s2 // won't compilre either
    var c = s3[1]
    fmt.Println(s3, c)
}

The result is

hello world 101

[1] https://golang.org/ref/spec#String_types

[2] https://blog.golang.org/strings

[3] https://golang.org/pkg/builtin/#string

[4] https://golang.org/pkg/reflect/#StringHeader