Golang style question: When writing a function that returns a struct with data, is it better to return the struct by value or by pointer? It seems to me that returning by pointer will mean it will escape to the heap?
-
-
The best way I've found to think about it is: If you use copy-by-value, you're tying the compiler's hands: that memory *will* be copied; that's the contract you asked for. If you use references, you've made the compiler much more free to decide where the memory lives.
-
As I write performance sensitive software, I find the best patterns of use are to have `func (x *X) Init()` rather than `func ConstructX() *X`. The init-a-ptr pattern continues to work well if I take a bunch of X structs and bulk alloc them with `type Y{ a, b, c X}` or `[20]X`.
- Još 14 drugih odgovora
Novi razgovor -
Čini se da učitavanje traje već neko vrijeme.
Twitter je možda preopterećen ili ima kratkotrajnih poteškoća u radu. Pokušajte ponovno ili potražite dodatne informacije u odjeljku Status Twittera.