2010-04-27 20:36:39 -06:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
)
|
|
|
|
|
2011-01-25 21:56:52 -07:00
|
|
|
type Page struct {
|
|
|
|
Title string
|
|
|
|
Body []byte
|
2010-04-27 20:36:39 -06:00
|
|
|
}
|
|
|
|
|
2011-01-25 21:56:52 -07:00
|
|
|
func (p *Page) save() os.Error {
|
|
|
|
filename := p.Title + ".txt"
|
|
|
|
return ioutil.WriteFile(filename, p.Body, 0600)
|
2010-04-27 20:36:39 -06:00
|
|
|
}
|
|
|
|
|
2011-01-25 21:56:52 -07:00
|
|
|
func loadPage(title string) (*Page, os.Error) {
|
2010-04-27 20:36:39 -06:00
|
|
|
filename := title + ".txt"
|
|
|
|
body, err := ioutil.ReadFile(filename)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2011-01-25 21:56:52 -07:00
|
|
|
return &Page{Title: title, Body: body}, nil
|
2010-04-27 20:36:39 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
2011-01-25 21:56:52 -07:00
|
|
|
p1 := &Page{Title: "TestPage", Body: []byte("This is a sample Page.")}
|
2010-04-27 20:36:39 -06:00
|
|
|
p1.save()
|
|
|
|
p2, _ := loadPage("TestPage")
|
2011-01-25 21:56:52 -07:00
|
|
|
fmt.Println(string(p2.Body))
|
2010-04-27 20:36:39 -06:00
|
|
|
}
|