A substring is a contiguous portion of a string produced by an operation such as slicing. A common mistake is assuming log[:36] creates an independent 36-byte string. Go's standard compiler may let the substring share the original string's backing array, so retaining a short substring can keep a much larger log message in memory. section 5.6
s1 := "Hello, World!" s2 := s1[:5] // Hello
Use byte slicing when the requested boundaries are byte boundaries, as with ASCII text or a known byte-length prefix. section 5.6
s1 := "Hêllo, World!" s2 := string([]rune(s1)[:5]) // Hêllo
Use []rune conversion when selecting by character position, because slicing a string counts bytes rather than runes. section 5.6
Avoid retaining a heavy log
A handler that stores uuid := log[:36] may retain the entire, potentially thousands-of-bytes log. Copy the substring before storing it: uuid := string([]byte(log[:36])). strings.Clone(log[:36]) is another supplied copying form. section 5.6 section 5.7
Choose the substring technique according to the data and its lifetime:
- Slice directly when byte positions are intentional and retaining the original backing storage is acceptable. section 5.6
- Convert to []rune before selecting a rune index or handling characters encoded with multiple bytes. section 5.6
- Copy a small substring when the source is large and the result will outlive the source operation. section 5.7




