lists
This commit is contained in:
@@ -2,31 +2,72 @@ package main
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"fmt"
|
||||
"example.com/note/note"
|
||||
"example.com/note/todo"
|
||||
"fmt"
|
||||
"os"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type saver interface {
|
||||
Save() error
|
||||
}
|
||||
|
||||
// type displayer interface {
|
||||
// Display()
|
||||
// }
|
||||
|
||||
type outputtable interface {
|
||||
saver
|
||||
Display()
|
||||
}
|
||||
|
||||
// type outputtable interface {
|
||||
// Save() error
|
||||
// Display()
|
||||
// }
|
||||
|
||||
func main() {
|
||||
title, content := getNoteData()
|
||||
todoText := getUserInput("Todo text: ")
|
||||
|
||||
userNote, err := note.New(title, content)
|
||||
todo, err := todo.New(todoText)
|
||||
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return
|
||||
}
|
||||
|
||||
userNote.Display()
|
||||
err = userNote.Save()
|
||||
userNote, err := note.New(title, content)
|
||||
|
||||
if err != nil {
|
||||
fmt.Println("Saving the note failed")
|
||||
return
|
||||
}
|
||||
|
||||
err = outputData(todo)
|
||||
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
outputData(userNote)
|
||||
}
|
||||
|
||||
func outputData(data outputtable) error {
|
||||
data.Display()
|
||||
return saveData(data)
|
||||
}
|
||||
|
||||
func saveData(data saver) error {
|
||||
err := data.Save()
|
||||
|
||||
if err != nil {
|
||||
fmt.Println("Saving the note failed")
|
||||
return err
|
||||
}
|
||||
|
||||
fmt.Println("Saving the note succeeded!")
|
||||
return nil
|
||||
}
|
||||
|
||||
func getNoteData() (string, string) {
|
||||
|
||||
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