...
Run Format

Source file doc/articles/wiki/part1.go

Documentation: doc/articles/wiki

     1  // Copyright 2010 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package main
     6  
     7  import (
     8  	"fmt"
     9  	"io/ioutil"
    10  )
    11  
    12  type Page struct {
    13  	Title string
    14  	Body  []byte
    15  }
    16  
    17  func (p *Page) save() error {
    18  	filename := p.Title + ".txt"
    19  	return ioutil.WriteFile(filename, p.Body, 0600)
    20  }
    21  
    22  func loadPage(title string) (*Page, error) {
    23  	filename := title + ".txt"
    24  	body, err := ioutil.ReadFile(filename)
    25  	if err != nil {
    26  		return nil, err
    27  	}
    28  	return &Page{Title: title, Body: body}, nil
    29  }
    30  
    31  func main() {
    32  	p1 := &Page{Title: "TestPage", Body: []byte("This is a sample Page.")}
    33  	p1.save()
    34  	p2, _ := loadPage("TestPage")
    35  	fmt.Println(string(p2.Body))
    36  }
    37  

View as plain text