On Sat, Mar 13, 2021 at 12:01 AM Kevin Chadwick <m8il1i...@gmail.com> wrote:
> I do not need to understand the difficulties that exist, though I struggle > to understand the zero bytes property as surely even an empty string has a > 4 or 8 byte pointer. > A string is effectively a struct{ data *byte len int } If you zero that, you get a nil pointer and a 0 length, which is a valid representation of an empty string. The same is true for every Go type. A nil pointer is all zero bytes (so pointing at address 0, which is not accessible), maps/funcs/chans are de-facto pointers (we often say "they are pointer-shaped"), so their zero value is also just a pointer to address 0. A nil slice is a struct { data *T len int cap int } which, if zeroed, points at 0 with length/capacity 0. And so on. The zero value of any Go type is just whatever it is represented as in memory, with all bytes set to 0. My original thinking was that either the function call or initialisation > could run make under the covers. >From Ian in those threads copied above "While not requiring the make call". > > Why not require make by calling it, behind the scenes? > But that is exactly the loss of "the zero value is all 0 bytes" we are talking about. It would mean if you write `var m map[int]int` the compiler needs to create instructions to initialize `m` to something that is not zero bytes (a.k.a. "call make behind the scenes"). And if you do `m := make([]map[int]int, 1e6)` it needs to run those instructions a million times - instead of just getting a block of 0-bytes from the runtime. Of course it's *possible* to abandon this property. Lots of languages (dare I say most languages) don't have it. We'd just prefer not to. > > -- > You received this message because you are subscribed to the Google Groups > "golang-nuts" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to golang-nuts+unsubscr...@googlegroups.com. > To view this discussion on the web visit > https://groups.google.com/d/msgid/golang-nuts/AAB589F5-DA04-4468-A745-77F9177EAF76%40gmail.com > . > -- You received this message because you are subscribed to the Google Groups "golang-nuts" group. To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/CAEkBMfGUjnqSnBmKZ%3DH6UHWmK%3DSFTigNLnYiBi%3DcT2Km2UzskQ%40mail.gmail.com.