Golang interfaces: Difference between revisions

From wikinotes
No edit summary
Line 6: Line 6:
= Basics =
= Basics =
<blockquote>
<blockquote>
In go, many interfaces expose just a single method, and are named after the method they expose.<br>
Libraries do not need to expose interfaces in go, you can create them for the subset of methods that are useful to you.
Frequently, interfaces are implemented on structs (but they can be for any type).
 
* An interface is a promise that an object implements a set of method-signatures.
* objects are automatically a part of an interface if they have methods, there is no <code>implements Foo</code>
* Empty interfaces match all oject types in go. (ex <code>fmt.Println()</code> can accept any type)


<syntaxhighlight lang="go">
<syntaxhighlight lang="go">
// declare an interface
// rectangle.go
type Writer interface {
type Rectangle struct {
     Write([]byte) (int, error)
     width int
    height int
}
 
func (rect Rectangle) Area() int {
    return rect.width * rect.height
}
}
</syntaxhighlight>
</syntaxhighlight>


<syntaxhighlight lang="go">
<syntaxhighlight lang="go">
// implement an interface (automatic if methods match)
// area_calculator.go
type PrinterWriter struct {}
type AreaCalculator interface {
 
    Area() int
func (w PrinterWriter) Write(data []byte) (int, error) {
    // ...
}
}
</syntaxhighlight>
</syntaxhighlight>


<syntaxhighlight lang="go">
<syntaxhighlight lang="go">
var w Writer = PrinterWriter{} // <-- type 'Writer'
// main.go
w.Write([]byte("Foobar"))
func LargestArea(a, b AreaCalculator) AreaCalculator {
    if a.Area() > b.Area() {
        return a
    }
    return b
}
 
func main() {
    rect1 := Rectangle{5, 5}
    rect2 := Rectangle{2, 10}
    fmt.Println(LargestArea(rect1, rect2) == rect1)
}
</syntaxhighlight>
</syntaxhighlight>
Libraries do not need to expose interfaces in go, you can create them for the subset of methods that are useful to you.
</blockquote><!-- Basics -->
</blockquote><!-- Basics -->



Revision as of 14:43, 6 June 2022

Similar to other languages, interfaces in go define a contract of method-signatures that implementors must have.
Unlike other languages, golang interfaces are implicit -- an object with all of the required methods automatically satisfies an interface.

Similar to other languages, a param can be typed to accept an interface, which abstracts the actual type that is received.

Basics

Libraries do not need to expose interfaces in go, you can create them for the subset of methods that are useful to you.

  • An interface is a promise that an object implements a set of method-signatures.
  • objects are automatically a part of an interface if they have methods, there is no implements Foo
  • Empty interfaces match all oject types in go. (ex fmt.Println() can accept any type)
// rectangle.go
type Rectangle struct {
    width int
    height int
}

func (rect Rectangle) Area() int {
    return rect.width * rect.height
}
// area_calculator.go
type AreaCalculator interface {
    Area() int
}
// main.go
func LargestArea(a, b AreaCalculator) AreaCalculator {
    if a.Area() > b.Area() {
        return a
    }
    return b
}

func main() {
    rect1 := Rectangle{5, 5}
    rect2 := Rectangle{2, 10}
    fmt.Println(LargestArea(rect1, rect2) == rect1)
}

Embedding

Like structs, interfaces can be embedded.

type Walker interface {
    Walk nil
}

type BubblegumChewer interface {
    ChewGum nil
}

// includes all methods from previous two interfaces
type BubblegumChewingWalker interface {
    Walker
    BubblegumChewer
}