github.com/riscv/riscv-go@v0.0.0-20200123204226-124ebd6fcc8e/doc/articles/wiki/part1-noerror.go (about)

     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 {
    23  	filename := title + ".txt"
    24  	body, _ := ioutil.ReadFile(filename)
    25  	return &Page{Title: title, Body: body}
    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  }