Open yonatonreid opened 6 years ago
@yonatonreid In first way, it returns a slice which element is a pointer to Feed
. In for statement ,you can get a copy of each element, each size of slice is 24 bytes ; in second way, it returns a pointer , you also can return document
, but size of ptr is 8 bytes, much more efficient
The decision to use value or pointer semantics is never based on the data being 8 or 24 bytes in length. Data of those sizes are basically the same once you factor in all the different granularities of how data is moved around. Email me bill@ardanlabs.com if you want to talk about this more.
As a note, it was a mistake to use a slice of pointers to a Feed in this program. By the time I caught it the book was ready to be published :(. The mistake was not because of the size of a Feed value.
@ardan-bkennedy @yonatonreid
I just had exactly the same question in golang general slack and the answer I received is the follows:
Pointers are used to avoid translating nulls (in JSON) to empty strings ("the billion-dollar mistake").
Examples: https://play.golang.org/p/TQ6GGBKK-gA vs. https://play.golang.org/p/ibhTYaKm6Vf
In this file:
https://github.com/goinaction/code/blob/master/chapter2/sample/search/feed.go
We have this code:
However in this file:
https://raw.githubusercontent.com/goinaction/code/master/chapter2/sample/matchers/rss.go
I don't understand why they are handled the way they are handled. Can you explain?