package main

import (
    "fmt"
    "reflect"
)

type Circle struct {
    radius float64
}

func (c *Circle) DummyMethod() { 
    fmt.Println("Type of receiver:", reflect.TypeOf(c))
}

func main() {
    // error: cannot take the address of Circle literal
    // Circle{radius: 1.0}.DummyMethod()
    (&Circle{radius: 1.0}).DummyMethod()
}

https://golang.org/ref/spec#Calls 

>  If x is addressable <https://golang.org/ref/spec#Address_operators> and 
> &x's method set contains m, x.m() is shorthand for (&x).m()


https://golang.org/ref/spec#Address_operators
Composite literals is addressable.

So why can't I call DummyMethod like Circle{radius: 1.0}.DummyMethod() ?

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to