>From unmarshal doc: 1. To unmarshal JSON into a pointer, Unmarshal first handles the case of the JSON being the JSON literal null. In that case, Unmarshal sets the pointer to nil. Otherwise, Unmarshal unmarshals the JSON *into the value pointed at by the pointer*. *If the pointer is nil, Unmarshal allocates a new value for it to point to.*
2. To unmarshal a JSON array into a slice, Unmarshal resets the slice length to zero and then appends each element to the slice. The problem with it is that it's really hard to describe what it really does without looking at code. When the slice size is reset to zero it means that only the length of this slice will be set to 0. The underlying data is still there in memory and the capacity is still X. Now during slice expansion, the data is not reinitialized to 0 if that slice is still able to fit another element (number of elements < current capacity) because the pointer was initialized, previously. So during first unmarshal a new element is created, with V=0, P=nil. Unmarshal will set the pointer to some place in memory (1) so it can store int value in there. After first Unmarshal you have a slice newElems with length 1 and capacity 4 with V=10 and P=0xSOMETHING. The pointer is there so the pointed data will be overwritten for element [0], as described by documentation in section (1) There's a problem with section (2) because that's not exactly what happens during append. There was a proposition to re-initialize the slice element before each element is "appended" by unmarshal (so it'd work like "real" append does and be in sync with the doc) but it was rejected (and that's very good as it broken some code). Real behaviour of unmarshal can maybe be described better by saying that it allocates slice elements with zeroinit and overwrites slice elements when allocation isn't needed. -- 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/6bb2c42b-dbae-44db-af95-16633c327cf8n%40googlegroups.com.