Приложение гостевой книги, выполненное в целях практики
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

56 lines
922 B

package main
import (
"bufio"
"os"
)
type TxtStore string
func (t *TxtStore) GetGuestbook() (Guestbook, error) {
var result Guestbook
file, err := os.Open(string(*t))
if os.IsNotExist(err) {
return result, nil
} else if err != nil {
return result, err
}
defer file.Close()
scanner := bufio.NewScanner(file)
var comments []string
for scanner.Scan() {
comments = append([]string{scanner.Text()}, comments...)
}
err = scanner.Err()
if err != nil {
return result, err
}
result.Comments = comments
result.Count = len(comments)
return result, nil
}
func (t *TxtStore) AddComment(comment string) error {
if comment == "" {
return nil
}
options := os.O_WRONLY | os.O_APPEND | os.O_CREATE
file, err := os.OpenFile(string(*t), options, os.FileMode(0600))
if err != nil {
return err
}
_, err = file.WriteString(comment + "\n")
if err != nil {
return err
}
return file.Close()
}