Skip to content

Checkpoint: Design a Small Interface

The Course
•
30 min study

Today's lessons each used an interface someone else designed: Labeled, Finisher, io.Writer. This checkpoint asks you to design one. The consumer is new: a reminder service that tells people about overdue tasks. In the real program it would send email, but you don't want a check that sends email, so the function should depend on a small interface that a check can satisfy with a recording type.

The problem

remind(s Sender, tasks []Task, today int) error sends one message, Overdue: and the title, for each open task whose due day is before today, in slice order. It returns the first send error and stops there. You decide what Sender contains. Keep it as small as remind allows.

To check it, write a recorder type that satisfies Sender by appending each message to a slice field. Then run remind with a recorder and print what it recorded.

Before you start

  • What does remind need to do with a sender, and what does it need back? That is your interface.
  • Recording a message changes the recorder. Which receiver does its method need, and so what do you pass to remind?
  • Which of the starter's tasks should produce a message on day 5?
Exercise

Your turn

Define Sender, write remind, and write recorder. With the starter's tasks and today set to 5, the program prints:

Text
Overdue: Pay invoice
Overdue: Call Sam
2 messages
Your working space Go
package main

import (
	"fmt"
)

type Task struct {
	Title string
	Due   int
	Done  bool
}

func main() {
	tasks := []Task{
		{Title: "Pay invoice", Due: 2},
		{Title: "Book room", Due: 4, Done: true},
		{Title: "Call Sam", Due: 3},
		{Title: "Plan trip", Due: 9},
	}
	fmt.Println(len(tasks))
}
Hint 1: the idea

List what remind does with a sender, and what it needs back each time. Each thing it does is one method of the interface, and nothing else belongs in it.

Hint 2: a first step

Write remind against your interface first, with no overdue rule yet: it sends something for every task. Then write the smallest fake that keeps what it receives, pass it to remind, and check that four messages arrive before you add the rule.

Worked solution
Run the solution Go
package main

import (
	"fmt"
)

type Task struct {
	Title string
	Due   int
	Done  bool
}

type Sender interface {
	Send(msg string) error
}

func remind(s Sender, tasks []Task, today int) error {
	for _, t := range tasks {
		if t.Done || t.Due >= today {
			continue
		}
		if err := s.Send("Overdue: " + t.Title); err != nil {
			return err
		}
	}
	return nil
}

type recorder struct {
	sent []string
}

func (r *recorder) Send(msg string) error {
	r.sent = append(r.sent, msg)
	return nil
}

func main() {
	tasks := []Task{
		{Title: "Pay invoice", Due: 2},
		{Title: "Book room", Due: 4, Done: true},
		{Title: "Call Sam", Due: 3},
		{Title: "Plan trip", Due: 9},
	}
	var rec recorder
	if err := remind(&rec, tasks, 5); err != nil {
		fmt.Println(err)
		return
	}
	for _, msg := range rec.sent {
		fmt.Println(msg)
	}
	fmt.Println(len(rec.sent), "messages")
}
Text
Overdue: Pay invoice
Overdue: Call Sam
2 messages

Sender has one method because remind uses one. An email sender, a chat sender and the recorder can all satisfy it without remind knowing which it got. Send needs a pointer receiver because it appends to sent. With a value receiver, remind(rec, ...) would compile, every message would go to a copy, and the program would print 0 messages. Book room is skipped because it is done, and Plan trip because day 9 is not before day 5.

Was this article helpful?

Your response is saved on this device.