Adding a Task
Part of the Logic & Flow section of Coddy's GO journey — lesson 21 of 68.
Challenge
EasyBuilding on your task management foundation, you'll now implement the core functionality to add new tasks to your system. This challenge focuses on creating a function that expands your task list by appending new tasks to the existing slice.
Important Note: Since task names may contain spaces (like "Buy groceries"), you'll need to use bufio.Scanner to read entire lines of input. The fmt.Scanln function stops reading at the first space, which would break task names with spaces. Use bufio.NewScanner(os.Stdin) and call scanner.Scan() followed by scanner.Text() to read complete lines.
You will receive two inputs:
- A string representing the number of existing tasks (e.g.,
"2") - A string containing task names separated by commas (e.g.,
"Buy groceries,Call dentist,Finish project")
Your task is to:
- Define the same
Taskstruct from the previous challenge withName(string) andCompleted(bool) fields - Create a function called
addTaskthat takes a slice ofTaskstructs and a task name (string) as parameters - The
addTaskfunction should return a new slice with the added task (the new task should haveCompletedset tofalse) - Use
bufio.Scannerto read the input lines properly to handle task names with spaces - Create an initial slice of tasks based on the number of existing tasks specified in the first input:
- If the number is
"0", start with an empty slice - If the number is greater than
"0", create that many placeholder tasks with names like"Existing Task 1","Existing Task 2", etc., all marked as incomplete
- If the number is
- Parse the comma-separated task names from the second input
- Use your
addTaskfunction to add each new task to the slice - Print each task in the final slice in the format:
"Task: [name], Completed: [true/false]" - Print the total number of tasks in the format:
"Total tasks: [count]"
For example, if you have 1 existing task and want to add "Buy groceries,Call dentist", your output should show the existing task first, followed by the two new tasks, and finally the total count. This challenge practices slice manipulation with the append function and demonstrates how to build functionality that extends your data structures.
Try it yourself
package main
import (
"bufio"
"fmt"
"os"
"strconv"
"strings"
)
type Task struct {
Name string
Completed bool
}
// TODO: Create addTask function here
func main() {
// Create scanner for reading input lines
scanner := bufio.NewScanner(os.Stdin)
// Read number of existing tasks
scanner.Scan()
existingTasksStr := scanner.Text()
// Read task names
scanner.Scan()
taskNamesStr := scanner.Text()
// TODO: Complete the implementation
// 1. Convert existingTasksStr to integer
// 2. Create initial slice with existing tasks
// 3. Parse taskNamesStr and add new tasks
// 4. Print all tasks and total count
}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