FAQ
package main

import "fmt"

type Animal interface {
Talk()
Move()
}

type Dog interface {
Animal
}

type Shepherd struct {
Dog
}

func New() Animal {
return &Shepherd{}
}

func (p *Shepherd) Talk() {
fmt.Println("woof!")
}

func main() {
fmt.Println("Hello, playground")

fido := New()

fido.Talk()
fido.Move()
}

woof!panic: runtime error: invalid memory address or nil pointer dereference
[signal 0xb code=0x1 addr=0x20 pc=0x1023e57]

goroutine 1 [running]:
main.(*Shepherd).Move(0xc010036160)
  /tmpfs/gosandbox-3b4927a9_89b3d8f1_41a3ab00_1dfeee01_401631ac/prog.go:1 +0x27
main.main()
  /tmpfs/gosandbox-3b4927a9_89b3d8f1_41a3ab00_1dfeee01_401631ac/prog.go:30 +0x85
  [process exited with non-zero status]


http://play.golang.org/p/m4ehAN2xFR


But, when I remove the Dog interface from the Shepherd struct (http://play.golang.org/p/BDfYULmjU4):


type Shepherd struct {

      // Dog

}


It gives me a compile error as expected:


prog.go:19: cannot use Shepherd literal (type *Shepherd) as type Animal in return argument:
  *Shepherd does not implement Animal (missing Move method)
  [process exited with non-zero status]



In other words: what exactly happens when I implement an interface in a struct? Is this a bug or expected behavior?

--
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 [email protected].
For more options, visit https://groups.google.com/groups/opt_out.

Search Discussions

  • Jesse McNelis at Aug 30, 2013 at 5:48 am

    On Fri, Aug 30, 2013 at 3:32 PM, Melvin Tercan wrote:

    type Dog interface {
    Animal
    }

    type Shepherd struct {
    Dog
    }

    In other words: what exactly happens when I implement an interface in a struct? Is this a bug or expected behavior?
    You're not 'implementing' the interface, you're embedding the interface.
    Just like all embedding, methods on the embedded type get promoted to be
    callable from the embedding type.

    So Shepherd has the methods of the Dog interface, but you didn't
    instantiate the Dog with something that implements Dog when you
    instantiated the Shepherd so it was still nil.

    You need a type that implements Dog so you can instantiate it in your New()
    function.
    eg.

    type Dogman struct{}
    func (p *Dogman) Talk() {
    fmt.Println("woof!")
    }

    func (p *Dogman) Move() {
    fmt.Println("run!")
    }

    func New() Animal {
    return &Shepherd{new(Dogman)}
    }

    --
    =====================
    http://jessta.id.au

    --
    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 [email protected].
    For more options, visit https://groups.google.com/groups/opt_out.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedAug 30, '13 at 5:32a
activeAug 30, '13 at 5:48a
posts2
users2
websitegolang.org

2 users in discussion

Jesse McNelis: 1 post Melvin Tercan: 1 post

People

Translate

site design / logo © 2023 Grokbase