Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Interfaces vs struct methods

I have code with interface:

package main

import (
    "math"
    "fmt"
)

type Circle struct {
    x, y, r float64
}

type Rectangle struct {
    x1, y1, x2, y2 float64
}

type Figure interface {
    Area() float64
}

func (c *Circle) Area() float64 {
    return math.Pi * c.r * c.r
}

func (r *Rectangle) Area() float64 {
    return math.Abs(r.x2 - r.x1) * math.Abs(r.y2 - r.y1)
}

func main() {
    figures := make([]Figure, 0)
    figures = append(figures, &Circle{0, 0, 10})
    figures = append(figures, &Rectangle{0, 0, 10, 20})
    for _, figure := range figures {
        fmt.Print(figure.Area(), "\n")
    }
}

output:

314.159265
200

and code with only methods for my struct:

package main

import (
    "math"
    "fmt"
)

type Circle struct {
    x, y, r float64
}

type Rectangle struct {
    x1, y1, x2, y2 float64
}

func (c *Circle) Area() float64 {
    return math.Pi * c.r * c.r
}

func (r *Rectangle) Area() float64 {
    return math.Abs(r.x2 - r.x1) * math.Abs(r.y2 - r.y1)
}

func main() {
    c := Circle{0,0,10}
    r := Rectangle{0,0,10,20}

    fmt.Print(c.Area(), "\n")
    fmt.Print(r.Area(), "\n")
}

and the same output:

314.1592653589793
200

When I use interface I have extra code in the form of interfaces declaration. If the interfaces perfectly implement polymorphism in Go, why then the methods of structures? What plus from interfaces, where is the difference? May be my example not good. Thank you!

like image 326
rtut Avatar asked Apr 12 '18 13:04

rtut


People also ask

What is the difference between interface and struct?

Structs and interfaces are Go's way of organizing methods and data handling. Where structs define the fields of an object, like a Person's first and last name. The interfaces define the methods; e.g. formatting and returning a Person's full name.

Is an interface a struct?

Since the interface is a type just like a struct, we can create a variable of its type. In the above case, we can create a variable s of type interface Shape . đź’ˇ Like a struct type, there is absolutely no need to create a derived type for an interface.

Can a struct have an interface?

A class or struct that implements an interface must provide an implementation for all declared members without a default implementation provided by the interface. However, if a base class implements an interface, any class that's derived from the base class inherits that implementation.

How do you declare a struct interface?

Like a struct an interface is created using the type keyword, followed by a name and the keyword interface . But instead of defining fields, we define a “method set”. A method set is a list of methods that a type must have in order to “implement” the interface.

What is [] interface {} Golang?

The interface type that specifies zero methods is known as the empty interface: interface{} An empty interface may hold values of any type. (Every type implements at least zero methods.) Empty interfaces are used by code that handles values of unknown type.

Can struct implement interface Golang?

In the above program, we created a Motorvehicle interface with the Mileage method and a Newq interface with the AverageSpeed method. Struct type BMW implements both the methods, hence it implements both the interfaces. Hence we can assign a value of struct type BMW to the variable of type Motorvehicle or Newq.


1 Answers

You should see it in your own code: in the first case you could handle all in unity, as values of Figure and so you could store them in a slice (of type []Figure), and range over them and call their Area() method.

In the 2nd case without interfaces you did not store them in a slice and you did not use a loop, instead you had to call Area() manually on each instance.

Without interfaces there is no type for which to create a slice of and store each in it. The only option would be the interface{} type:

figures := make([]interface{}, 0)
figures = append(figures, &Circle{0, 0, 10})
figures = append(figures, &Rectangle{0, 0, 10, 20})

But then when ranging over them, you wouldn't be able to call their Area() method, as the interface{} type does not define any methods.

for _, figure := range figures {
    fmt.Print(figure.Area(), "\n") // COMPILE-TIME error
}

If you only have 2 instances, you can call their Area() methods manually, it would be even shorter. But if you have a hundred or a thousand...

Without repeating advantages of interfaces, see possible duplicate:
Why are interfaces needed in Golang?

like image 73
icza Avatar answered Oct 05 '22 18:10

icza