mirror of
https://github.com/unknwon/the-way-to-go_ZH_CN.git
synced 2025-08-12 05:33:04 +08:00
@@ -287,7 +287,7 @@ type ReaderWriter struct {
|
||||
|
||||
稍微改变练习 11.9,允许 `mapFunc` 接收不定数量的 items。
|
||||
|
||||
**练习 11.13**:[main_stack.go—stack/stack_general.go](exercises/chapter_11/main_stack.go—stack/stack_general.go):
|
||||
**练习 11.13**:[main_stack.go](exercises/chapter_11/main_stack.go)—[stack/stack_general.go](exercises/chapter_11/stack/stack_general.go):
|
||||
|
||||
在练习 10.10 和 10.11 中我们开发了一些栈结构类型。但是它们被限制为某种固定的内建类型。现在用一个元素类型是 interface{}(空接口)的切片开发一个通用的栈类型。
|
||||
|
||||
|
@@ -71,7 +71,7 @@ func main() {
|
||||
// panic(err.Error())
|
||||
}
|
||||
fmt.Printf("%s\n", string(buf))
|
||||
err = ioutil.WriteFile(outputFile, buf, 0x644)
|
||||
err = ioutil.WriteFile(outputFile, buf, 0644) // oct, not hex
|
||||
if err != nil {
|
||||
panic(err. Error())
|
||||
}
|
||||
|
@@ -14,7 +14,7 @@ func main() {
|
||||
panic(err.Error())
|
||||
}
|
||||
fmt.Printf("%s\n", string(buf))
|
||||
err = ioutil.WriteFile(outputFile, buf, 0x644)
|
||||
err = ioutil.WriteFile(outputFile, buf, 0644) // oct, not hex
|
||||
if err != nil {
|
||||
panic(err.Error())
|
||||
}
|
||||
|
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