String indexing, s[i], returns a single byte (type byte, an alias of uint8) at position i. String slicing, s[start:end], returns a new string that covers the bytes from start up to but not including end. Both work on byte offsets, not characters, which matters as soon as a string contains non-ASCII text.
Go strings are immutable sequences of bytes, usually holding UTF-8. Neither operation modifies the original string, and both panic if an index is out of range.
s[i] yields the byte at zero-based position i. The index must be in the range 0 <= i < len(s); otherwise the program panics at run time (or fails to compile for a constant index on a constant string).
You cannot assign to an index: s[0] = 'h' does not compile because strings are immutable. To change text, convert to []byte (or []rune), edit the copy and convert back.
s[start:end] creates a substring. Both bounds are optional: s[:n] starts at 0, s[n:] runs to the end, and s[:] is the whole string. The rule 0 <= start <= end <= len(s) must hold or the program panics.
Slicing is cheap: the new string shares the original bytes and only gets its own pointer and length. Unlike slices of other types, a string slice has no capacity and does not support the three-index form s[a:b:c].
Because the bytes are shared, a small substring of a huge string keeps the entire original in memory. If you store small pieces for a long time, copy them with strings.Clone (Go 1.18+).
Both operations count bytes. In UTF-8, ASCII characters take one byte, but many characters take two to four. Indexing or slicing in the middle of a multi-byte character gives invalid UTF-8.
Ways to work with characters correctly:
for i, r := range s iterates over runes; i is the byte offset and r is the code point.[]rune(s) converts to code points so you can index by character, at the cost of a copy.utf8.RuneCountInString(s) counts runes without allocating.utf8.DecodeRuneInString(s[i:]) decodes the rune at a byte offset.Note that a rune is a Unicode code point, not always a user-perceived character. Emoji with modifiers or accented letters built from combining marks span several runes. For grapheme-level handling, use a library based on Unicode text segmentation.
| Aspect | Indexing s[i] | Slicing s[a:b] |
|---|---|---|
| Result type | byte (uint8) | string |
| Number of bytes | Exactly one | b - a |
| Copies data | No (returns a value) | No (shares bytes) |
| Cost | O(1) | O(1) |
| Out of range | Run-time panic | Run-time panic |
| Safe for Unicode text | Only for ASCII | Only on rune boundaries |
| Assignable | No | No |
Check bounds before slicing user-supplied indexes, or the program panics:
To truncate by characters rather than bytes, work with runes:
For splitting at a known separator, prefer strings.Cut(s, "=") (Go 1.18+), strings.HasPrefix and strings.TrimPrefix over hand-written index arithmetic.
s[0] return a number instead of a character?Indexing returns a byte, which is an integer. Print it with %c or convert with string(s[0]) to see the character, but only for single-byte (ASCII) characters.
No. The substring shares the original memory, so slicing is O(1). Use strings.Clone if you want to release the parent string.
Convert to []rune and index that, or loop with for range and count. Indexing the string gives you a byte, not a character.
No. Go indexes must be non-negative, and negative values panic or fail to compile. Compute the position from len(s), for example s[len(s)-3:].
Indexing returns one byte and slicing returns a substring, both by byte offset in O(1) time and without modifying the original. They are safe for ASCII, but for UTF-8 text use runes, for range or the unicode/utf8 package. Always check bounds, and remember that strings are immutable.