Putting It All Together
Part of the Logic & Flow section of Coddy's GO journey. Lesson 25 of 68.
Challenge
EasyTime to finish your task list application. In this last step you bring together everything from the previous lessons: the Task struct, addTask, viewAllTasks, completeTask and removeTask, driven by a session of menu actions.
You will receive three inputs:
- A string with the application name and the user name separated by a comma (e.g.,
"MyTasks,Alice") - A string with the tasks the list starts with, in the format
"name1:status1,name2:status2"where status is"true"or"false"(e.g.,"Write report:false,Call dentist:true"). This line is empty when the list starts out empty. - A string with the actions of the session, separated by commas. An action is either its name on its own or its name and one parameter joined by
|(e.g.,"add|Buy groceries,view,complete|2,remove|0,exit"). The action list always ends with"exit".
Task names contain spaces, so read all three lines with bufio.Scanner, as in the previous lessons.
Your task is to:
- Define the same
Taskstruct withName(string) andCompleted(bool) fields - Bring back the four functions you already wrote, with the same signatures:
addTask(tasks []Task, taskName string) []TaskviewAllTasks(tasks []Task), which prints"[x] [task_name]"for a completed task and"[ ] [task_name]"for one that is still opencompleteTask(tasks *[]Task, index int)removeTask(tasks []Task, index int) []Task
- Build the starting list from the second input, remembering that an empty line means an empty list
- Print the welcome message and the menu, exactly as in the first step of the project:
"Welcome to [app_name], [user_name]!""1. Add Task""2. View Tasks""3. Complete Task""4. Remove Task""5. Exit""Current tasks: [count]"
- Run the actions in order. Every action prints its header first:
"add": print"--- ADD TASK ---", add the task named by the parameter, then print"Task '[task_name]' added!""view": print"--- VIEW TASKS ---", list every task withviewAllTasks, then print the summary line"complete": print"--- COMPLETE TASK ---", mark the task at that index, then print"Task '[task_name]' marked as completed!""remove": print"--- REMOVE TASK ---", remove the task at that index, then print"Task '[task_name]' removed successfully!""exit": print"--- EXIT ---", then"Final list:", then the whole list, then the summary line, and finally"Goodbye, [user_name]!". Nothing runs after exit.
- Indexes are 0 based, as in the previous lessons. When a
"complete"or"remove"index is outside the list, print"Invalid task number"instead and leave the list unchanged. - The summary line is the one you already know:
"Total: [total_count] tasks ([completed_count] completed, [incomplete_count] remaining)"
Use the strings package to split the input lines and the strconv package to turn an index into a number. This final step shows how the small functions you wrote one at a time add up to a working program.
Try it yourself
package main
import (
"bufio"
"fmt"
"os"
"strconv"
"strings"
)
type Task struct {
Name string
Completed bool
}
// TODO: Bring back addTask, viewAllTasks, completeTask and removeTask
// from the previous lessons of this project
func main() {
// Create scanner for reading input lines
scanner := bufio.NewScanner(os.Stdin)
// Read the application name and the user name
scanner.Scan()
names := scanner.Text()
// Read the tasks the list starts with
scanner.Scan()
taskData := scanner.Text()
// Read the actions of the session
scanner.Scan()
actionData := scanner.Text()
// TODO: Complete the implementation
// 1. Split names on "," to get the app name and the user name
// 2. Build the starting slice of tasks from taskData
// 3. Print the welcome message, the menu and the current task count
// 4. Run every action in actionData in order, printing its header first
}
All lessons in Logic & Flow
1Advanced Control Flow
Switch with `fallthrough`Breaking from Nested LoopsContinuing a Specific LoopThe `goto` StatementRecap - Advanced Loop Control4Project: Simple Task List
Project SetupAdding a Task2Structs and Methods
Defining Methods on StructsValue ReceiversPointer ReceiversChoosing ReceiversMethods vs FunctionsRecap - Struct Behavior5Maps In-Depth
Maps of StructsPointers as Map ValuesTesting for Nil MapsComparing MapsRecap - Word Frequency Counter3Interfaces (The Basics)
What is an Interface?Defining an InterfaceImplementing an InterfaceUsing Interface TypesEmpty InterfaceType AssertionsType SwitchRecap - Shapes and BehaviorsPractice on your own: Online Go compiler