split the todo into a sep file
This commit is contained in:
		
							parent
							
								
									5e5e0ee732
								
							
						
					
					
						commit
						d08834e39e
					
				
							
								
								
									
										43
									
								
								todo.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										43
									
								
								todo.go
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,43 @@
 | 
			
		||||
package main
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"errors"
 | 
			
		||||
	"fmt"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
type Todo struct {
 | 
			
		||||
	Title   string
 | 
			
		||||
	Content string
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
type Todos struct {
 | 
			
		||||
	Items []Todo
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (todo *Todos) AddTodo(newTodo Todo) {
 | 
			
		||||
	todo.Items = append(todo.Items, newTodo)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (todo *Todos) RemoveTodo(index int) (err error) {
 | 
			
		||||
	//remove todo via index
 | 
			
		||||
	if len(todo.Items) == 0 {
 | 
			
		||||
		return errors.New("[ERR] Todo list is empty!\n")
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	if index > len(todo.Items) {
 | 
			
		||||
		return errors.New("[ERR] Index out of bounds")
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	todo.Items = append(todo.Items[:index], todo.Items[index+1:]...)
 | 
			
		||||
	return nil
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (todo *Todos) ListTodo() int {
 | 
			
		||||
	if len(todo.Items) == 0 {
 | 
			
		||||
		return 0
 | 
			
		||||
	}
 | 
			
		||||
	for k, v := range todo.Items {
 | 
			
		||||
		fmt.Printf("%d:%s\n", k, v)
 | 
			
		||||
	}
 | 
			
		||||
	return 1
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user