A quick way to see how Go types are laid out in memory is to trigger a panic and read the stack trace.
1 | package main |
When this runs, the panic prints stack information for the call site twice. Focus on the function arguments (note that panic only shows ten arguments; the rest are truncated):
main.retFunc(0x9edcf, 0x1, 0x1042ff9c, 0x3, 0x3, 0x0, 0x0, 0x0, 0x0, 0x0, ...)
That does not line up with the parameter list:
retFunc(s string, sl []rune, it interface{}, a AA) int
You can shrink the signature and try one type at a time until the pattern is clear. Here is the mapping:
map— one word (pointer to map header)string— two words (pointer to data and length)slice— three words (pointer to backing array, length, capacity)interface— two words (pointer to type and pointer to data)struct— fields laid out in order, each following its own representation
So the panic line decodes as:
1 | main.retFunc( |
Questions? Leave a comment below or email: newbvirgil@gmail.com![]()
