...

/

Solution Review: Make a Simple Interface

Solution Review: Make a Simple Interface

This lesson discusses the solution to the challenge given in the previous lesson.

We'll cover the following...
Press + to interact
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, from line 6 to line 9, we define the interface Simpler by specifying the signature of the functions Get and Set. The Get() function returns an integer, and the Set(int) function takes an integer as a parameter. Note that because these are only function descriptions, we do not have to specify variables’ names here.

The type Simple itself is defined ...