Viewing All Tasks
Part of the Logic & Flow section of Coddy's GO journey — lesson 22 of 68.
Challenge
EasyContinuing with your task management system, you'll now implement the functionality to display all tasks in a user-friendly format. This challenge focuses on iterating through your task collection and presenting each task with its completion status using clear visual indicators.
You will receive two inputs:
- A string representing the number of tasks (e.g.,
"4") - A string containing task data in the format:
"name1:status1,name2:status2,name3:status3"where status is either"true"or"false"(e.g.,"Buy groceries:false,Call dentist:true,Finish project:false,Review code:true")
Your task is to:
- Define the same
Taskstruct from previous challenges withName(string) andCompleted(bool) fields - Create a function called
viewAllTasksthat takes a slice ofTaskstructs as a parameter - Inside the
viewAllTasksfunction, iterate through the slice and print each task with the following format:- For completed tasks:
"[x] [task_name]" - For incomplete tasks:
"[ ] [task_name]"
- For completed tasks:
- Parse the task data from the second input to create your slice of tasks:
- Split the input by commas to get individual task entries
- For each entry, split by colon to separate the name and status
- Convert the status string to a boolean (
"true"becomestrue,"false"becomesfalse)
- Call your
viewAllTasksfunction with the created slice - After displaying all tasks, print a summary in the format:
"Total: [total_count] tasks ([completed_count] completed, [incomplete_count] remaining)"
For example, if you have tasks "Buy groceries:false,Call dentist:true,Finish project:false", your output should show each task with appropriate checkboxes and then display the summary with the total count and completion statistics. This challenge practices slice iteration, conditional formatting with if statements, and provides users with a clear view of their task progress.
Try it yourself
package main
import (
"bufio"
"fmt"
"os"
"strconv"
"strings"
)
type Task struct {
Name string
Completed bool
}
func viewAllTasks(tasks []Task) {
// TODO: Implement the function to display all tasks
// For completed tasks: "[x] [task_name]"
// For incomplete tasks: "[ ] [task_name]"
}
func main() {
var numTasksStr string
fmt.Scanln(&numTasksStr)
numTasks, _ := strconv.Atoi(numTasksStr)
scanner := bufio.NewScanner(os.Stdin)
scanner.Scan()
taskData := scanner.Text()
var tasks []Task
// TODO: Parse the task data from taskData string
// Split by commas to get individual task entries
taskEntries := strings.Split(taskData, ",")
_ = taskEntries
// For each entry, split by colon to separate name and status
// Convert status string to boolean and create Task structs
// TODO: Call viewAllTasks function with the created slice
// TODO: Calculate and print summary
// Format: "Total: [total_count] tasks ([completed_count] completed, [incomplete_count] remaining)"
_ = tasks
_ = numTasks
}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 Behaviors