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!):

  1. if want modify value inside function or method: pass pointer.
  2. if not want modify value:
    1. if struct large: use pointer.
    2. 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

Popular posts from this blog

javascript - Jquery show_hide, what to add in order to make the page scroll to the bottom of the hidden field once button is clicked -

javascript - Highcharts multi-color line -

javascript - Enter key does not work in search box -