lists
This commit is contained in:
38
structs-practice/todo/todo.go
Normal file
38
structs-practice/todo/todo.go
Normal file
@@ -0,0 +1,38 @@
|
||||
package todo
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"os"
|
||||
)
|
||||
|
||||
type Todo struct {
|
||||
Text string `json:"text"`
|
||||
}
|
||||
|
||||
func (todo Todo) Display() {
|
||||
fmt.Printf(todo.Text)
|
||||
}
|
||||
|
||||
func (todo Todo) Save() error {
|
||||
fileName := "todo.json"
|
||||
|
||||
json, err := json.Marshal(todo)
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return os.WriteFile(fileName, json, 0644)
|
||||
}
|
||||
|
||||
func New(content string) (Todo, error) {
|
||||
if content == "" || content == "" {
|
||||
return Todo{}, errors.New("Invalid input")
|
||||
}
|
||||
|
||||
return Todo{
|
||||
Text: content,
|
||||
}, nil
|
||||
}
|
||||
Reference in New Issue
Block a user