Junior — Middle
Can you explain how the string structure is implemented in programming and what it consists of?
sobes.tech AI
Answer from AI
In Go, a string (string) is an immutable sequence of bytes, usually representing text in UTF-8 encoding. Internally, a string is implemented as a structure with two fields: a pointer to a byte array and a length.
Example of a simplified string structure in Go:
// stringHeader — internal string structure
import "reflect"
var s string = "hello"
header := (*reflect.StringHeader)(unsafe.Pointer(&s))
// header.Data — pointer to bytes
// header.Len — length of the string
Strings in Go are immutable, so any operations that modify a string create a new copy.