Files
noahspan-go/structs-practice/todo/todo.go
2024-10-23 12:10:32 +00:00

38 lines
509 B
Go

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
}