Adding test file read and write
This commit is contained in:
parent
56d3e153cb
commit
2bf278c312
2 changed files with 32 additions and 4 deletions
32
galley.go
Normal file
32
galley.go
Normal file
|
|
@ -0,0 +1,32 @@
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"os"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
p1 := &Page{Title: "TestPage", Body: []byte("This is a sample page.")}
|
||||||
|
p1.save()
|
||||||
|
p2, _ := loadPage("TestPage")
|
||||||
|
fmt.Println(string(p2.Body))
|
||||||
|
}
|
||||||
|
|
||||||
|
type Page struct {
|
||||||
|
Title string
|
||||||
|
Body []byte
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *Page) save() error {
|
||||||
|
filename := p.Title + ".txt"
|
||||||
|
return os.WriteFile(filename, p.Body, 0600)
|
||||||
|
}
|
||||||
|
|
||||||
|
func loadPage(title string) (*Page, error) {
|
||||||
|
filename := title + ".txt"
|
||||||
|
body, err := os.ReadFile(filename)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return &Page{Title: title, Body: body}, nil
|
||||||
|
}
|
||||||
4
main.go
4
main.go
|
|
@ -1,4 +0,0 @@
|
||||||
package main
|
|
||||||
|
|
||||||
func main() {
|
|
||||||
}
|
|
||||||
Loading…
Reference in a new issue