Strings
- Collection of bytes (or
rune) -
Out-of-the-box support for Unicode strings (Where multiple bytes are required to store on character ->
runeis aint32value that represents one unicode code point) - Strings can be thought of of both bytes and runes:
a := "Hello 榇"
for i := 0; i < len(a); i++ {
fmt.Printf("Character %v, Type: %T\n", a[i], a[i])
}
Will print
Character 72, Type: uint8
Character 101, Type: uint8
Character 108, Type: uint8
Character 108, Type: uint8
Character 111, Type: uint8
Character 32, Type: uint8
Character 230, Type: uint8
Character 166, Type: uint8
Character 135, Type: uint8
An each a[i] is treated as a byte. In the same way,
a := "Hello 榇"
for _, c := range a {
fmt.Printf("Character %v, Type: %T\n", c, c)
}
will print
Character 72, Type: int32
Character 101, Type: int32
Character 108, Type: int32
Character 108, Type: int32
Character 111, Type: int32
Character 32, Type: int32
Character 27015, Type: int32
where every charaacter is a rune
Conversion
The strconv package includes many functions to convert strings to other data types, and in reverse, other types to string.
To convert an integer for example, special attention must be given to the method:
n := 100
input := strconv.Itoa(n) //Will be 100
input = strconv.FormatInt(int64(n), 10) //will be 100
input = string(n) // will be "d"
Processing
Packages like unicode and strings help with string processing. In particular, strings has some useful methods as:
EqualFoldto compare irrelevant of caseHasPrefix,HasSuffixFieldsto split into a slice based on whitespaceSplitto split into a slice based on a separator stringSplitAfterto split into a slice based on a separator string, keeping itTrimto apply aTrimFuncfo the string