Go Reference Β· Built-in Types
The Go string type
Reference for Go's string type: immutable byte sequences, UTF-8, len vs rune count, indexing, slicing, concatenation, comparison and conversions.
A string in Go is an immutable sequence of bytes. It is usually, but not necessarily, valid UTF-8 text. A string value is a small header holding a pointer to the bytes and a length, so passing or assigning a string copies only that header, never the text.
Because strings are bytes, len(s) counts bytes, s[i] yields a byte, and for i, r := range s decodes UTF-8 and yields runes. The zero value of a string is the empty string "".
s := "hΓ©llo" fmt.Println(len(s), len([]rune(s))) // 6 5
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input Β· what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with bufio.Scanner or fmt.Scan), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input Β· what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with bufio.Scanner or fmt.Scan), it gets the next line from this box.
Syntax
var s string // "" (zero value) s = "interpreted\n" // escapes processed s = `raw string` // backticks: no escapes, may span lines b := []byte(s) // copy into a byte slice r := []rune(s) // decode into code points t := string(b) // bytes to string (copies) t = string(rune(65)) // "A" (not string(65) on an int: vet warns)
| Operation | Description |
|---|---|
len(s) |
Number of bytes. Constant if s is a constant. |
s[i] |
The byte at index i (type byte). Panics if i is out of range. Not addressable, so &s[i] is illegal. |
s[i:j] |
Substring from byte i up to but excluding j. Shares memory with s. |
s + t |
Concatenation; creates a new string. |
==, < and friends |
Byte-wise comparison (not locale-aware). |
range s |
Iterates over runes, yielding the byte offset and the rune. Invalid bytes yield U+FFFD with width 1. |
Errors and edge cases: indexing or slicing out of range panics at run time (or fails to compile for constant indexes into a constant string).
Usage notes
- Strings are not characters. One visible character may be several runes (combining accents, emoji sequences). Use
unicode/utf8and packages such asgolang.org/x/textwhen you need grapheme-level behavior. - Slicing can cut a rune in half.
s[:2]of"Γ©..."is a broken UTF-8 sequence. Convert to[]runewhen you need rune positions. - Concatenating in a loop is quadratic. Each
+=allocates a new string. Usestrings.Builderfor many pieces. - Conversions copy.
[]byte(s)andstring(b)allocate and copy, although the compiler skips the copy in some cases that cannot be observed, such asfor range []byte(s)or map lookups withm[string(b)]. string(i)for an integeriyields the rune with that code point, not decimal digits. Usestrconv.Itoaorfmt.Sprintfor digits;go vetflags the mistake.- Substrings keep the original alive. A small slice of a huge string pins the whole backing memory; use
strings.Clone(Go 1.18) to detach it. - Comparison is bytewise, so uppercase letters sort before lowercase. Use
strings.EqualFoldfor case-insensitive equality. - Strings are safe to share between goroutines because they cannot change.