Skip to content
Tutorials Center
My learning

Topics

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 "".

Go
s := "hΓ©llo"
fmt.Println(len(s), len([]rune(s))) // 6 5

Try it

Go Bytes versus runes

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.

Go Immutability, concatenation and comparison

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

Go
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/utf8 and packages such as golang.org/x/text when you need grapheme-level behavior.
  • Slicing can cut a rune in half. s[:2] of "Γ©..." is a broken UTF-8 sequence. Convert to []rune when you need rune positions.
  • Concatenating in a loop is quadratic. Each += allocates a new string. Use strings.Builder for many pieces.
  • Conversions copy. []byte(s) and string(b) allocate and copy, although the compiler skips the copy in some cases that cannot be observed, such as for range []byte(s) or map lookups with m[string(b)].
  • string(i) for an integer i yields the rune with that code point, not decimal digits. Use strconv.Itoa or fmt.Sprint for digits; go vet flags 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.EqualFold for case-insensitive equality.
  • Strings are safe to share between goroutines because they cannot change.