Solution Review: Make a Simple Interface

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

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
}

Get hands-on with 1400+ tech skills courses.