Usage of interface in Go

后端 未结 4 1771
名媛妹妹
名媛妹妹 2020-11-27 18:26

I would like to understand the interface type with a simple example of it\'s use in Go (Language).

I read the web documentation, but I don\'t get it.

相关标签:
4条回答
  • 2020-11-27 18:32

    The idea behind go interfaces is duck typing. Which simply translates into: If you look like a duck and quack like a duck then you are a duck. Meaning that if your object implements all duck's features then there should be no problem using it as a duck. Here is an example:

    package main
    
    import (
        "fmt"
    )
    
    type Walker interface {
        Walk() string
    }
    
    type Human string
    type Dog string
    
    func (human Human) Walk() string { //A human is a walker
        return "I'm a man and I walked!"
    }
    
    func (dog Dog) Walk() string { //A dog is a walker
        return "I'm a dog and I walked!"
    }
    
    //Make a walker walk
    func MakeWalk(w Walker) {
        fmt.Println(w.Walk())
    }
    
    func main() {
        var human Human
        var dog Dog
        MakeWalk(human)
        MakeWalk(dog)
    }
    

    Here a Human is a Walker and a Dog is a Walker. Why? Because they both.. well... Walk. They both implement the Walk () string function. So this is why you can execute MakeWalk on them.

    This is very helpful when you want different types to behave in the same manner. A practical example would be file type objects (sockets, file objects) - you need a Write and a Read function on all of them. Then you can use Write and Read in the same fashion independent of their type - which is cool.

    0 讨论(0)
  • 2020-11-27 18:47

    In this example, I'm using the interface to demonstrate how to achieve polymorphism in Golang.

    package main 
    
    
    import(
        "fmt"
        "math"
    )
    
    func main(){
        rect := Rectangle{20,50}
        cir := Circle{2}
    
        //According to object you passed in getArea method, 
        // it will change the behaviour and that is called Polymorphism.
        fmt.Println("Area of Rectangle =",getArea(rect))
        fmt.Println("Area of Circle =",getArea(cir))
    
    }
    
    //Interface Shape with one area method
    type Shape interface{
         area() float64
    }
    
    //Creating Rectangle and Circle type using struct 
    type Rectangle struct{
        height float64
        width float64
    }
    
    type Circle struct{
        radius float64
    }
    
    //Receiver function, which implements struct's area methods
    func(r Rectangle) area() float64{
        return r.height * r.width
    }
    
    func(c Circle) area() float64{
        return math.Pi * math.Pow(c.radius,2)
    }
    
    //passing interface as arguments, which can calculate shape of any mentioned type
    //All the struct are tied together because of the Interface.
    func getArea(shape Shape) float64{
        return shape.area()
    }
    
    0 讨论(0)
  • 2020-11-27 18:48

    To add to the excellent answer by @AlexPlugaru here and by @NickCraig-Wood at https://stackoverflow.com/a/18854285/12817546.

    package main
    
    import . "fmt"
    
    func main() {
        cat("Bird").Eat()      // Bird
        eater.Eat(cat("Milk")) // Milk
        Break(cat("Fish"))     // Fish
        Lunch().Eat()          // Mice
        Tea(true)           // Bird
    }
    
    type cat string
    type eater interface{ Eat() }
    
    func (c cat) Eat()  { Println(c) }
    func Break(e eater) { e.Eat() }
    func Lunch() eater  { return cat("Mice") }
    func Tea(b bool) {
        if b {
            cat("Bird").Eat()
        }
    }
    

    Methods on a struct or any other concrete type are resolved statically. See cat("Bird").Eat() and eater.Eat(cat("Milk")). The only way to have dynamically dispatched methods is through an interface. See Break(cat("Fish")) and Lunch().Eat(), http://www.golangbr.org/doc/faq#How_do_I_get_dynamic_dispatch_of_methods.

    Tea(true) calls a method dynamically without using an interface. See https://talks.golang.org/2014/taste.slide#19. But interfaces—by design and convention—encourage us to write composable code. See https://talks.golang.org/2014/go4gophers.slide#21.

    The result: simple pieces connected by small interfaces. See https://talks.golang.org/2012/chat.slide#5. Better to have many small simple things than one big complex thing. See https://talks.golang.org/2014/go4gophers.slide#24.

    You can also hide anything in a private package and just expose specific types, interfaces, and factory functions. See Break(cat("Fish")) and Lunch().Eat(), https://code.tutsplus.com/tutorials/lets-go-object-oriented-programming-in-golang--cms-26540

    0 讨论(0)
  • 2020-11-27 18:51

    Another working example showing the interaction between an interface and a structure

    package main
    
    import "fmt"
    
    type Info interface {
    Noofchar() int
    Increment()
    }
    
    type Testinfo struct {
    noofchar int     
    }
    
    func (x *Testinfo) Noofchar() int {
    return x.noofchar
    }
    func (x *Testinfo) Increment() {
    x.noofchar++
    }
    
    func main(){
    var t Info = &Testinfo{noofchar:1}
    fmt.Println("No of char ",t.Noofchar())
    t.Increment()
    fmt.Println("No of char ",t.Noofchar())
    }
    
    0 讨论(0)
提交回复
热议问题