go写任务管理
时间: 2023-07-04 19:13:24 浏览: 49
实现一个简单的任务管理器,可以添加、删除、修改和列出任务。
以下是一个简单的示例代码:
```go
package main
import (
"fmt"
)
// Task represents a task in the task manager
type Task struct {
ID int
Title string
Completed bool
}
// TaskManager represents a task manager that can add, delete, modify, and list tasks
type TaskManager struct {
tasks []*Task
}
// AddTask adds a new task to the task manager
func (tm *TaskManager) AddTask(title string) {
id := len(tm.tasks) + 1
task := &Task{
ID: id,
Title: title,
Completed: false,
}
tm.tasks = append(tm.tasks, task)
fmt.Printf("Task %d added: %s\n", id, title)
}
// DeleteTask deletes a task from the task manager by ID
func (tm *TaskManager) DeleteTask(id int) {
index := -1
for i, task := range tm.tasks {
if task.ID == id {
index = i
break
}
}
if index == -1 {
fmt.Printf("Task %d not found\n", id)
return
}
tm.tasks = append(tm.tasks[:index], tm.tasks[index+1:]...)
fmt.Printf("Task %d deleted\n", id)
}
// ModifyTask modifies a task in the task manager by ID
func (tm *TaskManager) ModifyTask(id int, title string, completed bool) {
for _, task := range tm.tasks {
if task.ID == id {
task.Title = title
task.Completed = completed
fmt.Printf("Task %d modified: %s\n", id, title)
return
}
}
fmt.Printf("Task %d not found\n", id)
}
// ListTasks lists all the tasks in the task manager
func (tm *TaskManager) ListTasks() {
for _, task := range tm.tasks {
status := "not completed"
if task.Completed {
status = "completed"
}
fmt.Printf("Task %d: %s (%s)\n", task.ID, task.Title, status)
}
}
func main() {
tm := &TaskManager{}
// Add some tasks
tm.AddTask("Buy groceries")
tm.AddTask("Do laundry")
// Modify a task
tm.ModifyTask(2, "Do laundry (wash whites)", false)
// List all tasks
tm.ListTasks()
// Delete a task
tm.DeleteTask(1)
// List all tasks again
tm.ListTasks()
}
```
这个任务管理器包含一个 `Task` 结构体,用于表示任务的属性,以及一个 `TaskManager` 结构体,用于管理任务。`TaskManager` 包含了 `AddTask`、`DeleteTask`、`ModifyTask` 和 `ListTasks` 方法,分别用于添加、删除、修改和列出任务。在 `main` 函数中,我们通过 `TaskManager` 来管理任务,并测试了这些方法的功能。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)