You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
2 different objects can have the same address in memory if they are empty structs (0 sized). The Go spec calls this out:
Two distinct zero-size variables may have the same address in memory.
This can still be surprising, and lead to issues when the pointer value is used for comparisons, here's a couple of examples that have some unexpected results.
Add 2 unique elements, but since they have the same address, only a single element is added:
I think taking addresses to empty structs (which I think are the only 0-sized objects) should be discouraged. I'd also like to build a linter for this to flag it automatically.
The text was updated successfully, but these errors were encountered:
The point is valid, though I think this is probably a bit niche compared to how (more or less) broadly applicable the other guidance is. Based on how things are structured right now, I'd consider adding two discrete bits:
How to properly define types for context keys to avoid conflation; and
"Memory gotchas" more generally, which would relate more to situations in which types (pointers to zero values, pointers to functions, etc) may have surprising memory addresses or other related behavior
The former could make sense as-is, provided that it's a common enough problem (unclear atm, but very possibly). We've also been talking about adding a section for more advanced and/or "in the weeds" guidance, which the latter would fall nicely into, I think.
Agree that the guidance is very specific, so doesn't make sense at the top-level by itself. I think breaking it apart guidance for context keys, and a separate memory gotchas section makes a lot of sense.
2 different objects can have the same address in memory if they are empty structs (0 sized). The Go spec calls this out:
This can still be surprising, and lead to issues when the pointer value is used for comparisons, here's a couple of examples that have some unexpected results.
Add 2 unique elements, but since they have the same address, only a single element is added:
Using context keys using pointers to empty structs which end up being the same,
I think taking addresses to empty structs (which I think are the only 0-sized objects) should be discouraged. I'd also like to build a linter for this to flag it automatically.
The text was updated successfully, but these errors were encountered: