The Go Programming Language

Source file doc/codelab/wiki/part1.go

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

release.r60.3. Except as noted, this content is licensed under a Creative Commons Attribution 3.0 License.