FAQ
If I create an interface called Animal and then I create a named Animal
interface called LandAnimal, what does that allow me to do? I know you can
associate methods to types, but not interfaces so what is the point of
creating a named interface? Hopefully this makes sense what I'm asking. I'm
trying to write code samples that show what you can and can't do with
interfaces so I'm trying to find a reason why Go allows me to do so.

// Interface with a method set
type Animal interface {
     Speak() string
}

// Named interface
type LandAnimal Animal


I understand that for a struct to implement the Animal interface, it must
be associated, at a minimum, with the same method set as the interface. I
know there is not inheritance in Go, but what I don't understand is the
benefit to:

type LandAnimal Animal

What can I do now with LandAnimal that I can't do with Animal? What use
would I have for this declaration in Go besides aliasing a third party
interface?

I've already posted this question on reddit, but it was suggested to try
this forum.
http://www.reddit.com/r/golang/comments/2r146a/why_would_you_create_a_named_interface/

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

  • Geoffrey Teale at Jan 1, 2015 at 9:56 pm
    Hi Joseph,

    Named Interfaces essentially allow you to have a function that accepts any
    type that supports a given interface as an argument - you're setting up a
    contract that the compiler can check for you.

    I'm a bit rusty, but In a direct answer to your question, there's no
    functional value to the line "type LandAnimal Animal" , if that's even
    valid Go, it's doing nothing more than binding a new name to the existing
    interface.

    --
    Geoff
    On 1 January 2015 at 22:27, Joseph Spurrier wrote:

    If I create an interface called Animal and then I create a named Animal
    interface called LandAnimal, what does that allow me to do? I know you can
    associate methods to types, but not interfaces so what is the point of
    creating a named interface? Hopefully this makes sense what I'm asking. I'm
    trying to write code samples that show what you can and can't do with
    interfaces so I'm trying to find a reason why Go allows me to do so.

    // Interface with a method set
    type Animal interface {
    Speak() string
    }

    // Named interface
    type LandAnimal Animal


    I understand that for a struct to implement the Animal interface, it must
    be associated, at a minimum, with the same method set as the interface. I
    know there is not inheritance in Go, but what I don't understand is the
    benefit to:

    type LandAnimal Animal

    What can I do now with LandAnimal that I can't do with Animal? What use
    would I have for this declaration in Go besides aliasing a third party
    interface?

    I've already posted this question on reddit, but it was suggested to try
    this forum.

    http://www.reddit.com/r/golang/comments/2r146a/why_would_you_create_a_named_interface/

    --
    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.
  • Axel Wagner at Jan 1, 2015 at 9:58 pm
    Hi,

    http://golang.org/pkg/compress/zlib/#Writer
    implements
    http://golang.org/pkg/io/#Writer
    but also more. Just because you implement Interface A, does not mean,
    you can not have *more* methods than A. io.Reader and io.Writer can be
    regarded as the prototypical examples of what to use interfaces for.

    If your question is, what is the use of the interface Animal, if there
    is only one implementation (and you know there is only one
    implementation), the answer is "there is none". Which is why a common
    advice is, to not overdo the whole interface-business and only define
    interfaces, once you see common pattern emerge in your concrete
    implementations.

    If your question is, what the title suggests, i.e. why do
    type Reader interface {
          Read(b []byte) (n int, err error)
    }
    instead of just write an unnamed (or anonymous) interface whenever you
    want to refer to this interface, the answer is "save yourself a lot of
    typing" ;)

    Best,

    Axel Wagner

    --
    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.
  • Joseph Spurrier at Jan 1, 2015 at 10:12 pm
    Thanks guys, that makes sense. I just wanted to make sure I wasn't missing
    something like the ability to add additional methods to an interface that
    was already defined.
    On Thursday, January 1, 2015 4:58:46 PM UTC-5, Axel Wagner wrote:

    Hi,

    http://golang.org/pkg/compress/zlib/#Writer
    implements
    http://golang.org/pkg/io/#Writer
    but also more. Just because you implement Interface A, does not mean,
    you can not have *more* methods than A. io.Reader and io.Writer can be
    regarded as the prototypical examples of what to use interfaces for.

    If your question is, what is the use of the interface Animal, if there
    is only one implementation (and you know there is only one
    implementation), the answer is "there is none". Which is why a common
    advice is, to not overdo the whole interface-business and only define
    interfaces, once you see common pattern emerge in your concrete
    implementations.

    If your question is, what the title suggests, i.e. why do
    type Reader interface {
    Read(b []byte) (n int, err error)
    }
    instead of just write an unnamed (or anonymous) interface whenever you
    want to refer to this interface, the answer is "save yourself a lot of
    typing" ;)

    Best,

    Axel Wagner
    --
    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.
  • Dave Cheney at Jan 2, 2015 at 12:54 am
    What can I do now with LandAnimal that I can't do with Animal?
    Nothing. Interface equality is defined by the method sets of the interface
    alone. I could equally write

    func Talk(a interface{ Speak() string}) { ... }

    and the results would be the same.
    On Friday, 2 January 2015 08:27:32 UTC+11, Joseph Spurrier wrote:

    If I create an interface called Animal and then I create a named Animal
    interface called LandAnimal, what does that allow me to do? I know you can
    associate methods to types, but not interfaces so what is the point of
    creating a named interface? Hopefully this makes sense what I'm asking. I'm
    trying to write code samples that show what you can and can't do with
    interfaces so I'm trying to find a reason why Go allows me to do so.

    // Interface with a method set
    type Animal interface {
    Speak() string
    }

    // Named interface
    type LandAnimal Animal


    I understand that for a struct to implement the Animal interface, it must
    be associated, at a minimum, with the same method set as the interface. I
    know there is not inheritance in Go, but what I don't understand is the
    benefit to:

    type LandAnimal Animal

    What can I do now with LandAnimal that I can't do with Animal? What use
    would I have for this declaration in Go besides aliasing a third party
    interface?

    I've already posted this question on reddit, but it was suggested to try
    this forum.

    http://www.reddit.com/r/golang/comments/2r146a/why_would_you_create_a_named_interface/
    --
    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
postedJan 1, '15 at 9:27p
activeJan 2, '15 at 12:54a
posts5
users4
websitegolang.org

People

Translate

site design / logo © 2022 Grokbase