Sfoglia il codice sorgente

Add exercise 12.4: wiki_part1.go

ArkBriar 10 anni fa
parent
commit
01e9fae48d
1 ha cambiato i file con 40 aggiunte e 0 eliminazioni
  1. 40 0
      eBook/exercises/chapter_12/wiki_part1.go

+ 40 - 0
eBook/exercises/chapter_12/wiki_part1.go

@@ -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.
+ */