Search⌘ K
AI Features

Solution Review: Make a Simple Interface

Understand how to create a simple interface in Go by defining method signatures and implementing them with pointer receiver methods on a struct. Explore how to use this interface in functions and test it with practical examples.

We'll cover the following...
Go (1.6.2)
package main
import (
"fmt"
)
type Simpler interface { // interface implementing functions called on Simple struct
Get() int
Set(int)
}
type Simple struct {
i int
}
func (p *Simple) Get() int {
return p.i
}
func (p *Simple) Set(u int) {
p.i = u
}
func fI(it Simpler) int { // function calling both methods through interface
it.Set(5)
return it.Get()
}
func main() {
var s Simple
fmt.Println(fI(&s)) // &s is required because Get() is defined with a receiver type pointer
}

In the code above, ...