structs
This commit is contained in:
55
structs-practice/main.go
Normal file
55
structs-practice/main.go
Normal file
@@ -0,0 +1,55 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"fmt"
|
||||
"example.com/note/note"
|
||||
"os"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func main() {
|
||||
title, content := getNoteData()
|
||||
|
||||
userNote, err := note.New(title, content)
|
||||
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return
|
||||
}
|
||||
|
||||
userNote.Display()
|
||||
err = userNote.Save()
|
||||
|
||||
if err != nil {
|
||||
fmt.Println("Saving the note failed")
|
||||
return
|
||||
}
|
||||
|
||||
fmt.Println("Saving the note succeeded!")
|
||||
}
|
||||
|
||||
func getNoteData() (string, string) {
|
||||
title := getUserInput("Note title:")
|
||||
|
||||
content := getUserInput("Note content:")
|
||||
|
||||
return title, content
|
||||
}
|
||||
|
||||
func getUserInput(prompt string) (string) {
|
||||
fmt.Printf("%v ", prompt)
|
||||
|
||||
reader := bufio.NewReader(os.Stdin)
|
||||
|
||||
text, err :=reader.ReadString('\n')
|
||||
|
||||
if err != nil {
|
||||
return ""
|
||||
}
|
||||
|
||||
text = strings.TrimSuffix(text, "\n")
|
||||
text = strings.TrimSuffix(text, "\r")
|
||||
|
||||
return text
|
||||
}
|
||||
Reference in New Issue
Block a user