Files
noahspan-go/structs-practice/note/note.go
2024-10-21 09:34:38 +00:00

45 lines
800 B
Go

package note
import (
"encoding/json"
"errors"
"fmt"
"os"
"strings"
"time"
)
type Note struct {
Title string `json:"title"`
Content string `json:"content"`
CreatedAt time.Time `json:"created_at"`
}
func (note Note) Display() {
fmt.Printf("Your note title %v has the following content:\n\n%v", note.Title, note.Content)
}
func (note Note) Save() error {
fileName := strings.ReplaceAll(note.Title, " ", "_")
fileName = strings.ToLower(fileName) + ".json"
json, err := json.Marshal(note)
if err != nil {
return err
}
return os.WriteFile(fileName, json, 0644)
}
func New(title, content string) (Note, error) {
if title == "" || content == "" {
return Note{}, errors.New("Invalid input")
}
return Note{
Title: title,
Content: content,
CreatedAt: time.Now(),
}, nil
}