A string in Go is an immutable, read-only sequence of arbitrary bytes. While conventionally used to hold UTF-8 encoded text, a string at its core is strictly a sequence of 8-bit bytes (Documentation Index
Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt
Use this file to discover all available pages before exploring further.
uint8) and does not inherently guarantee valid Unicode.
Internal Representation and Slicing
At runtime, a string is represented as a two-word data structure. It consists of a pointer to the underlying backing array and an integer tracking the length. Conceptually, it mirrors this struct:s[start:end]). Slicing a string creates a new string header that points to a different offset within the same backing array. It adjusts the length accordingly without allocating new memory or copying the underlying bytes.
Immutability
Once a string is created, its contents cannot be altered. Attempting to reassign a value at a specific index results in a compile-time error.[]byte or []rune), modified, and then converted back to a string. This process allocates new memory and copies the data.
String Literals
Go provides two syntaxes for declaring string literals:- Interpreted String Literals: Enclosed in double quotes (
"..."). These evaluate standard escape sequences (e.g.,\n,\t,\xNNfor hex bytes,\uNNNNfor Unicode code points). - Raw String Literals: Enclosed in backticks (
`...`). These ignore all escape sequences, treat backslashes as literal characters, and can span multiple lines.
Indexing and Length
Because strings are byte sequences, the built-inlen() function returns the number of bytes, not the number of Unicode characters. Similarly, indexing into a string yields the raw byte (uint8) at that memory offset.
unicode/utf8 package:
Iteration and Runes
To handle multi-byte Unicode characters correctly, Go uses therune type, which is an alias for int32 representing a single Unicode code point.
When you iterate over a string using a for...range loop, Go implicitly decodes the UTF-8 sequence on the fly. It yields the starting byte index and the decoded rune. If the loop encounters an invalid UTF-8 byte sequence, it yields the Unicode replacement character (\uFFFD, also defined as utf8.RuneError) and advances the loop by exactly one byte.
for loop with an index, you will iterate strictly byte-by-byte, which will fracture multi-byte Unicode characters.
Master Go with Deep Grasping Methodology!Learn More





