go - By reference or value -
if had instance of following struct
type node struct { id string name string address string conn net.conn enc json.encoder dec json.decoder in chan *command out chan *command clients map[string]clientnodescontainer }
i failing understand when should send struct reference , when should send value(considering not want make changes instance), there rule of thumb makes easier decide?
all find send struct value when small or inexpensive copy, small mean smaller 64bit address example?
would glad if can point more obvious rules
the rule simple:
there no concept of "pass/send reference" in go, can pass value.
regarding question whether pass value of struct or pointer struct (this not call reference!):
- if want modify value inside function or method: pass pointer.
- if not want modify value:
- if struct large: use pointer.
- otherwise: doesn't matter.
all thinking how copy costs wasted time. copies cheap, medium sized structs. passing pointer might suitable optimization after profiling.
your struct not large. large struct contains fields wholeworldbuf [1000000]uint64
. tiny structs yours might or might not benefit passing pointer , gives advice 1 better lying: depends on code , call patterns.
if run out of sensible options , profiling shows time spent copying structs: experiment pointers.
Comments
Post a Comment