Strings

Strings are interpreted as UTF-8 encoded sequences of Unicode code points (they are based on runes).

The byte sequance contained in a string value can't be changed but we can assign a new value to a string variable (Strings are immutable).

str := "hello"
fmt.Println(str[0]) // 104 (type byte)
fmt.Println(&str[0]) // illegal to take address of string
c := str[len(str)] // panic: index out of range

Immutability means that it is safe for two copies of a string to share the same underlying memory, making it cheap to copy strings of any lenght.

A string s and s[:7] may safely share the same data. No new memory is allocated in either case.

results matching ""

    No results matching ""