On Fri, Oct 13, 2017 at 5:41 AM, Cholerae Hu <cholerae...@gmail.com> wrote:
> 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 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() ?

Composite literals are not addressable.  The section of the spec you
are citing lists the addressable objects and then says that, as an
exception, the address operator '&' may be applied to a composite
literal even though they are not addressable.

Ian

-- 
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