V
Vel·ToolKit
Simple · Fast · Ready to use
EN
Chapter 8 of 20

Strings

What strings really are, runes, the strings package

Strings Are Immutable Byte Sequences

A string is backed by a read-only UTF-8 byte array. Indexing s[i] yields a byte (uint8), not a character.

package main

import "fmt"

func main() {
    s := "你好"
    fmt.Println(len(s)) // 6 (number of UTF-8 bytes)
    fmt.Println(s[0])   // 228 (the first byte)
}

Runes and Iterating Characters

A rune is an alias for int32 representing one Unicode code point. range iteration decodes UTF-8 automatically.

package main

import "fmt"

func main() {
    for i, r := range "你好" {
        fmt.Printf("%d %c %U\n", i, r, r)
    }
    // output:
    // 0 你 U+4F60
    // 3 好 U+597D
}

Concatenation Performance

+ concatenation produces a new string every time. For heavy concatenation use strings.Builder.

package main

import (
    "fmt"
    "strings"
)

func main() {
    var b strings.Builder
    for i := 0; i < 1000; i++ {
        b.WriteString("x")
    }
    s := b.String()
    fmt.Println(len(s))
}

Common strings Package Functions

  • strings.Contains / HasPrefix / HasSuffix
  • strings.Split / Join
  • strings.ReplaceAll / TrimSpace / ToLower
  • strconv.Itoa / Atoi for number ⇄ string