Your turn
Define a Task with a Done field and a pointer method Finish that returns whether it actually changed the task. Call it twice on the same task, then print the two results and the final Done field.
package main
func main() {
// Build your solution here.
}Hint 1: the idea
Check the current state before writing the new one.
Hint 2: a first step
Return false immediately when Done is already true.
Worked solution
package main
import (
"fmt"
)
type Task struct{ Done bool }
func (t *Task) Finish() bool {
if t.Done {
return false
}
t.Done = true
return true
}
func main() {
task := Task{}
fmt.Println(task.Finish())
fmt.Println(task.Finish())
fmt.Println(task.Done)
}Text
true
false
trueThe second call finds the task already done and returns false without writing. The queue's Finish uses the same check to reject a repeated done.
Completion is saved in this browser only. It does not measure mastery.