FAQ
Why in this case is the interface not satisfied when I can call the method
directly, ( user1.SendEmail() ), on the same value? I would think this
would not be possible regardless if I am using an interface or the value
directly.

This seems to be the rule when the interface is involved?
If S contains an anonymous field T, the method sets of S does not include
promoted methods with receiver *T

Here is where I have been looking in the spec:
http://golang.org/ref/spec#Struct_types
http://golang.org/ref/spec#Method_sets
http://golang.org/ref/spec#Interface_types

/*
./main.go:51: cannot use user1 (type Admin) as type Notifications in
function argument:
Admin does not implement Notifications (SendEmail method has pointer
receiver)
*/

package main

import (
"log"
)

type User struct {
Name string
Email string
Phone string
}

type Admin struct {
User
Level string
}

type Notifications interface {
SendEmail()
}

func SendEmail(notify Notifications) {
notify.SendEmail()
}

func (u *User) SendEmail() {
if u.Email != "" {
log.Printf("User: Sending User Email To %s<%s>\n", u.Name, u.Email)
}
}

func main() {
user1 := Admin{
User: User{
Name: "janet jones",
Email: "janet@email.com",
},
Level: "super",
}

user2 := &Admin{
User: User{
Name: "jill smith",
Email: "jill@email.com",
},
Level: "super",
}

         user1.SendEmail() // Works
SendEmail(user1)
SendEmail(user2)
}

--
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+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Search Discussions

  • Caleb Spare at Apr 29, 2014 at 11:29 pm
    A play.golang.org example would be nicer (then we can see the error
    more easily).

    The part you're missing is at http://golang.org/ref/spec#Calls:

    "If x is addressable and &x's method set contains m, x.m() is
    shorthand for (&x).m()"

    This is what's happening when you call user1.SendEmail().

    On Tue, Apr 29, 2014 at 3:34 PM, William Kennedy
    wrote:
    Why in this case is the interface not satisfied when I can call the method
    directly, ( user1.SendEmail() ), on the same value? I would think this would
    not be possible regardless if I am using an interface or the value directly.

    This seems to be the rule when the interface is involved?
    If S contains an anonymous field T, the method sets of S does not include
    promoted methods with receiver *T

    Here is where I have been looking in the spec:
    http://golang.org/ref/spec#Struct_types
    http://golang.org/ref/spec#Method_sets
    http://golang.org/ref/spec#Interface_types

    /*
    ./main.go:51: cannot use user1 (type Admin) as type Notifications in
    function argument:
    Admin does not implement Notifications (SendEmail method has pointer
    receiver)
    */

    package main

    import (
    "log"
    )

    type User struct {
    Name string
    Email string
    Phone string
    }

    type Admin struct {
    User
    Level string
    }

    type Notifications interface {
    SendEmail()
    }

    func SendEmail(notify Notifications) {
    notify.SendEmail()
    }

    func (u *User) SendEmail() {
    if u.Email != "" {
    log.Printf("User: Sending User Email To %s<%s>\n", u.Name, u.Email)
    }
    }

    func main() {
    user1 := Admin{
    User: User{
    Name: "janet jones",
    Email: "janet@email.com",
    },
    Level: "super",
    }

    user2 := &Admin{
    User: User{
    Name: "jill smith",
    Email: "jill@email.com",
    },
    Level: "super",
    }

    user1.SendEmail() // Works
    SendEmail(user1)
    SendEmail(user2)
    }

    --
    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+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
    --
    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+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • William Kennedy at Apr 29, 2014 at 11:31 pm

    On Apr 29, 2014, at 7:29 PM, Caleb Spare wrote:

    A play.golang.org example would be nicer (then we can see the error
    more easily).
    I will remember that for next time.
    The part you're missing is at http://golang.org/ref/spec#Calls:

    "If x is addressable and &x's method set contains m, x.m() is
    shorthand for (&x).m()"

    This is what's happening when you call user1.SendEmail().
    Thanks for the reference, I couldn't find it :)
    On Tue, Apr 29, 2014 at 3:34 PM, William Kennedy
    wrote:
    Why in this case is the interface not satisfied when I can call the method
    directly, ( user1.SendEmail() ), on the same value? I would think this would
    not be possible regardless if I am using an interface or the value directly.

    This seems to be the rule when the interface is involved?
    If S contains an anonymous field T, the method sets of S does not include
    promoted methods with receiver *T

    Here is where I have been looking in the spec:
    http://golang.org/ref/spec#Struct_types
    http://golang.org/ref/spec#Method_sets
    http://golang.org/ref/spec#Interface_types

    /*
    ./main.go:51: cannot use user1 (type Admin) as type Notifications in
    function argument:
    Admin does not implement Notifications (SendEmail method has pointer
    receiver)
    */

    package main

    import (
    "log"
    )

    type User struct {
    Name string
    Email string
    Phone string
    }

    type Admin struct {
    User
    Level string
    }

    type Notifications interface {
    SendEmail()
    }

    func SendEmail(notify Notifications) {
    notify.SendEmail()
    }

    func (u *User) SendEmail() {
    if u.Email != "" {
    log.Printf("User: Sending User Email To %s<%s>\n", u.Name, u.Email)
    }
    }

    func main() {
    user1 := Admin{
    User: User{
    Name: "janet jones",
    Email: "janet@email.com",
    },
    Level: "super",
    }

    user2 := &Admin{
    User: User{
    Name: "jill smith",
    Email: "jill@email.com",
    },
    Level: "super",
    }

    user1.SendEmail() // Works
    SendEmail(user1)
    SendEmail(user2)
    }

    --
    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+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
    --
    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+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • William Kennedy at Apr 29, 2014 at 11:43 pm
    So is this a true statement:

    When a method is called via an interface and the value supporting the interface is a pointer, the method must be declared with a receiver of a pointer of that type. When the value supporting the interface is a value, the method can be declared with a receiver of the values type or a pointer of that type.

    On Apr 29, 2014, at 7:29 PM, Caleb Spare wrote:

    A play.golang.org example would be nicer (then we can see the error
    more easily).

    The part you're missing is at http://golang.org/ref/spec#Calls:

    "If x is addressable and &x's method set contains m, x.m() is
    shorthand for (&x).m()"

    This is what's happening when you call user1.SendEmail().
    --
    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+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • William Kennedy at Apr 29, 2014 at 11:57 pm
    And then I can conclude that since this is NOT part of the spec: (http://golang.org/ref/spec#Struct_types)

    * If S contains an anonymous field T, the method sets of S include promoted methods with receiver *T.

    The method I have (p *T) is not promoted and therefore I am receiving a compiler error?

    -- Bill
    On Apr 29, 2014, at 7:43 PM, William Kennedy wrote:

    So is this a true statement:

    When a method is called via an interface and the value supporting the interface is a pointer, the method must be declared with a receiver of a pointer of that type. When the value supporting the interface is a value, the method can be declared with a receiver of the values type or a pointer of that type.
    --
    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+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedApr 29, '14 at 10:34p
activeApr 29, '14 at 11:57p
posts5
users2
websitegolang.org

2 users in discussion

William Kennedy: 4 posts Caleb Spare: 1 post

People

Translate

site design / logo © 2022 Grokbase