/wiki.go

https://code.google.com/p/goblogger/ · Go · 34 lines · 28 code · 6 blank · 0 comment · 2 complexity · 35637cde658dc62f47f0b202e5e68749 MD5 · raw file

  1. package main
  2. import (
  3. "fmt"
  4. "io/ioutil"
  5. "os"
  6. )
  7. type Page struct {
  8. Title string
  9. Body []byte
  10. }
  11. func ( p *Page ) save() os.Error {
  12. fileName := p.Title + ".txt"
  13. return ioutil.WriteFile( fileName, p.Body, 0600 )
  14. }
  15. func loadPage( title string ) ( *Page, os.Error ) {
  16. fileName := title + ".txt"
  17. body, err := ioutil.ReadFile( fileName )
  18. if( nil != err ) {
  19. return nil, err
  20. }
  21. return &Page{ Title: title, Body: body }, nil
  22. }
  23. func main() {
  24. pageLocal := &Page{ Title: "TestPage", Body: []byte( "This a sample") }
  25. pageLocal.save()
  26. pageLoaded, _ := loadPage( "TestPage" )
  27. fmt.Println( string( pageLoaded.Body ) )
  28. }