Note that there are some explanatory texts on larger screens.

plurals
  1. POGo receiver methods calling syntax confusion
    primarykey
    data
    text
    <p>I was just reading through <a href="http://golang.org/doc/effective_go.html" rel="nofollow">Effective Go</a> and in the <a href="http://golang.org/doc/effective_go.html#pointers_vs_values" rel="nofollow">Pointers vs. Values</a> section, near the <strong>end</strong> it says:</p> <blockquote> <p>The rule about pointers vs. values for receivers is that value methods can be invoked on pointers and values, but pointer methods can only be invoked on pointers. This is because pointer methods can modify the receiver; invoking them on a copy of the value would cause those modifications to be discarded.</p> </blockquote> <p>To test it, I wrote this:</p> <pre class="lang-go prettyprint-override"><code>package main import ( "fmt" "reflect" ) type age int func (a age) String() string { return fmt.Sprintf("%d yeasr(s) old", int(a)) } func (a *age) Set(newAge int) { if newAge &gt;= 0 { *a = age(newAge) } } func main() { var vAge age = 5 pAge := new(age) fmt.Printf("TypeOf =&gt;\n\tvAge: %v\n\tpAge: %v\n", reflect.TypeOf(vAge), reflect.TypeOf(pAge)) fmt.Printf("vAge.String(): %v\n", vAge.String()) fmt.Printf("vAge.Set(10)\n") vAge.Set(10) fmt.Printf("vAge.String(): %v\n", vAge.String()) fmt.Printf("pAge.String(): %v\n", pAge.String()) fmt.Printf("pAge.Set(10)\n") pAge.Set(10) fmt.Printf("pAge.String(): %v\n", pAge.String()) } </code></pre> <p>And it compiles, even though the document says it shouldn't since the pointer method <code>Set()</code> should not be invocable through the value var <code>vAge</code>. Am I doing something wrong here?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload