structs
This commit is contained in:
45
structs-practice/note/note.go
Normal file
45
structs-practice/note/note.go
Normal file
@@ -0,0 +1,45 @@
|
||||
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
|
||||
}
|
||||
Reference in New Issue
Block a user