mirror of
https://github.com/unknwon/the-way-to-go_ZH_CN.git
synced 2025-08-12 03:55:28 +08:00
Add exercise 12.4: wiki_part1.go
This commit is contained in:
40
eBook/exercises/chapter_12/wiki_part1.go
Normal file
40
eBook/exercises/chapter_12/wiki_part1.go
Normal file
@@ -0,0 +1,40 @@
|
|||||||
|
// wiki_part1.go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"io/ioutil"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Page struct {
|
||||||
|
Title string
|
||||||
|
Body []byte
|
||||||
|
}
|
||||||
|
|
||||||
|
func (this *Page) save() (err error) {
|
||||||
|
return ioutil.WriteFile(this.Title, this.Body, 0666)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (this *Page) load(title string) (err error) {
|
||||||
|
this.Title = title
|
||||||
|
this.Body, err = ioutil.ReadFile(this.Title)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
page := Page{
|
||||||
|
"Page.md",
|
||||||
|
[]byte("# Page\n## Section1\nThis is section1."),
|
||||||
|
}
|
||||||
|
page.save()
|
||||||
|
|
||||||
|
// load from Page.md
|
||||||
|
var new_page Page
|
||||||
|
new_page.load("Page.md")
|
||||||
|
fmt.Println(string(new_page.Body))
|
||||||
|
}
|
||||||
|
/* Output:
|
||||||
|
* # Page
|
||||||
|
* ## Section1
|
||||||
|
* This is section1.
|
||||||
|
*/
|
Reference in New Issue
Block a user