I got slightly confused this morning when the following code worked.
// s points to an empty string in memory
s := new(string)
// assign 1000 byte string to that address
b := make([]byte, 0, 1000)
for i := 0; i < 1000; i++ {
if i%100 == 0 {
b = append(b, '
')
} else {
b = append(b, 'x')
}
}
*s = string(b)
// how is there room for it there?
print(*s)
http://play.golang.org/p/dAvKLChapd
I feel like I'm missing something obvious here. Some insight would be appreciated.
I hope I understood the question...
An entity of type string is implemented by a run time struct, roughly
type rt_string struct {
ptr *byte // first byte of the string
len int // number of bytes in the string
}
The line
*s = string(b)
sets a new value (of type rt_string) at *s. Its size is constant, so there's "room" for it.
More details in rsc's paper.