Solution Review: Make a Simple Interface

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

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
}

Get hands-on with 1200+ tech skills courses.