From bc0f795d4c5ecb3fb27a4d6feb9e534fcc6abc44 Mon Sep 17 00:00:00 2001 From: songleo Date: Tue, 29 Dec 2015 15:54:15 +0800 Subject: [PATCH 01/20] start OpenSource --- eBook/18.0.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 eBook/18.0.md diff --git a/eBook/18.0.md b/eBook/18.0.md new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/eBook/18.0.md @@ -0,0 +1 @@ + From 8740fc7bc3e93e0b1f748d5f8389a0229811729e Mon Sep 17 00:00:00 2001 From: reborncodinglife Date: Sat, 2 Jan 2016 18:47:13 +0800 Subject: [PATCH 02/20] Update 18.0.md --- eBook/18.0.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/eBook/18.0.md b/eBook/18.0.md index 8b13789..9daeafb 100644 --- a/eBook/18.0.md +++ b/eBook/18.0.md @@ -1 +1 @@ - +test From e44f5d725724fdf5fceade3ada3edec15def2c13 Mon Sep 17 00:00:00 2001 From: songleo Date: Sat, 2 Jan 2016 20:22:27 +0800 Subject: [PATCH 03/20] add 18.0 and 18.1 --- eBook/18.0.md | 8 ++++++- eBook/18.1.md | 58 +++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 65 insertions(+), 1 deletion(-) create mode 100644 eBook/18.1.md diff --git a/eBook/18.0.md b/eBook/18.0.md index 9daeafb..63e6191 100644 --- a/eBook/18.0.md +++ b/eBook/18.0.md @@ -1 +1,7 @@ -test +# 18 出于性能考虑的实用代码片段 + +## 链接 + +- [目录](directory.md) +- 上一节:[运算符模板和接口](17.4.md) +- 下一节:[字符串](18.1.md) \ No newline at end of file diff --git a/eBook/18.1.md b/eBook/18.1.md new file mode 100644 index 0000000..cedbadd --- /dev/null +++ b/eBook/18.1.md @@ -0,0 +1,58 @@ +# 18.1 字符串 + +(1)如何修改字符串中的一个字符: + +```go +str:="hello" +c:=[]byte(s) +c[0]='c' +s2:= string(c) // s2 == "cello" +``` + +(2)如何获取字符串的子串: + +```go +substr := str[n:m] +``` + +(3)如何使用for或者for-range遍历一个字符串: + +```go +// gives only the bytes: +for i:=0; i < len(str); i++ { +… = str[i] +} +// gives the Unicode characters: +for ix, ch := range str { +… +} +``` + +(4)如何获取一个字符串的字节数:`len(str)` + 如何获取一个字符串的字符数: + 最快速:`utf8.RuneCountInString(str)` + + `len([]int(str)) //TBD` + +(5)如何连接字符串: + 最快速: `with a bytes.Buffer`(参考[章节7.2](7.2.md)) + + `Strings.Join()`(参考[章节4.7](4.7.md)) + + `+=` + + ```go + str1 := "Hello " + str2 := "World!" + str1 += str2 //str1 == "Hello World!" + ``` + +(6)如何解析命令行参数:使用os或者flag包 + + (参考[例12.4](examples/chapter_12/fileinput.go)) + +## 链接 + +- [目录](directory.md) +- 上一节:[出于性能考虑的实用代码片段](18.0.md) +- 下一节:[数组和切片](18.2.md) \ No newline at end of file From 3eb5cb5496c289bf12c05eb4e12aeda317aa87d8 Mon Sep 17 00:00:00 2001 From: songleo Date: Sat, 2 Jan 2016 20:33:39 +0800 Subject: [PATCH 04/20] modified: eBook/18.1.md --- eBook/18.1.md | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/eBook/18.1.md b/eBook/18.1.md index cedbadd..ec98db3 100644 --- a/eBook/18.1.md +++ b/eBook/18.1.md @@ -29,15 +29,18 @@ for ix, ch := range str { ``` (4)如何获取一个字符串的字节数:`len(str)` + 如何获取一个字符串的字符数: + 最快速:`utf8.RuneCountInString(str)` `len([]int(str)) //TBD` (5)如何连接字符串: - 最快速: `with a bytes.Buffer`(参考[章节7.2](7.2.md)) - `Strings.Join()`(参考[章节4.7](4.7.md)) + 最快速: `with a bytes.Buffer`(参考[章节7.2](07.2.md)) + + `Strings.Join()`(参考[章节4.7](04.7.md)) `+=` @@ -47,12 +50,12 @@ for ix, ch := range str { str1 += str2 //str1 == "Hello World!" ``` -(6)如何解析命令行参数:使用os或者flag包 +(6)如何解析命令行参数:使用`os`或者`flag`包 (参考[例12.4](examples/chapter_12/fileinput.go)) ## 链接 - [目录](directory.md) -- 上一节:[出于性能考虑的实用代码片段](18.0.md) +- 上一章:[出于性能考虑的实用代码片段](18.0.md) - 下一节:[数组和切片](18.2.md) \ No newline at end of file From 5db1a1f78447d04035460d3c6d796f8990d9c879 Mon Sep 17 00:00:00 2001 From: songleo Date: Sat, 2 Jan 2016 20:47:13 +0800 Subject: [PATCH 05/20] modified: 18.1.md --- eBook/18.0.md | 2 +- eBook/18.1.md | 2 +- eBook/18.10.md | 0 eBook/18.11.md | 0 eBook/18.2.md | 7 +++++++ eBook/18.3.md | 0 eBook/18.4.md | 0 eBook/18.5.md | 0 eBook/18.6.md | 0 eBook/18.7.md | 0 eBook/18.8.md | 0 eBook/18.9.md | 0 12 files changed, 9 insertions(+), 2 deletions(-) create mode 100644 eBook/18.10.md create mode 100644 eBook/18.11.md create mode 100644 eBook/18.2.md create mode 100644 eBook/18.3.md create mode 100644 eBook/18.4.md create mode 100644 eBook/18.5.md create mode 100644 eBook/18.6.md create mode 100644 eBook/18.7.md create mode 100644 eBook/18.8.md create mode 100644 eBook/18.9.md diff --git a/eBook/18.0.md b/eBook/18.0.md index 63e6191..f7739e2 100644 --- a/eBook/18.0.md +++ b/eBook/18.0.md @@ -3,5 +3,5 @@ ## 链接 - [目录](directory.md) -- 上一节:[运算符模板和接口](17.4.md) +- 上一章:[运算符模板和接口](17.4.md) - 下一节:[字符串](18.1.md) \ No newline at end of file diff --git a/eBook/18.1.md b/eBook/18.1.md index ec98db3..2d075ba 100644 --- a/eBook/18.1.md +++ b/eBook/18.1.md @@ -57,5 +57,5 @@ for ix, ch := range str { ## 链接 - [目录](directory.md) -- 上一章:[出于性能考虑的实用代码片段](18.0.md) +- 上一节:[出于性能考虑的实用代码片段](18.0.md) - 下一节:[数组和切片](18.2.md) \ No newline at end of file diff --git a/eBook/18.10.md b/eBook/18.10.md new file mode 100644 index 0000000..e69de29 diff --git a/eBook/18.11.md b/eBook/18.11.md new file mode 100644 index 0000000..e69de29 diff --git a/eBook/18.2.md b/eBook/18.2.md new file mode 100644 index 0000000..f7739e2 --- /dev/null +++ b/eBook/18.2.md @@ -0,0 +1,7 @@ +# 18 出于性能考虑的实用代码片段 + +## 链接 + +- [目录](directory.md) +- 上一章:[运算符模板和接口](17.4.md) +- 下一节:[字符串](18.1.md) \ No newline at end of file diff --git a/eBook/18.3.md b/eBook/18.3.md new file mode 100644 index 0000000..e69de29 diff --git a/eBook/18.4.md b/eBook/18.4.md new file mode 100644 index 0000000..e69de29 diff --git a/eBook/18.5.md b/eBook/18.5.md new file mode 100644 index 0000000..e69de29 diff --git a/eBook/18.6.md b/eBook/18.6.md new file mode 100644 index 0000000..e69de29 diff --git a/eBook/18.7.md b/eBook/18.7.md new file mode 100644 index 0000000..e69de29 diff --git a/eBook/18.8.md b/eBook/18.8.md new file mode 100644 index 0000000..e69de29 diff --git a/eBook/18.9.md b/eBook/18.9.md new file mode 100644 index 0000000..e69de29 From f28c409d54c9efc561e1538aa63e9202c657f734 Mon Sep 17 00:00:00 2001 From: songleo Date: Sat, 2 Jan 2016 20:55:48 +0800 Subject: [PATCH 06/20] deleted: 18.6.md --- eBook/18.1.md | 120 ++++++++++++++++++++++++------------------------- eBook/18.10.md | 0 eBook/18.11.md | 0 eBook/18.2.md | 7 --- eBook/18.3.md | 0 eBook/18.4.md | 0 eBook/18.5.md | 0 eBook/18.6.md | 0 eBook/18.7.md | 0 eBook/18.8.md | 0 eBook/18.9.md | 0 11 files changed, 60 insertions(+), 67 deletions(-) delete mode 100644 eBook/18.10.md delete mode 100644 eBook/18.11.md delete mode 100644 eBook/18.2.md delete mode 100644 eBook/18.3.md delete mode 100644 eBook/18.4.md delete mode 100644 eBook/18.5.md delete mode 100644 eBook/18.6.md delete mode 100644 eBook/18.7.md delete mode 100644 eBook/18.8.md delete mode 100644 eBook/18.9.md diff --git a/eBook/18.1.md b/eBook/18.1.md index 2d075ba..7b6bc96 100644 --- a/eBook/18.1.md +++ b/eBook/18.1.md @@ -1,61 +1,61 @@ -# 18.1 字符串 - -(1)如何修改字符串中的一个字符: - -```go -str:="hello" -c:=[]byte(s) -c[0]='c' -s2:= string(c) // s2 == "cello" -``` - -(2)如何获取字符串的子串: - -```go -substr := str[n:m] -``` - -(3)如何使用for或者for-range遍历一个字符串: - -```go -// gives only the bytes: -for i:=0; i < len(str); i++ { -… = str[i] -} -// gives the Unicode characters: -for ix, ch := range str { -… -} -``` - -(4)如何获取一个字符串的字节数:`len(str)` - - 如何获取一个字符串的字符数: - - 最快速:`utf8.RuneCountInString(str)` - - `len([]int(str)) //TBD` - -(5)如何连接字符串: - - 最快速: `with a bytes.Buffer`(参考[章节7.2](07.2.md)) - - `Strings.Join()`(参考[章节4.7](04.7.md)) - - `+=` - - ```go - str1 := "Hello " - str2 := "World!" - str1 += str2 //str1 == "Hello World!" - ``` - -(6)如何解析命令行参数:使用`os`或者`flag`包 - - (参考[例12.4](examples/chapter_12/fileinput.go)) - -## 链接 - -- [目录](directory.md) -- 上一节:[出于性能考虑的实用代码片段](18.0.md) +# 18.1 字符串 + +(1)如何修改字符串中的一个字符: + +```go +str:="hello" +c:=[]byte(s) +c[0]='c' +s2:= string(c) // s2 == "cello" +``` + +(2)如何获取字符串的子串: + +```go +substr := str[n:m] +``` + +(3)如何使用for或者for-range遍历一个字符串: + +```go +// gives only the bytes: +for i:=0; i < len(str); i++ { +… = str[i] +} +// gives the Unicode characters: +for ix, ch := range str { +… +} +``` + +(4)如何获取一个字符串的字节数:`len(str)` + + 如何获取一个字符串的字符数: + + 最快速:`utf8.RuneCountInString(str)` + + `len([]int(str)) //TBD` + +(5)如何连接字符串: + + 最快速: `with a bytes.Buffer`(参考[章节7.2](07.2.md)) + + `Strings.Join()`(参考[章节4.7](04.7.md)) + + `+=` + + ```go + str1 := "Hello " + str2 := "World!" + str1 += str2 //str1 == "Hello World!" + ``` + +(6)如何解析命令行参数:使用`os`或者`flag`包 + + (参考[例12.4](examples/chapter_12/fileinput.go)) + +## 链接 + +- [目录](directory.md) +- 上一节:[出于性能考虑的实用代码片段](18.0.md) - 下一节:[数组和切片](18.2.md) \ No newline at end of file diff --git a/eBook/18.10.md b/eBook/18.10.md deleted file mode 100644 index e69de29..0000000 diff --git a/eBook/18.11.md b/eBook/18.11.md deleted file mode 100644 index e69de29..0000000 diff --git a/eBook/18.2.md b/eBook/18.2.md deleted file mode 100644 index f7739e2..0000000 --- a/eBook/18.2.md +++ /dev/null @@ -1,7 +0,0 @@ -# 18 出于性能考虑的实用代码片段 - -## 链接 - -- [目录](directory.md) -- 上一章:[运算符模板和接口](17.4.md) -- 下一节:[字符串](18.1.md) \ No newline at end of file diff --git a/eBook/18.3.md b/eBook/18.3.md deleted file mode 100644 index e69de29..0000000 diff --git a/eBook/18.4.md b/eBook/18.4.md deleted file mode 100644 index e69de29..0000000 diff --git a/eBook/18.5.md b/eBook/18.5.md deleted file mode 100644 index e69de29..0000000 diff --git a/eBook/18.6.md b/eBook/18.6.md deleted file mode 100644 index e69de29..0000000 diff --git a/eBook/18.7.md b/eBook/18.7.md deleted file mode 100644 index e69de29..0000000 diff --git a/eBook/18.8.md b/eBook/18.8.md deleted file mode 100644 index e69de29..0000000 diff --git a/eBook/18.9.md b/eBook/18.9.md deleted file mode 100644 index e69de29..0000000 From b108571cd8c4fd0e0898e3e5319d03391d03ed09 Mon Sep 17 00:00:00 2001 From: leo Date: Sat, 2 Jan 2016 20:54:26 +0800 Subject: [PATCH 07/20] Delete 18.1.md --- eBook/18.1.md | 61 --------------------------------------------------- 1 file changed, 61 deletions(-) delete mode 100644 eBook/18.1.md diff --git a/eBook/18.1.md b/eBook/18.1.md deleted file mode 100644 index 7b6bc96..0000000 --- a/eBook/18.1.md +++ /dev/null @@ -1,61 +0,0 @@ -# 18.1 字符串 - -(1)如何修改字符串中的一个字符: - -```go -str:="hello" -c:=[]byte(s) -c[0]='c' -s2:= string(c) // s2 == "cello" -``` - -(2)如何获取字符串的子串: - -```go -substr := str[n:m] -``` - -(3)如何使用for或者for-range遍历一个字符串: - -```go -// gives only the bytes: -for i:=0; i < len(str); i++ { -… = str[i] -} -// gives the Unicode characters: -for ix, ch := range str { -… -} -``` - -(4)如何获取一个字符串的字节数:`len(str)` - - 如何获取一个字符串的字符数: - - 最快速:`utf8.RuneCountInString(str)` - - `len([]int(str)) //TBD` - -(5)如何连接字符串: - - 最快速: `with a bytes.Buffer`(参考[章节7.2](07.2.md)) - - `Strings.Join()`(参考[章节4.7](04.7.md)) - - `+=` - - ```go - str1 := "Hello " - str2 := "World!" - str1 += str2 //str1 == "Hello World!" - ``` - -(6)如何解析命令行参数:使用`os`或者`flag`包 - - (参考[例12.4](examples/chapter_12/fileinput.go)) - -## 链接 - -- [目录](directory.md) -- 上一节:[出于性能考虑的实用代码片段](18.0.md) -- 下一节:[数组和切片](18.2.md) \ No newline at end of file From dbb6cf07dcac71cfee86e0b8d023a5387ec3f994 Mon Sep 17 00:00:00 2001 From: leo Date: Sat, 2 Jan 2016 20:54:59 +0800 Subject: [PATCH 08/20] Create 18.1.md --- eBook/18.1.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 eBook/18.1.md diff --git a/eBook/18.1.md b/eBook/18.1.md new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/eBook/18.1.md @@ -0,0 +1 @@ + From 05b0340da3706034f2d73f4fee66bb0c9e7c9967 Mon Sep 17 00:00:00 2001 From: songleo Date: Sat, 2 Jan 2016 21:00:00 +0800 Subject: [PATCH 09/20] test --- eBook/18.1.md | 60 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) diff --git a/eBook/18.1.md b/eBook/18.1.md index 8b13789..7b6bc96 100644 --- a/eBook/18.1.md +++ b/eBook/18.1.md @@ -1 +1,61 @@ +# 18.1 字符串 +(1)如何修改字符串中的一个字符: + +```go +str:="hello" +c:=[]byte(s) +c[0]='c' +s2:= string(c) // s2 == "cello" +``` + +(2)如何获取字符串的子串: + +```go +substr := str[n:m] +``` + +(3)如何使用for或者for-range遍历一个字符串: + +```go +// gives only the bytes: +for i:=0; i < len(str); i++ { +… = str[i] +} +// gives the Unicode characters: +for ix, ch := range str { +… +} +``` + +(4)如何获取一个字符串的字节数:`len(str)` + + 如何获取一个字符串的字符数: + + 最快速:`utf8.RuneCountInString(str)` + + `len([]int(str)) //TBD` + +(5)如何连接字符串: + + 最快速: `with a bytes.Buffer`(参考[章节7.2](07.2.md)) + + `Strings.Join()`(参考[章节4.7](04.7.md)) + + `+=` + + ```go + str1 := "Hello " + str2 := "World!" + str1 += str2 //str1 == "Hello World!" + ``` + +(6)如何解析命令行参数:使用`os`或者`flag`包 + + (参考[例12.4](examples/chapter_12/fileinput.go)) + +## 链接 + +- [目录](directory.md) +- 上一节:[出于性能考虑的实用代码片段](18.0.md) +- 下一节:[数组和切片](18.2.md) \ No newline at end of file From 7ea77a4738f5860e26176701d7d91cecafa0a032 Mon Sep 17 00:00:00 2001 From: songleo Date: Sun, 3 Jan 2016 13:10:15 +0800 Subject: [PATCH 10/20] new file: 18.5.md --- eBook/18.5.md | 67 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 67 insertions(+) create mode 100644 eBook/18.5.md diff --git a/eBook/18.5.md b/eBook/18.5.md new file mode 100644 index 0000000..7dfcda6 --- /dev/null +++ b/eBook/18.5.md @@ -0,0 +1,67 @@ +# 18.1 字符串 + +(1)如何修改字符串中的一个字符: + +```go +str:="hello" +c:=[]byte(s) +c[0]='c' +s2:= string(c) // s2 == "cello" +``` + +(2)如何获取字符串的子串: + +```go +substr := str[n:m] +``` + +(3)如何使用for或者for-range遍历一个字符串: + +```go +// gives only the bytes: +for i:=0; i < len(str); i++ { +… = str[i] +} +// gives the Unicode characters: +for ix, ch := range str { +… +} +``` + +(4)如何获取一个字符串的字节数:`len(str)` + + 如何获取一个字符串的字符数: + + 最快速:`utf8.RuneCountInString(str)` + + `len([]int(str)) //TBD` + +(5)如何连接字符串: + + 最快速: `with a bytes.Buffer`(参考[章节7.2](07.2.md)) + + `Strings.Join()`(参考[章节4.7](04.7.md)) + + `+=` + + ```go + str1 := "Hello " + str2 := "World!" + str1 += str2 //str1 == "Hello World!" + ``` + +(6)如何解析命令行参数:使用`os`或者`flag`包 + + (参考[例12.4](examples/chapter_12/fileinput.go)) + +## 链接 + +- [目录](directory.md) +- 上一章:[运算符模板和接口](17.4.md) +- 下一节:[字符串](18.1.md) + +## 链接 + +- [目录](directory.md) +- 上一章:[运算符模板和接口](17.4.md) +- 下一节:[字符串](18.1.md) \ No newline at end of file From 7c06912c3042d6591c794e2ec80073f2455a77ec Mon Sep 17 00:00:00 2001 From: songleo Date: Sun, 3 Jan 2016 13:53:23 +0800 Subject: [PATCH 11/20] modified: 18.1.md new file: 18.10.md new file: 18.11.md new file: 18.2.md new file: 18.3.md new file: 18.4.md modified: 18.5.md new file: 18.6.md new file: 18.7.md new file: 18.8.md new file: 18.9.md modified: directory.md --- eBook/18.1.md | 19 ++++---- eBook/18.10.md | 23 ++++++++++ eBook/18.11.md | 19 ++++++++ eBook/18.2.md | 46 +++++++++++++++++++ eBook/18.3.md | 28 ++++++++++++ eBook/18.4.md | 34 ++++++++++++++ eBook/18.5.md | 103 +++++++++++++++-------------------------- eBook/18.6.md | 23 ++++++++++ eBook/18.7.md | 52 +++++++++++++++++++++ eBook/18.8.md | 112 +++++++++++++++++++++++++++++++++++++++++++++ eBook/18.9.md | 20 ++++++++ eBook/directory.md | 13 +++++- 12 files changed, 417 insertions(+), 75 deletions(-) create mode 100644 eBook/18.10.md create mode 100644 eBook/18.11.md create mode 100644 eBook/18.2.md create mode 100644 eBook/18.3.md create mode 100644 eBook/18.4.md create mode 100644 eBook/18.6.md create mode 100644 eBook/18.7.md create mode 100644 eBook/18.8.md create mode 100644 eBook/18.9.md diff --git a/eBook/18.1.md b/eBook/18.1.md index 7b6bc96..3862f24 100644 --- a/eBook/18.1.md +++ b/eBook/18.1.md @@ -32,23 +32,26 @@ for ix, ch := range str { 如何获取一个字符串的字符数: - 最快速:`utf8.RuneCountInString(str)` - + 最快速: + ```go + utf8.RuneCountInString(str) + ``` `len([]int(str)) //TBD` (5)如何连接字符串: - 最快速: `with a bytes.Buffer`(参考[章节7.2](07.2.md)) + 最快速: + `with a bytes.Buffer`(参考[章节7.2](07.2.md)) `Strings.Join()`(参考[章节4.7](04.7.md)) `+=` - ```go - str1 := "Hello " - str2 := "World!" - str1 += str2 //str1 == "Hello World!" - ``` + ```go + str1 := "Hello " + str2 := "World!" + str1 += str2 //str1 == "Hello World!" + ``` (6)如何解析命令行参数:使用`os`或者`flag`包 diff --git a/eBook/18.10.md b/eBook/18.10.md new file mode 100644 index 0000000..b2da43b --- /dev/null +++ b/eBook/18.10.md @@ -0,0 +1,23 @@ +# 18.10 其他 + + 如何在程序出错时停止: + +```go +if err != nil { + fmt.Printf(“Program stopping with error %v”, err) + os.Exit(1) +} +``` + + 或者: +```go +if err != nil { +panic(“ERROR occurred: “ + err.Error()) +} +``` + +## 链接 + +- [目录](directory.md) +- 上一章:[运算符模板和接口](17.4.md) +- 下一节:[字符串](18.1.md) \ No newline at end of file diff --git a/eBook/18.11.md b/eBook/18.11.md new file mode 100644 index 0000000..4201d3e --- /dev/null +++ b/eBook/18.11.md @@ -0,0 +1,19 @@ +# 18.11 出于性能考虑的最佳实践和建议 + +(1)尽可能的使用:=去初始化声明一个变量(在函数内部); +(2)尽可能的使用字符代替字符串; +(3)尽可能的使用切片代替数组; +(4)尽可能的使用数组和切片代替映射(详见 参考文献15); +(5)如果只想获取切片中某项值,不需要值的索引,尽可能的使用for range去遍历切片,这比必须去查询切片中的每个元素要快一些; +(6)当数组元素是稀疏的(例如有很多0值或者空值),使用映射会降低内存消耗; +(7)初始化映射时指定其容量; +(8)当定义一个方法时,使用指针类型作为方法的接受者; +(9)在代码中使用常量或者标志提取常亮的值; +(10)尽可能在需要分配大量内存时使用缓存; +(11)使用缓存模板(参考15.7小节)。 + +## 链接 + +- [目录](directory.md) +- 上一章:[运算符模板和接口](17.4.md) +- 下一节:[字符串](18.1.md) \ No newline at end of file diff --git a/eBook/18.2.md b/eBook/18.2.md new file mode 100644 index 0000000..173f401 --- /dev/null +++ b/eBook/18.2.md @@ -0,0 +1,46 @@ +# 18.2 数组和切片 + + 创建: `arr1 := new([len]type)` + + `slice1 := make([]type, len)` + + 初始化:`arr1 := [...]type{i1, i2, i3, i4, i5}` + + `arrKeyValue := [len]type{i1: val1, i2: val2}` + + `var slice1 []type = arr1[start:end]` + +(1)如何截断数组或者切片的最后一个元素: + + `line = line[:len(line)-1]` + +(2)如何使用`for`或者`for-range`遍历一个数组(或者切片): + +```go +for i:=0; i < len(arr); i++ { +… = arr[i] +} +for ix, value := range arr { +… +} +``` + +(3)如何在一个二维数组或者切片arr2Dim中查找一个指定值V: + +```go +found := false +Found: for row := range arr2Dim { + for column := range arr2Dim[row] { + if arr2Dim[row][column] == V{ + found = true + break Found + } + } +} +``` + +## 链接 + +- [目录](directory.md) +- 上一章:[运算符模板和接口](17.4.md) +- 下一节:[字符串](18.1.md) \ No newline at end of file diff --git a/eBook/18.3.md b/eBook/18.3.md new file mode 100644 index 0000000..3617e5a --- /dev/null +++ b/eBook/18.3.md @@ -0,0 +1,28 @@ +# 18.3 映射 + + 创建: `map1 := make(map[keytype]valuetype)` + 初始化: `map1 := map[string]int{"one": 1, "two": 2}` + +(1)如何使用`for`或者`for-range`遍历一个映射: + +```go +for key, value := range map1 { +… +} +``` + +(2)如何在一个映射中检测键key1是否存在: + + `val1, isPresent = map1[key1]` + + 返回值: 键`key1`对应的值或者`0`, `true`或者`false` + +(3)如何在映射中删除一个键: + + `delete(map1, key1)` + +## 链接 + +- [目录](directory.md) +- 上一章:[运算符模板和接口](17.4.md) +- 下一节:[字符串](18.1.md) \ No newline at end of file diff --git a/eBook/18.4.md b/eBook/18.4.md new file mode 100644 index 0000000..301021b --- /dev/null +++ b/eBook/18.4.md @@ -0,0 +1,34 @@ +# 18.4 结构体 + + 创建: + +```go +type struct1 struct { + field1 type1 + field2 type2 + … +} +ms := new(struct1) +``` + + 初始化: + +```go +ms := &struct1{10, 15.5, "Chris"} +``` + + 当结构体的命名以大写字母开头时,该结构体在包外可见。 + 通常情况下,为每个结构体定义一个构建函数,并推荐使用构建函数初始化结构体(参考例10.2): + +```go +ms := Newstruct1{10, 15.5, "Chris"} +func Newstruct1(n int, f float32, name string) *struct1 { + return &struct1{n, f, name} +} +``` + +## 链接 + +- [目录](directory.md) +- 上一章:[运算符模板和接口](17.4.md) +- 下一节:[字符串](18.1.md) \ No newline at end of file diff --git a/eBook/18.5.md b/eBook/18.5.md index 7dfcda6..0b048bb 100644 --- a/eBook/18.5.md +++ b/eBook/18.5.md @@ -1,67 +1,38 @@ -# 18.1 字符串 - -(1)如何修改字符串中的一个字符: - -```go -str:="hello" -c:=[]byte(s) -c[0]='c' -s2:= string(c) // s2 == "cello" -``` - -(2)如何获取字符串的子串: - -```go -substr := str[n:m] -``` - -(3)如何使用for或者for-range遍历一个字符串: - -```go -// gives only the bytes: -for i:=0; i < len(str); i++ { -… = str[i] -} -// gives the Unicode characters: -for ix, ch := range str { -… -} -``` - -(4)如何获取一个字符串的字节数:`len(str)` - - 如何获取一个字符串的字符数: - - 最快速:`utf8.RuneCountInString(str)` - - `len([]int(str)) //TBD` - -(5)如何连接字符串: - - 最快速: `with a bytes.Buffer`(参考[章节7.2](07.2.md)) - - `Strings.Join()`(参考[章节4.7](04.7.md)) - - `+=` - - ```go - str1 := "Hello " - str2 := "World!" - str1 += str2 //str1 == "Hello World!" - ``` - -(6)如何解析命令行参数:使用`os`或者`flag`包 - - (参考[例12.4](examples/chapter_12/fileinput.go)) - -## 链接 - -- [目录](directory.md) -- 上一章:[运算符模板和接口](17.4.md) -- 下一节:[字符串](18.1.md) - -## 链接 - -- [目录](directory.md) -- 上一章:[运算符模板和接口](17.4.md) +# 18.5 接口 + +(1)如何检测一个值v是否实现了一个接口`Stringer`: + +```go +if v, ok := v.(Stringer); ok { + fmt.Printf("implements String(): %s\n", v.String()) +} +``` + +(2)如何使用接口实现一个类型分类函数: + +```go +func classifier(items ...interface{}) { + for i, x := range items { + switch x.(type) { + case bool: + fmt.Printf("param #%d is a bool\n", i) + case float64: + fmt.Printf("param #%d is a float64\n", i) + case int, int64: + fmt.Printf("param #%d is an int\n", i) + case nil: + fmt.Printf("param #%d is nil\n", i) + case string: + fmt.Printf("param #%d is a string\n", i) + default: + fmt.Printf("param #%d’s type is unknown\n", i) + } + } +} +``` + +## 链接 + +- [目录](directory.md) +- 上一章:[运算符模板和接口](17.4.md) - 下一节:[字符串](18.1.md) \ No newline at end of file diff --git a/eBook/18.6.md b/eBook/18.6.md new file mode 100644 index 0000000..8ee59d1 --- /dev/null +++ b/eBook/18.6.md @@ -0,0 +1,23 @@ +# 18.6 函数 + + 如何使用内建函数recover停止panic过程(参考13.3小节): + +```go +func protect(g func()) { + defer func() { + log.Println("done") + // Println executes normally even if there is a panic + if x := recover(); x != nil { + log.Printf("run time panic: %v", x) + } + }() + log.Println("start") + g() +} +``` + +## 链接 + +- [目录](directory.md) +- 上一章:[运算符模板和接口](17.4.md) +- 下一节:[字符串](18.1.md) \ No newline at end of file diff --git a/eBook/18.7.md b/eBook/18.7.md new file mode 100644 index 0000000..b7208c4 --- /dev/null +++ b/eBook/18.7.md @@ -0,0 +1,52 @@ +# 18.7 文件 + +(1)如何打开一个文件并读取: + +```go +file, err := os.Open("input.dat") + if err != nil { + fmt.Printf("An error occurred on opening the inputfile\n" + + "Does the file exist?\n" + + "Have you got acces to it?\n") + return + } + defer file.Close() + iReader := bufio.NewReader(file) + for { + str, err := iReader.ReadString('\n') + if err != nil { + return // error or EOF + } + fmt.Printf("The input was: %s", str) + } +``` + +(2)如何通过切片读写文件: + +```go +func cat(f *file.File) { + const NBUF = 512 + var buf [NBUF]byte + for { + switch nr, er := f.Read(buf[:]); true { + case nr < 0: + fmt.Fprintf(os.Stderr, "cat: error reading from %s: %s\n", + f.String(), er.String()) + os.Exit(1) + case nr == 0: // EOF + return + case nr > 0: + if nw, ew := file.Stdout.Write(buf[0:nr]); nw != nr { + fmt.Fprintf(os.Stderr, "cat: error writing from %s: %s\n", + f.String(), ew.String()) + } + } + } +} +``` + +## 链接 + +- [目录](directory.md) +- 上一章:[运算符模板和接口](17.4.md) +- 下一节:[字符串](18.1.md) \ No newline at end of file diff --git a/eBook/18.8.md b/eBook/18.8.md new file mode 100644 index 0000000..7b1aecb --- /dev/null +++ b/eBook/18.8.md @@ -0,0 +1,112 @@ +# 18.8 协程(goroutine)与通道(channel) + + 出于性能考虑的建议: + + 实践经验表明,如果你使用并行性获得高于串行运算的效率:在协程内部已经完成的大部分工作,其开销比创建协程和协程间通信还高。 + + 1 出于出于性能考虑建议使用带缓存的通道: + 使用带缓存的通道很轻易成倍提高它的吞吐量,某些场景其性能可以提高至10倍甚至更多。通过调整通道的容量,你可以尝试着更进一步的优化其的性能。 + + 2 限制一个通道的数据数量并将它们封装在成一个数组: + + 如果使用通道传递大量单独的数据,那么通道将变成你的性能瓶颈。然而,当将数据块打包封装成数组,在接收端解压数据时,性能可以提高至10倍。 + + 创建:`ch := make(chan type, buf)` + + (1)如何使用for或者for-range遍历一个通道: +```go +for v := range ch { + // do something with v +} +``` + + (2)如何检测一个通道ch是否是关闭的: + +```go +//read channel until it closes or error-condition +for { + if input, open := <-ch; !open { + break + } + fmt.Printf(“%s “, input) +} +``` + + 或者使用(1)自动检测。 + + (3)如何通过一个通道让主程序等待直到协程完成: + (信号量模式): +```go +ch := make(chan int) // Allocate a channel. +// Start something in a goroutine; when it completes, signal on the channel. +go func() { + // doSomething + ch <- 1 // Send a signal; value does not matter. +}() +doSomethingElseForAWhile() +<-ch // Wait for goroutine to finish; discard sent value. +``` + + 如果希望程序必须一直阻塞,在匿名函数中省略 `ch <- 1`即可。 + + (4)通道的工厂模板:下面的函数是一个通道工厂,启动一个匿名函数作为协程以生产通道 + +```go +func pump() chan int { + ch := make(chan int) + go func() { + for i := 0; ; i++ { + ch <- i + } + }() + return ch +} +``` + + (5)通道迭代器模板: + + (6)如何限制并发处理请求的数量:参考14.11小节 + + (7)如何在多核CPU上实现并行计算:参考14.13小节 + + (8)如何停止一个协程:`runtime.Goexit()` + + (9)简单的超时模板: + +```go +timeout := make(chan bool, 1) +go func() { + time.Sleep(1e9) // one second + timeout <- true +}() +select { + case <-ch: + // a read from ch has occurred + case <-timeout: + // the read from ch has timed out +} +``` + + (10)如何使用输入通道和输出通道代替锁: + +```go +func Worker(in, out chan *Task) { + for { + t := <-in + process(t) + out <- t + } +} +``` + + (11)如何在同步调用运行时间过长时将之丢弃:参考14.5小节 第二个变体 + + (12)如何在通道中使用计时器和定时器:参考14.5小节 + + (13)典型的服务器后端模型:参考14.4小节 + +## 链接 + +- [目录](directory.md) +- 上一章:[运算符模板和接口](17.4.md) +- 下一节:[字符串](18.1.md) \ No newline at end of file diff --git a/eBook/18.9.md b/eBook/18.9.md new file mode 100644 index 0000000..898055b --- /dev/null +++ b/eBook/18.9.md @@ -0,0 +1,20 @@ +# 18.9 网络和网页应用 + +## 18.9.1 模板: + + 制作、解析并是模块生效: + +```go +var strTempl = template.Must(template.New(“TName”).Parse(strTemplateHTML)) +``` + 在网页应用中使用HTML过滤器过滤HTML特殊字符: + + `{{html .}}` 或者通过一个字段 `FieldName {{ .FieldName |html }}` + + 使用缓存模板(参考15.7小节) + +## 链接 + +- [目录](directory.md) +- 上一章:[运算符模板和接口](17.4.md) +- 下一节:[字符串](18.1.md) \ No newline at end of file diff --git a/eBook/directory.md b/eBook/directory.md index eac2c1d..d8bf476 100644 --- a/eBook/directory.md +++ b/eBook/directory.md @@ -149,7 +149,18 @@ - 第16章:常见的陷阱与错误 - 第17章:模式 -- 第18章:出于性能考虑的实用代码片段 +- 第18章:[出于性能考虑的实用代码片段](18.0.md) + -18.1 + -18.2 + -18.3 + -18.4 + -18.5 + -18.6 + -18.7 + -18.8 + -18.9 + -18.10 + -18.11 - 第19章:构建一个完整的应用程序 - 第20章:Go 语言在 Google App Engine 的使用 - 第21章:实际部署案例 From 34534d240cc4af2b12a1c049449b2ce8a95bd9eb Mon Sep 17 00:00:00 2001 From: songleo Date: Sun, 3 Jan 2016 14:16:28 +0800 Subject: [PATCH 12/20] modified: eBook/18.11.md modified: eBook/18.8.md --- eBook/18.11.md | 13 ++++++++++++- eBook/18.8.md | 15 ++++++++++++--- 2 files changed, 24 insertions(+), 4 deletions(-) diff --git a/eBook/18.11.md b/eBook/18.11.md index 4201d3e..29f6fe9 100644 --- a/eBook/18.11.md +++ b/eBook/18.11.md @@ -1,17 +1,28 @@ # 18.11 出于性能考虑的最佳实践和建议 (1)尽可能的使用:=去初始化声明一个变量(在函数内部); + (2)尽可能的使用字符代替字符串; + (3)尽可能的使用切片代替数组; + (4)尽可能的使用数组和切片代替映射(详见 参考文献15); -(5)如果只想获取切片中某项值,不需要值的索引,尽可能的使用for range去遍历切片,这比必须去查询切片中的每个元素要快一些; + +(5)如果只想获取切片中某项值,不需要值的索引,尽可能的使用`for range`去遍历切片,这比必须去查询切片中的每个元素要快一些; + (6)当数组元素是稀疏的(例如有很多0值或者空值),使用映射会降低内存消耗; + (7)初始化映射时指定其容量; + (8)当定义一个方法时,使用指针类型作为方法的接受者; + (9)在代码中使用常量或者标志提取常亮的值; + (10)尽可能在需要分配大量内存时使用缓存; + (11)使用缓存模板(参考15.7小节)。 + ## 链接 - [目录](directory.md) diff --git a/eBook/18.8.md b/eBook/18.8.md index 7b1aecb..04cd71c 100644 --- a/eBook/18.8.md +++ b/eBook/18.8.md @@ -11,16 +11,21 @@ 如果使用通道传递大量单独的数据,那么通道将变成你的性能瓶颈。然而,当将数据块打包封装成数组,在接收端解压数据时,性能可以提高至10倍。 + 创建:`ch := make(chan type, buf)` - (1)如何使用for或者for-range遍历一个通道: + + (1)如何使用`for`或者`for-range`遍历一个通道: + + ```go for v := range ch { // do something with v } ``` - (2)如何检测一个通道ch是否是关闭的: + (2)如何检测一个通道`ch`是否是关闭的: + ```go //read channel until it closes or error-condition @@ -47,8 +52,10 @@ doSomethingElseForAWhile() <-ch // Wait for goroutine to finish; discard sent value. ``` + 如果希望程序必须一直阻塞,在匿名函数中省略 `ch <- 1`即可。 + (4)通道的工厂模板:下面的函数是一个通道工厂,启动一个匿名函数作为协程以生产通道 ```go @@ -69,7 +76,9 @@ func pump() chan int { (7)如何在多核CPU上实现并行计算:参考14.13小节 - (8)如何停止一个协程:`runtime.Goexit()` + + (8)如何停止一个协程:`runtime.Goexit()` + (9)简单的超时模板: From 0aefb4a0327a0fd808c2def543ecb96734d84a67 Mon Sep 17 00:00:00 2001 From: songleo Date: Sun, 3 Jan 2016 14:29:25 +0800 Subject: [PATCH 13/20] modified: eBook/18.1.md modified: eBook/18.11.md modified: eBook/18.2.md modified: eBook/18.3.md modified: eBook/18.4.md modified: eBook/18.8.md --- eBook/18.1.md | 16 ++++++++------ eBook/18.11.md | 4 ++-- eBook/18.2.md | 15 ++++++------- eBook/18.3.md | 10 ++++----- eBook/18.4.md | 10 ++++----- eBook/18.8.md | 58 ++++++++++++++++++++++---------------------------- 6 files changed, 52 insertions(+), 61 deletions(-) diff --git a/eBook/18.1.md b/eBook/18.1.md index 3862f24..daa75b9 100644 --- a/eBook/18.1.md +++ b/eBook/18.1.md @@ -30,22 +30,24 @@ for ix, ch := range str { (4)如何获取一个字符串的字节数:`len(str)` - 如何获取一个字符串的字符数: + 如何获取一个字符串的字符数: + + 最快速: - 最快速: ```go utf8.RuneCountInString(str) ``` - `len([]int(str)) //TBD` + + `len([]int(str)) //TBD` (5)如何连接字符串: - 最快速: - `with a bytes.Buffer`(参考[章节7.2](07.2.md)) + 最快速: +`with a bytes.Buffer`(参考[章节7.2](07.2.md)) - `Strings.Join()`(参考[章节4.7](04.7.md)) +`Strings.Join()`(参考[章节4.7](04.7.md)) - `+=` +`+=` ```go str1 := "Hello " diff --git a/eBook/18.11.md b/eBook/18.11.md index 29f6fe9..996fbaf 100644 --- a/eBook/18.11.md +++ b/eBook/18.11.md @@ -26,5 +26,5 @@ ## 链接 - [目录](directory.md) -- 上一章:[运算符模板和接口](17.4.md) -- 下一节:[字符串](18.1.md) \ No newline at end of file +- 上一节:[其他](18.10.md) +- 下一章:[构建一个完整的应用程序](19.0.md) \ No newline at end of file diff --git a/eBook/18.2.md b/eBook/18.2.md index 173f401..c41c4a1 100644 --- a/eBook/18.2.md +++ b/eBook/18.2.md @@ -1,14 +1,11 @@ # 18.2 数组和切片 - 创建: `arr1 := new([len]type)` +创建: `arr1 := new([len]type)` + `slice1 := make([]type, len)` - `slice1 := make([]type, len)` - - 初始化:`arr1 := [...]type{i1, i2, i3, i4, i5}` - - `arrKeyValue := [len]type{i1: val1, i2: val2}` - - `var slice1 []type = arr1[start:end]` +初始化:`arr1 := [...]type{i1, i2, i3, i4, i5}` + `arrKeyValue := [len]type{i1: val1, i2: val2}` + `var slice1 []type = arr1[start:end]` (1)如何截断数组或者切片的最后一个元素: @@ -25,7 +22,7 @@ for ix, value := range arr { } ``` -(3)如何在一个二维数组或者切片arr2Dim中查找一个指定值V: +(3)如何在一个二维数组或者切片`arr2Dim`中查找一个指定值`V`: ```go found := false diff --git a/eBook/18.3.md b/eBook/18.3.md index 3617e5a..be1d763 100644 --- a/eBook/18.3.md +++ b/eBook/18.3.md @@ -1,7 +1,7 @@ # 18.3 映射 - 创建: `map1 := make(map[keytype]valuetype)` - 初始化: `map1 := map[string]int{"one": 1, "two": 2}` +创建: `map1 := make(map[keytype]valuetype)` +初始化: `map1 := map[string]int{"one": 1, "two": 2}` (1)如何使用`for`或者`for-range`遍历一个映射: @@ -13,13 +13,13 @@ for key, value := range map1 { (2)如何在一个映射中检测键key1是否存在: - `val1, isPresent = map1[key1]` +`val1, isPresent = map1[key1]` - 返回值: 键`key1`对应的值或者`0`, `true`或者`false` +返回值: 键`key1`对应的值或者`0`, `true`或者`false` (3)如何在映射中删除一个键: - `delete(map1, key1)` +`delete(map1, key1)` ## 链接 diff --git a/eBook/18.4.md b/eBook/18.4.md index 301021b..b18b354 100644 --- a/eBook/18.4.md +++ b/eBook/18.4.md @@ -1,7 +1,6 @@ # 18.4 结构体 - 创建: - +创建: ```go type struct1 struct { field1 type1 @@ -11,14 +10,13 @@ type struct1 struct { ms := new(struct1) ``` - 初始化: - +初始化: ```go ms := &struct1{10, 15.5, "Chris"} ``` - 当结构体的命名以大写字母开头时,该结构体在包外可见。 - 通常情况下,为每个结构体定义一个构建函数,并推荐使用构建函数初始化结构体(参考例10.2): +当结构体的命名以大写字母开头时,该结构体在包外可见。 +通常情况下,为每个结构体定义一个构建函数,并推荐使用构建函数初始化结构体(参考例10.2): ```go ms := Newstruct1{10, 15.5, "Chris"} diff --git a/eBook/18.8.md b/eBook/18.8.md index 04cd71c..5c7e632 100644 --- a/eBook/18.8.md +++ b/eBook/18.8.md @@ -1,22 +1,20 @@ # 18.8 协程(goroutine)与通道(channel) - 出于性能考虑的建议: +出于性能考虑的建议: - 实践经验表明,如果你使用并行性获得高于串行运算的效率:在协程内部已经完成的大部分工作,其开销比创建协程和协程间通信还高。 +实践经验表明,如果你使用并行性获得高于串行运算的效率:在协程内部已经完成的大部分工作,其开销比创建协程和协程间通信还高。 - 1 出于出于性能考虑建议使用带缓存的通道: - 使用带缓存的通道很轻易成倍提高它的吞吐量,某些场景其性能可以提高至10倍甚至更多。通过调整通道的容量,你可以尝试着更进一步的优化其的性能。 +1 出于出于性能考虑建议使用带缓存的通道: - 2 限制一个通道的数据数量并将它们封装在成一个数组: +使用带缓存的通道很轻易成倍提高它的吞吐量,某些场景其性能可以提高至10倍甚至更多。通过调整通道的容量,你可以尝试着更进一步的优化其的性能。 - 如果使用通道传递大量单独的数据,那么通道将变成你的性能瓶颈。然而,当将数据块打包封装成数组,在接收端解压数据时,性能可以提高至10倍。 +2 限制一个通道的数据数量并将它们封装在成一个数组: +如果使用通道传递大量单独的数据,那么通道将变成你的性能瓶颈。然而,当将数据块打包封装成数组,在接收端解压数据时,性能可以提高至10倍。 - 创建:`ch := make(chan type, buf)` - - - (1)如何使用`for`或者`for-range`遍历一个通道: +创建:`ch := make(chan type, buf)` +(1)如何使用`for`或者`for-range`遍历一个通道: ```go for v := range ch { @@ -24,8 +22,7 @@ for v := range ch { } ``` - (2)如何检测一个通道`ch`是否是关闭的: - +(2)如何检测一个通道`ch`是否是关闭的: ```go //read channel until it closes or error-condition @@ -37,10 +34,11 @@ for { } ``` - 或者使用(1)自动检测。 +或者使用(1)自动检测。 + +(3)如何通过一个通道让主程序等待直到协程完成: +(信号量模式): - (3)如何通过一个通道让主程序等待直到协程完成: - (信号量模式): ```go ch := make(chan int) // Allocate a channel. // Start something in a goroutine; when it completes, signal on the channel. @@ -52,11 +50,9 @@ doSomethingElseForAWhile() <-ch // Wait for goroutine to finish; discard sent value. ``` +如果希望程序必须一直阻塞,在匿名函数中省略 `ch <- 1`即可。 - 如果希望程序必须一直阻塞,在匿名函数中省略 `ch <- 1`即可。 - - - (4)通道的工厂模板:下面的函数是一个通道工厂,启动一个匿名函数作为协程以生产通道 +(4)通道的工厂模板:下面的函数是一个通道工厂,启动一个匿名函数作为协程以生产通道 ```go func pump() chan int { @@ -70,17 +66,15 @@ func pump() chan int { } ``` - (5)通道迭代器模板: - - (6)如何限制并发处理请求的数量:参考14.11小节 - - (7)如何在多核CPU上实现并行计算:参考14.13小节 +(5)通道迭代器模板: + +(6)如何限制并发处理请求的数量:参考14.11小节 +(7)如何在多核CPU上实现并行计算:参考14.13小节 - (8)如何停止一个协程:`runtime.Goexit()` - - - (9)简单的超时模板: + (8)如何停止一个协程:`runtime.Goexit()` + +(9)简单的超时模板: ```go timeout := make(chan bool, 1) @@ -96,7 +90,7 @@ select { } ``` - (10)如何使用输入通道和输出通道代替锁: +(10)如何使用输入通道和输出通道代替锁: ```go func Worker(in, out chan *Task) { @@ -108,11 +102,11 @@ func Worker(in, out chan *Task) { } ``` - (11)如何在同步调用运行时间过长时将之丢弃:参考14.5小节 第二个变体 +(11)如何在同步调用运行时间过长时将之丢弃:参考14.5小节 第二个变体 - (12)如何在通道中使用计时器和定时器:参考14.5小节 +(12)如何在通道中使用计时器和定时器:参考14.5小节 - (13)典型的服务器后端模型:参考14.4小节 +(13)典型的服务器后端模型:参考14.4小节 ## 链接 From b3550e98e3e153658e25254682c0a1b20a624db2 Mon Sep 17 00:00:00 2001 From: songleo Date: Sun, 3 Jan 2016 14:43:12 +0800 Subject: [PATCH 14/20] modified: eBook/18.1.md modified: eBook/18.10.md modified: eBook/18.2.md modified: eBook/18.3.md modified: eBook/18.6.md modified: eBook/18.8.md modified: eBook/18.9.md --- eBook/18.1.md | 6 ++---- eBook/18.10.md | 4 ++-- eBook/18.2.md | 6 +++++- eBook/18.3.md | 1 + eBook/18.6.md | 2 +- eBook/18.8.md | 3 ++- eBook/18.9.md | 11 ++++++----- 7 files changed, 19 insertions(+), 14 deletions(-) diff --git a/eBook/18.1.md b/eBook/18.1.md index daa75b9..d7ca296 100644 --- a/eBook/18.1.md +++ b/eBook/18.1.md @@ -34,9 +34,7 @@ for ix, ch := range str { 最快速: - ```go - utf8.RuneCountInString(str) - ``` + `utf8.RuneCountInString(str)` `len([]int(str)) //TBD` @@ -47,7 +45,7 @@ for ix, ch := range str { `Strings.Join()`(参考[章节4.7](04.7.md)) -`+=` +使用`+=`: ```go str1 := "Hello " diff --git a/eBook/18.10.md b/eBook/18.10.md index b2da43b..18e1412 100644 --- a/eBook/18.10.md +++ b/eBook/18.10.md @@ -1,6 +1,6 @@ # 18.10 其他 - 如何在程序出错时停止: +如何在程序出错时停止: ```go if err != nil { @@ -9,7 +9,7 @@ if err != nil { } ``` - 或者: +或者: ```go if err != nil { panic(“ERROR occurred: “ + err.Error()) diff --git a/eBook/18.2.md b/eBook/18.2.md index c41c4a1..72dc42a 100644 --- a/eBook/18.2.md +++ b/eBook/18.2.md @@ -1,15 +1,19 @@ # 18.2 数组和切片 创建: `arr1 := new([len]type)` + `slice1 := make([]type, len)` 初始化:`arr1 := [...]type{i1, i2, i3, i4, i5}` + `arrKeyValue := [len]type{i1: val1, i2: val2}` + `var slice1 []type = arr1[start:end]` + (1)如何截断数组或者切片的最后一个元素: - `line = line[:len(line)-1]` +`line = line[:len(line)-1]` (2)如何使用`for`或者`for-range`遍历一个数组(或者切片): diff --git a/eBook/18.3.md b/eBook/18.3.md index be1d763..c260eee 100644 --- a/eBook/18.3.md +++ b/eBook/18.3.md @@ -1,6 +1,7 @@ # 18.3 映射 创建: `map1 := make(map[keytype]valuetype)` + 初始化: `map1 := map[string]int{"one": 1, "two": 2}` (1)如何使用`for`或者`for-range`遍历一个映射: diff --git a/eBook/18.6.md b/eBook/18.6.md index 8ee59d1..8b996a9 100644 --- a/eBook/18.6.md +++ b/eBook/18.6.md @@ -1,6 +1,6 @@ # 18.6 函数 - 如何使用内建函数recover停止panic过程(参考13.3小节): +如何使用内建函数`recover`停止`panic`过程(参考13.3小节): ```go func protect(g func()) { diff --git a/eBook/18.8.md b/eBook/18.8.md index 5c7e632..d9cdb66 100644 --- a/eBook/18.8.md +++ b/eBook/18.8.md @@ -37,6 +37,7 @@ for { 或者使用(1)自动检测。 (3)如何通过一个通道让主程序等待直到协程完成: + (信号量模式): ```go @@ -72,7 +73,7 @@ func pump() chan int { (7)如何在多核CPU上实现并行计算:参考14.13小节 - (8)如何停止一个协程:`runtime.Goexit()` +(8)如何停止一个协程:`runtime.Goexit()` (9)简单的超时模板: diff --git a/eBook/18.9.md b/eBook/18.9.md index 898055b..874703b 100644 --- a/eBook/18.9.md +++ b/eBook/18.9.md @@ -2,16 +2,17 @@ ## 18.9.1 模板: - 制作、解析并是模块生效: +制作、解析并是模块生效: ```go var strTempl = template.Must(template.New(“TName”).Parse(strTemplateHTML)) ``` - 在网页应用中使用HTML过滤器过滤HTML特殊字符: - - `{{html .}}` 或者通过一个字段 `FieldName {{ .FieldName |html }}` - 使用缓存模板(参考15.7小节) +在网页应用中使用HTML过滤器过滤HTML特殊字符: + +`{{html .}}` 或者通过一个字段 `FieldName {{ .FieldName |html }}` + +使用缓存模板(参考15.7小节) ## 链接 From 706c594a7e5192a38b9bcd47e8bfe6244c9ec697 Mon Sep 17 00:00:00 2001 From: songleo Date: Sun, 3 Jan 2016 14:49:10 +0800 Subject: [PATCH 15/20] modified: eBook/18.1.md modified: eBook/18.11.md modified: eBook/18.2.md modified: eBook/18.3.md --- eBook/18.1.md | 2 +- eBook/18.11.md | 2 +- eBook/18.2.md | 13 +++++++------ eBook/18.3.md | 2 +- 4 files changed, 10 insertions(+), 9 deletions(-) diff --git a/eBook/18.1.md b/eBook/18.1.md index d7ca296..00acb7b 100644 --- a/eBook/18.1.md +++ b/eBook/18.1.md @@ -15,7 +15,7 @@ s2:= string(c) // s2 == "cello" substr := str[n:m] ``` -(3)如何使用for或者for-range遍历一个字符串: +(3)如何使用`for`或者`for-range`遍历一个字符串: ```go // gives only the bytes: diff --git a/eBook/18.11.md b/eBook/18.11.md index 996fbaf..8aff0c8 100644 --- a/eBook/18.11.md +++ b/eBook/18.11.md @@ -8,7 +8,7 @@ (4)尽可能的使用数组和切片代替映射(详见 参考文献15); -(5)如果只想获取切片中某项值,不需要值的索引,尽可能的使用`for range`去遍历切片,这比必须去查询切片中的每个元素要快一些; +(5)如果只想获取切片中某项值,不需要值的索引,尽可能的使用`for range`去遍历切片,这比必须去查询切片中的每个元素要快一些; (6)当数组元素是稀疏的(例如有很多0值或者空值),使用映射会降低内存消耗; diff --git a/eBook/18.2.md b/eBook/18.2.md index 72dc42a..164da44 100644 --- a/eBook/18.2.md +++ b/eBook/18.2.md @@ -1,15 +1,16 @@ # 18.2 数组和切片 -创建: `arr1 := new([len]type)` +创建: +`arr1 := new([len]type)` - `slice1 := make([]type, len)` +`slice1 := make([]type, len)` -初始化:`arr1 := [...]type{i1, i2, i3, i4, i5}` +初始化: +`arr1 := [...]type{i1, i2, i3, i4, i5}` - `arrKeyValue := [len]type{i1: val1, i2: val2}` - - `var slice1 []type = arr1[start:end]` +`arrKeyValue := [len]type{i1: val1, i2: val2}` +`var slice1 []type = arr1[start:end]` (1)如何截断数组或者切片的最后一个元素: diff --git a/eBook/18.3.md b/eBook/18.3.md index c260eee..1f1d750 100644 --- a/eBook/18.3.md +++ b/eBook/18.3.md @@ -16,7 +16,7 @@ for key, value := range map1 { `val1, isPresent = map1[key1]` -返回值: 键`key1`对应的值或者`0`, `true`或者`false` +返回值:键`key1`对应的值或者`0`, `true`或者`false` (3)如何在映射中删除一个键: From 8fdcfd377600e9b7c033d7ce3365bf1408f8434b Mon Sep 17 00:00:00 2001 From: songleo Date: Sun, 3 Jan 2016 21:47:46 +0800 Subject: [PATCH 16/20] modified: eBook/18.1.md modified: eBook/18.10.md modified: eBook/18.11.md modified: eBook/18.2.md modified: eBook/18.3.md modified: eBook/18.4.md modified: eBook/18.5.md modified: eBook/18.6.md modified: eBook/18.7.md modified: eBook/18.8.md modified: eBook/18.9.md --- eBook/18.1.md | 6 ++---- eBook/18.10.md | 6 +++--- eBook/18.11.md | 6 +++--- eBook/18.2.md | 4 ++-- eBook/18.3.md | 6 +++--- eBook/18.4.md | 6 +++--- eBook/18.5.md | 4 ++-- eBook/18.6.md | 6 +++--- eBook/18.7.md | 4 ++-- eBook/18.8.md | 14 +++++++------- eBook/18.9.md | 6 +++--- 11 files changed, 33 insertions(+), 35 deletions(-) diff --git a/eBook/18.1.md b/eBook/18.1.md index 00acb7b..e46352c 100644 --- a/eBook/18.1.md +++ b/eBook/18.1.md @@ -34,9 +34,7 @@ for ix, ch := range str { 最快速: - `utf8.RuneCountInString(str)` - - `len([]int(str)) //TBD` +`utf8.RuneCountInString(str)` (5)如何连接字符串: @@ -55,7 +53,7 @@ for ix, ch := range str { (6)如何解析命令行参数:使用`os`或者`flag`包 - (参考[例12.4](examples/chapter_12/fileinput.go)) +(参考[例12.4](examples/chapter_12/fileinput.go)) ## 链接 diff --git a/eBook/18.10.md b/eBook/18.10.md index 18e1412..8282566 100644 --- a/eBook/18.10.md +++ b/eBook/18.10.md @@ -1,6 +1,6 @@ # 18.10 其他 -如何在程序出错时停止: +如何在程序出错时终止程序: ```go if err != nil { @@ -19,5 +19,5 @@ panic(“ERROR occurred: “ + err.Error()) ## 链接 - [目录](directory.md) -- 上一章:[运算符模板和接口](17.4.md) -- 下一节:[字符串](18.1.md) \ No newline at end of file +- 上一章:[网络和网页应用](18.9.md) +- 下一节:[出于性能考虑的最佳实践和建议](18.11.md) \ No newline at end of file diff --git a/eBook/18.11.md b/eBook/18.11.md index 8aff0c8..8074563 100644 --- a/eBook/18.11.md +++ b/eBook/18.11.md @@ -6,11 +6,11 @@ (3)尽可能的使用切片代替数组; -(4)尽可能的使用数组和切片代替映射(详见 参考文献15); +(4)尽可能的使用数组和切片代替映射(详见参考文献15); (5)如果只想获取切片中某项值,不需要值的索引,尽可能的使用`for range`去遍历切片,这比必须去查询切片中的每个元素要快一些; -(6)当数组元素是稀疏的(例如有很多0值或者空值),使用映射会降低内存消耗; +(6)当数组元素是稀疏的(例如有很多`0`值或者空值),使用映射会降低内存消耗; (7)初始化映射时指定其容量; @@ -20,7 +20,7 @@ (10)尽可能在需要分配大量内存时使用缓存; -(11)使用缓存模板(参考15.7小节)。 +(11)使用缓存模板(参考[章节15.7](15.7.md))。 ## 链接 diff --git a/eBook/18.2.md b/eBook/18.2.md index 164da44..a57b43c 100644 --- a/eBook/18.2.md +++ b/eBook/18.2.md @@ -44,5 +44,5 @@ Found: for row := range arr2Dim { ## 链接 - [目录](directory.md) -- 上一章:[运算符模板和接口](17.4.md) -- 下一节:[字符串](18.1.md) \ No newline at end of file +- 上一节:[字符串](18.1.md) +- 下一节:[映射](18.3.md) \ No newline at end of file diff --git a/eBook/18.3.md b/eBook/18.3.md index 1f1d750..310db42 100644 --- a/eBook/18.3.md +++ b/eBook/18.3.md @@ -2,7 +2,7 @@ 创建: `map1 := make(map[keytype]valuetype)` -初始化: `map1 := map[string]int{"one": 1, "two": 2}` +初始化: `map1 := map[string]int{"one": 1, "two": 2}` (1)如何使用`for`或者`for-range`遍历一个映射: @@ -25,5 +25,5 @@ for key, value := range map1 { ## 链接 - [目录](directory.md) -- 上一章:[运算符模板和接口](17.4.md) -- 下一节:[字符串](18.1.md) \ No newline at end of file +- 上一章:[数组和切片](18.2.md) +- 下一节:[结构体](18.4.md) \ No newline at end of file diff --git a/eBook/18.4.md b/eBook/18.4.md index b18b354..3e3d321 100644 --- a/eBook/18.4.md +++ b/eBook/18.4.md @@ -16,7 +16,7 @@ ms := &struct1{10, 15.5, "Chris"} ``` 当结构体的命名以大写字母开头时,该结构体在包外可见。 -通常情况下,为每个结构体定义一个构建函数,并推荐使用构建函数初始化结构体(参考例10.2): +通常情况下,为每个结构体定义一个构建函数,并推荐使用构建函数初始化结构体(参考[例10.2](examples/chapter_10/person.go)): ```go ms := Newstruct1{10, 15.5, "Chris"} @@ -28,5 +28,5 @@ func Newstruct1(n int, f float32, name string) *struct1 { ## 链接 - [目录](directory.md) -- 上一章:[运算符模板和接口](17.4.md) -- 下一节:[字符串](18.1.md) \ No newline at end of file +- 上一章:[映射](18.3.md) +- 下一节:[接口](18.5.md) \ No newline at end of file diff --git a/eBook/18.5.md b/eBook/18.5.md index 0b048bb..a3805bd 100644 --- a/eBook/18.5.md +++ b/eBook/18.5.md @@ -34,5 +34,5 @@ func classifier(items ...interface{}) { ## 链接 - [目录](directory.md) -- 上一章:[运算符模板和接口](17.4.md) -- 下一节:[字符串](18.1.md) \ No newline at end of file +- 上一章:[结构体](18.4.md) +- 下一节:[函数](18.6.md) \ No newline at end of file diff --git a/eBook/18.6.md b/eBook/18.6.md index 8b996a9..55afc8d 100644 --- a/eBook/18.6.md +++ b/eBook/18.6.md @@ -1,6 +1,6 @@ # 18.6 函数 -如何使用内建函数`recover`停止`panic`过程(参考13.3小节): +如何使用内建函数`recover`停止`panic`过程(参考[章节13.3](13.3.md)): ```go func protect(g func()) { @@ -19,5 +19,5 @@ func protect(g func()) { ## 链接 - [目录](directory.md) -- 上一章:[运算符模板和接口](17.4.md) -- 下一节:[字符串](18.1.md) \ No newline at end of file +- 上一章:[接口](18.5.md) +- 下一节:[文件](18.7.md) \ No newline at end of file diff --git a/eBook/18.7.md b/eBook/18.7.md index b7208c4..7bf6626 100644 --- a/eBook/18.7.md +++ b/eBook/18.7.md @@ -48,5 +48,5 @@ func cat(f *file.File) { ## 链接 - [目录](directory.md) -- 上一章:[运算符模板和接口](17.4.md) -- 下一节:[字符串](18.1.md) \ No newline at end of file +- 上一章:[函数](18.6.md) +- 下一节:[协程(goroutine)与通道(channel)](18.8.md) \ No newline at end of file diff --git a/eBook/18.8.md b/eBook/18.8.md index d9cdb66..55117a7 100644 --- a/eBook/18.8.md +++ b/eBook/18.8.md @@ -69,9 +69,9 @@ func pump() chan int { (5)通道迭代器模板: -(6)如何限制并发处理请求的数量:参考14.11小节 +(6)如何限制并发处理请求的数量:参考[章节14.11](14.11.md) -(7)如何在多核CPU上实现并行计算:参考14.13小节 +(7)如何在多核CPU上实现并行计算:参考[章节14.13](14.13.md) (8)如何停止一个协程:`runtime.Goexit()` @@ -103,14 +103,14 @@ func Worker(in, out chan *Task) { } ``` -(11)如何在同步调用运行时间过长时将之丢弃:参考14.5小节 第二个变体 +(11)如何在同步调用运行时间过长时将之丢弃:参考[章节14.5](14.5.md) 第二个变体 -(12)如何在通道中使用计时器和定时器:参考14.5小节 +(12)如何在通道中使用计时器和定时器:参考[章节14.5](14.5.md) -(13)典型的服务器后端模型:参考14.4小节 +(13)典型的服务器后端模型:参考[章节14.4](14.4.md) ## 链接 - [目录](directory.md) -- 上一章:[运算符模板和接口](17.4.md) -- 下一节:[字符串](18.1.md) \ No newline at end of file +- 上一章:[文件](18.7.md) +- 下一节:[网络和网页应用](18.9.md) \ No newline at end of file diff --git a/eBook/18.9.md b/eBook/18.9.md index 874703b..d600d74 100644 --- a/eBook/18.9.md +++ b/eBook/18.9.md @@ -12,10 +12,10 @@ var strTempl = template.Must(template.New(“TName”).Parse(strTemplateHTML)) `{{html .}}` 或者通过一个字段 `FieldName {{ .FieldName |html }}` -使用缓存模板(参考15.7小节) +使用缓存模板(参考[章节15.7](15.7.md)) ## 链接 - [目录](directory.md) -- 上一章:[运算符模板和接口](17.4.md) -- 下一节:[字符串](18.1.md) \ No newline at end of file +- 上一章:[协程(goroutine)与通道(channel)](18.8.md) +- 下一节:[其他](18.10.md) \ No newline at end of file From 63367af386e18735e5b41ff39eacba71fb136ad0 Mon Sep 17 00:00:00 2001 From: songleo Date: Sun, 3 Jan 2016 22:15:04 +0800 Subject: [PATCH 17/20] modified: eBook/18.1.md modified: eBook/18.11.md modified: eBook/18.2.md modified: eBook/18.5.md --- eBook/18.1.md | 6 ++---- eBook/18.11.md | 2 +- eBook/18.2.md | 3 ++- eBook/18.5.md | 2 +- 4 files changed, 6 insertions(+), 7 deletions(-) diff --git a/eBook/18.1.md b/eBook/18.1.md index e46352c..2e79f59 100644 --- a/eBook/18.1.md +++ b/eBook/18.1.md @@ -32,13 +32,11 @@ for ix, ch := range str { 如何获取一个字符串的字符数: - 最快速: - -`utf8.RuneCountInString(str)` + 最快速:`utf8.RuneCountInString(str)` (5)如何连接字符串: - 最快速: + 最快速: `with a bytes.Buffer`(参考[章节7.2](07.2.md)) `Strings.Join()`(参考[章节4.7](04.7.md)) diff --git a/eBook/18.11.md b/eBook/18.11.md index 8074563..8790b0a 100644 --- a/eBook/18.11.md +++ b/eBook/18.11.md @@ -1,6 +1,6 @@ # 18.11 出于性能考虑的最佳实践和建议 -(1)尽可能的使用:=去初始化声明一个变量(在函数内部); +(1)尽可能的使用`:=`去初始化声明一个变量(在函数内部); (2)尽可能的使用字符代替字符串; diff --git a/eBook/18.2.md b/eBook/18.2.md index a57b43c..5a388d0 100644 --- a/eBook/18.2.md +++ b/eBook/18.2.md @@ -1,11 +1,12 @@ # 18.2 数组和切片 -创建: +创建: `arr1 := new([len]type)` `slice1 := make([]type, len)` 初始化: + `arr1 := [...]type{i1, i2, i3, i4, i5}` `arrKeyValue := [len]type{i1: val1, i2: val2}` diff --git a/eBook/18.5.md b/eBook/18.5.md index a3805bd..7746efa 100644 --- a/eBook/18.5.md +++ b/eBook/18.5.md @@ -1,6 +1,6 @@ # 18.5 接口 -(1)如何检测一个值v是否实现了一个接口`Stringer`: +(1)如何检测一个值`v`是否实现了一个接口`Stringer`: ```go if v, ok := v.(Stringer); ok { From 84b4cd2a7bc71440f87ef2b0cd9e69b1a00fcf24 Mon Sep 17 00:00:00 2001 From: songleo Date: Sun, 3 Jan 2016 22:23:20 +0800 Subject: [PATCH 18/20] modified: eBook/18.2.md modified: eBook/18.3.md modified: eBook/18.4.md modified: eBook/directory.md --- eBook/18.2.md | 1 + eBook/18.3.md | 2 +- eBook/18.4.md | 2 +- eBook/directory.md | 11 ----------- 4 files changed, 3 insertions(+), 13 deletions(-) diff --git a/eBook/18.2.md b/eBook/18.2.md index 5a388d0..d5ab9f2 100644 --- a/eBook/18.2.md +++ b/eBook/18.2.md @@ -1,6 +1,7 @@ # 18.2 数组和切片 创建: + `arr1 := new([len]type)` `slice1 := make([]type, len)` diff --git a/eBook/18.3.md b/eBook/18.3.md index 310db42..040b0a9 100644 --- a/eBook/18.3.md +++ b/eBook/18.3.md @@ -12,7 +12,7 @@ for key, value := range map1 { } ``` -(2)如何在一个映射中检测键key1是否存在: +(2)如何在一个映射中检测键`key1`是否存在: `val1, isPresent = map1[key1]` diff --git a/eBook/18.4.md b/eBook/18.4.md index 3e3d321..2692fae 100644 --- a/eBook/18.4.md +++ b/eBook/18.4.md @@ -10,7 +10,7 @@ type struct1 struct { ms := new(struct1) ``` -初始化: +初始化: ```go ms := &struct1{10, 15.5, "Chris"} ``` diff --git a/eBook/directory.md b/eBook/directory.md index 97265ad..804e279 100644 --- a/eBook/directory.md +++ b/eBook/directory.md @@ -151,17 +151,6 @@ - 第16章:常见的陷阱与错误 - 第17章:模式 - 第18章:[出于性能考虑的实用代码片段](18.0.md) - -18.1 - -18.2 - -18.3 - -18.4 - -18.5 - -18.6 - -18.7 - -18.8 - -18.9 - -18.10 - -18.11 - 第19章:构建一个完整的应用程序 - 第20章:Go 语言在 Google App Engine 的使用 - 第21章:实际部署案例 From 830785f46e43e0f5b61079306d606b7e7bc97654 Mon Sep 17 00:00:00 2001 From: songleo Date: Sun, 3 Jan 2016 22:25:47 +0800 Subject: [PATCH 19/20] modified: eBook/18.10.md modified: eBook/18.3.md modified: eBook/18.4.md modified: eBook/18.5.md modified: eBook/18.6.md modified: eBook/18.7.md modified: eBook/18.8.md modified: eBook/18.9.md --- eBook/18.10.md | 2 +- eBook/18.3.md | 2 +- eBook/18.4.md | 2 +- eBook/18.5.md | 2 +- eBook/18.6.md | 2 +- eBook/18.7.md | 2 +- eBook/18.8.md | 2 +- eBook/18.9.md | 2 +- 8 files changed, 8 insertions(+), 8 deletions(-) diff --git a/eBook/18.10.md b/eBook/18.10.md index 8282566..79c42d8 100644 --- a/eBook/18.10.md +++ b/eBook/18.10.md @@ -19,5 +19,5 @@ panic(“ERROR occurred: “ + err.Error()) ## 链接 - [目录](directory.md) -- 上一章:[网络和网页应用](18.9.md) +- 上一节:[网络和网页应用](18.9.md) - 下一节:[出于性能考虑的最佳实践和建议](18.11.md) \ No newline at end of file diff --git a/eBook/18.3.md b/eBook/18.3.md index 040b0a9..de41251 100644 --- a/eBook/18.3.md +++ b/eBook/18.3.md @@ -25,5 +25,5 @@ for key, value := range map1 { ## 链接 - [目录](directory.md) -- 上一章:[数组和切片](18.2.md) +- 上一节:[数组和切片](18.2.md) - 下一节:[结构体](18.4.md) \ No newline at end of file diff --git a/eBook/18.4.md b/eBook/18.4.md index 2692fae..1d43d87 100644 --- a/eBook/18.4.md +++ b/eBook/18.4.md @@ -28,5 +28,5 @@ func Newstruct1(n int, f float32, name string) *struct1 { ## 链接 - [目录](directory.md) -- 上一章:[映射](18.3.md) +- 上一节:[映射](18.3.md) - 下一节:[接口](18.5.md) \ No newline at end of file diff --git a/eBook/18.5.md b/eBook/18.5.md index 7746efa..fcd260d 100644 --- a/eBook/18.5.md +++ b/eBook/18.5.md @@ -34,5 +34,5 @@ func classifier(items ...interface{}) { ## 链接 - [目录](directory.md) -- 上一章:[结构体](18.4.md) +- 上一节:[结构体](18.4.md) - 下一节:[函数](18.6.md) \ No newline at end of file diff --git a/eBook/18.6.md b/eBook/18.6.md index 55afc8d..3224c47 100644 --- a/eBook/18.6.md +++ b/eBook/18.6.md @@ -19,5 +19,5 @@ func protect(g func()) { ## 链接 - [目录](directory.md) -- 上一章:[接口](18.5.md) +- 上一节:[接口](18.5.md) - 下一节:[文件](18.7.md) \ No newline at end of file diff --git a/eBook/18.7.md b/eBook/18.7.md index 7bf6626..0112e5d 100644 --- a/eBook/18.7.md +++ b/eBook/18.7.md @@ -48,5 +48,5 @@ func cat(f *file.File) { ## 链接 - [目录](directory.md) -- 上一章:[函数](18.6.md) +- 上一节:[函数](18.6.md) - 下一节:[协程(goroutine)与通道(channel)](18.8.md) \ No newline at end of file diff --git a/eBook/18.8.md b/eBook/18.8.md index 55117a7..893b859 100644 --- a/eBook/18.8.md +++ b/eBook/18.8.md @@ -112,5 +112,5 @@ func Worker(in, out chan *Task) { ## 链接 - [目录](directory.md) -- 上一章:[文件](18.7.md) +- 上一节:[文件](18.7.md) - 下一节:[网络和网页应用](18.9.md) \ No newline at end of file diff --git a/eBook/18.9.md b/eBook/18.9.md index d600d74..9eefaa6 100644 --- a/eBook/18.9.md +++ b/eBook/18.9.md @@ -17,5 +17,5 @@ var strTempl = template.Must(template.New(“TName”).Parse(strTemplateHTML)) ## 链接 - [目录](directory.md) -- 上一章:[协程(goroutine)与通道(channel)](18.8.md) +- 上一节:[协程(goroutine)与通道(channel)](18.8.md) - 下一节:[其他](18.10.md) \ No newline at end of file From bc390fa8722abc40bc898d3e4b19155625516cc3 Mon Sep 17 00:00:00 2001 From: songleo Date: Sun, 3 Jan 2016 22:45:20 +0800 Subject: [PATCH 20/20] modified: eBook/18.11.md modified: eBook/18.5.md modified: eBook/18.6.md modified: eBook/18.8.md modified: eBook/18.9.md --- eBook/18.11.md | 4 ++-- eBook/18.5.md | 2 +- eBook/18.6.md | 2 +- eBook/18.8.md | 22 +++++++++++----------- eBook/18.9.md | 4 ++-- 5 files changed, 17 insertions(+), 17 deletions(-) diff --git a/eBook/18.11.md b/eBook/18.11.md index 8790b0a..b102272 100644 --- a/eBook/18.11.md +++ b/eBook/18.11.md @@ -10,13 +10,13 @@ (5)如果只想获取切片中某项值,不需要值的索引,尽可能的使用`for range`去遍历切片,这比必须去查询切片中的每个元素要快一些; -(6)当数组元素是稀疏的(例如有很多`0`值或者空值),使用映射会降低内存消耗; +(6)当数组元素是稀疏的(例如有很多`0`值或者空值`nil`),使用映射会降低内存消耗; (7)初始化映射时指定其容量; (8)当定义一个方法时,使用指针类型作为方法的接受者; -(9)在代码中使用常量或者标志提取常亮的值; +(9)在代码中使用常量或者标志提取常量的值; (10)尽可能在需要分配大量内存时使用缓存; diff --git a/eBook/18.5.md b/eBook/18.5.md index fcd260d..009178a 100644 --- a/eBook/18.5.md +++ b/eBook/18.5.md @@ -1,6 +1,6 @@ # 18.5 接口 -(1)如何检测一个值`v`是否实现了一个接口`Stringer`: +(1)如何检测一个值`v`是否实现了接口`Stringer`: ```go if v, ok := v.(Stringer); ok { diff --git a/eBook/18.6.md b/eBook/18.6.md index 3224c47..ee2093b 100644 --- a/eBook/18.6.md +++ b/eBook/18.6.md @@ -1,6 +1,6 @@ # 18.6 函数 -如何使用内建函数`recover`停止`panic`过程(参考[章节13.3](13.3.md)): +如何使用内建函数`recover`终止`panic`过程(参考[章节13.3](13.3.md)): ```go func protect(g func()) { diff --git a/eBook/18.8.md b/eBook/18.8.md index 893b859..7e9f0b6 100644 --- a/eBook/18.8.md +++ b/eBook/18.8.md @@ -2,17 +2,17 @@ 出于性能考虑的建议: -实践经验表明,如果你使用并行性获得高于串行运算的效率:在协程内部已经完成的大部分工作,其开销比创建协程和协程间通信还高。 +实践经验表明,如果你使用并行计算获得高于串行运算的效率:在协程内部已经完成的大部分工作,其开销比创建协程和协程间通信还高。 -1 出于出于性能考虑建议使用带缓存的通道: +1 出于性能考虑建议使用带缓存的通道: -使用带缓存的通道很轻易成倍提高它的吞吐量,某些场景其性能可以提高至10倍甚至更多。通过调整通道的容量,你可以尝试着更进一步的优化其的性能。 +使用带缓存的通道可以很轻易成倍提高它的吞吐量,某些场景其性能可以提高至10倍甚至更多。通过调整通道的容量,你可以尝试着更进一步的优化其性能。 -2 限制一个通道的数据数量并将它们封装在成一个数组: +2 限制一个通道的数据数量并将它们封装成一个数组: -如果使用通道传递大量单独的数据,那么通道将变成你的性能瓶颈。然而,当将数据块打包封装成数组,在接收端解压数据时,性能可以提高至10倍。 +如果使用通道传递大量单独的数据,那么通道将变成性能瓶颈。然而,将数据块打包封装成数组,在接收端解压数据时,性能可以提高至10倍。 -创建:`ch := make(chan type, buf)` +创建:`ch := make(chan type,buf)` (1)如何使用`for`或者`for-range`遍历一个通道: @@ -22,7 +22,7 @@ for v := range ch { } ``` -(2)如何检测一个通道`ch`是否是关闭的: +(2)如何检测一个通道`ch`是否关闭: ```go //read channel until it closes or error-condition @@ -30,7 +30,7 @@ for { if input, open := <-ch; !open { break } - fmt.Printf(“%s “, input) + fmt.Printf("%s", input) } ``` @@ -51,9 +51,9 @@ doSomethingElseForAWhile() <-ch // Wait for goroutine to finish; discard sent value. ``` -如果希望程序必须一直阻塞,在匿名函数中省略 `ch <- 1`即可。 +如果希望程序一直阻塞,在匿名函数中省略 `ch <- 1`即可。 -(4)通道的工厂模板:下面的函数是一个通道工厂,启动一个匿名函数作为协程以生产通道 +(4)通道的工厂模板:以下函数是一个通道工厂,启动一个匿名函数作为协程以生产通道: ```go func pump() chan int { @@ -73,7 +73,7 @@ func pump() chan int { (7)如何在多核CPU上实现并行计算:参考[章节14.13](14.13.md) -(8)如何停止一个协程:`runtime.Goexit()` +(8)如何终止一个协程:`runtime.Goexit()` (9)简单的超时模板: diff --git a/eBook/18.9.md b/eBook/18.9.md index 9eefaa6..491d7e0 100644 --- a/eBook/18.9.md +++ b/eBook/18.9.md @@ -2,10 +2,10 @@ ## 18.9.1 模板: -制作、解析并是模块生效: +制作、解析并使模板生效: ```go -var strTempl = template.Must(template.New(“TName”).Parse(strTemplateHTML)) +var strTempl = template.Must(template.New("TName").Parse(strTemplateHTML)) ``` 在网页应用中使用HTML过滤器过滤HTML特殊字符: