I'm in the process of learning Golang, and I'm currently trying to create a Todo project.
My knowledge is limited to the GO base.
Please take a look at my code and tell me how I can delete a task.
The user writes in the console:
add Eat
add Sleep
add Study
Then they write:
list
to check the tasks
For example, if they want to delete the second task (Sleep), and I write del 1, it will delete the first element of the slice, which is 0.
And every time I write del 2, it will still delete the first element.
And now I need to make it so that I can delete:
a) By the title
b) By the number of the task location
I'm stuck at the "Creating a task deletion command" stage. package main
import (
"bufio"
"fmt"
"os"
"strings"
"time"
)
type Todo struct {
Heading string
Text string
Creationtime time.Time
Completed bool
LeadTime time.Time
}
func main() {
todos := []Todo{}
_ = todos
scanner := bufio.NewScanner(os.Stdin)
for {
scanner.Scan()
input := scanner.Text()
words := strings.Split(input, " ")
switch words[0] {
case "add":
todo := Todo{
Heading: words[1],
Text: strings.Join(words[2:], " "),
Creationtime: time.Now(),
Completed: false,
}
todos = append(todos, todo)
case "list":
for _, todo := range todos {
fmt.Println(todo.Heading, todo.Text)
}
case "del":
var n int
todos = append(todos[:n], todos[n+1:]...)