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.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{}(空接口)的切片开发一个通用的栈类型。
|
在练习 10.10 和 10.11 中我们开发了一些栈结构类型。但是它们被限制为某种固定的内建类型。现在用一个元素类型是 interface{}(空接口)的切片开发一个通用的栈类型。
|
||||||
|
|
||||||
@@ -310,4 +310,4 @@ Pop() (x interface{}, error)
|
|||||||
|
|
||||||
- [目录](directory.md)
|
- [目录](directory.md)
|
||||||
- 上一节:[Printf 和反射](11.11.md)
|
- 上一节:[Printf 和反射](11.11.md)
|
||||||
- 下一节:[总结:Go 中的面向对象](11.13.md)
|
- 下一节:[总结:Go 中的面向对象](11.13.md)
|
||||||
|
@@ -74,7 +74,7 @@ allNewBMWs := allCars.FindAll(func(car *Car) bool {
|
|||||||
4)我们也可以根据入参返回不同的函数。也许我们想根据不同的厂商添加汽车到不同的集合,但是这可能会是多变的。所以我们可以定义一个函数来产生特定的添加函数和 map 集:
|
4)我们也可以根据入参返回不同的函数。也许我们想根据不同的厂商添加汽车到不同的集合,但是这可能会是多变的。所以我们可以定义一个函数来产生特定的添加函数和 map 集:
|
||||||
|
|
||||||
```go
|
```go
|
||||||
funcMakeSortedAppender(manufacturers[]string)(func(car*Car),map[string]Cars) {
|
func MakeSortedAppender(manufacturers[]string)(func(car*Car),map[string]Cars) {
|
||||||
// Prepare maps of sorted cars.
|
// Prepare maps of sorted cars.
|
||||||
sortedCars := make(map[string]Cars)
|
sortedCars := make(map[string]Cars)
|
||||||
for _, m := range manufacturers {
|
for _, m := range manufacturers {
|
||||||
@@ -213,4 +213,4 @@ We have 2 BMWs
|
|||||||
|
|
||||||
- [目录](directory.md)
|
- [目录](directory.md)
|
||||||
- 上一节:[Go 中的面向对象](11.13.md)
|
- 上一节:[Go 中的面向对象](11.13.md)
|
||||||
- 下一章:[读写数据](12.0.md)
|
- 下一章:[读写数据](12.0.md)
|
||||||
|
@@ -71,7 +71,7 @@ func main() {
|
|||||||
// panic(err.Error())
|
// panic(err.Error())
|
||||||
}
|
}
|
||||||
fmt.Printf("%s\n", string(buf))
|
fmt.Printf("%s\n", string(buf))
|
||||||
err = ioutil.WriteFile(outputFile, buf, 0x644)
|
err = ioutil.WriteFile(outputFile, buf, 0644) // oct, not hex
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err. Error())
|
panic(err. Error())
|
||||||
}
|
}
|
||||||
|
@@ -14,7 +14,7 @@ func main() {
|
|||||||
panic(err.Error())
|
panic(err.Error())
|
||||||
}
|
}
|
||||||
fmt.Printf("%s\n", string(buf))
|
fmt.Printf("%s\n", string(buf))
|
||||||
err = ioutil.WriteFile(outputFile, buf, 0x644)
|
err = ioutil.WriteFile(outputFile, buf, 0644) // oct, not hex
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err.Error())
|
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