From c467e4631a43d5749365263222e6095aed15fb4c Mon Sep 17 00:00:00 2001 From: skiy Date: Fri, 23 Oct 2015 14:53:45 +0800 Subject: [PATCH 01/12] Update 07.2.md MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 勘误 --- eBook/07.2.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/eBook/07.2.md b/eBook/07.2.md index 7ba6557..7b5b2c3 100644 --- a/eBook/07.2.md +++ b/eBook/07.2.md @@ -121,7 +121,7 @@ func sum(a []int) int { return s } -func main { +func main() { var arr = [5]int{0, 1, 2, 3, 4} sum(arr[:]) } From a021d742499d94ce783295cf03dd9e59dc5e5891 Mon Sep 17 00:00:00 2001 From: skiy Date: Fri, 23 Oct 2015 15:36:23 +0800 Subject: [PATCH 02/12] Update 07.2.md MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 勘误 --- eBook/07.2.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/eBook/07.2.md b/eBook/07.2.md index 7ba6557..c9c47c4 100644 --- a/eBook/07.2.md +++ b/eBook/07.2.md @@ -226,7 +226,7 @@ v := make([]int, 10, 50) 这样分配一个有 50 个 int 值的数组,并且创建了一个长度为 10,容量为 50 的 切片 v,该 切片 指向数组的前 10 个元素。 **问题 7.3** 给定 `s := make([]byte, 5)`,len(s) 和 cap(s) 分别是多少?`s = s[2:4]`,len(s) 和 cap(s) 又分别是多少? -**问题 7.4** 假设 `s1 := []byte{'p', 'o', 'e', 'm'}` 且 `s2 := d[2:]`,s2 的值是多少?如果我们执行 `s2[1] == 't'`,s1 和 s2 现在的值又分配是多少? +**问题 7.4** 假设 `s1 := []byte{'p', 'o', 'e', 'm'}` 且 `s2 := s1[2:]`,s2 的值是多少?如果我们执行 `s2[1] = 't'`,s1 和 s2 现在的值又分配是多少? ## 7.2.5 多维 切片 From be2cea6c549b00c595543afa56b6ed7d3ea78636 Mon Sep 17 00:00:00 2001 From: ArkBriar Date: Fri, 23 Oct 2015 15:41:29 +0800 Subject: [PATCH 03/12] =?UTF-8?q?=E4=BF=AE=E5=A4=8D10.8=E7=9A=84=E4=BB=A3?= =?UTF-8?q?=E7=A0=81=EF=BC=8C=E4=BD=BFGo1.5.1=E8=83=BD=E5=A4=9F=E8=BF=90?= =?UTF-8?q?=E8=A1=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- eBook/10.8.md | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/eBook/10.8.md b/eBook/10.8.md index 4781493..b5ceedc 100644 --- a/eBook/10.8.md +++ b/eBook/10.8.md @@ -7,7 +7,11 @@ Go 开发者不需要写代码来释放程序中不再使用的变量和结构 如果想知道当前的内存状态,可以使用: ```go -fmt.Printf("%d\n", runtime.MemStats.Alloc/1024) +// fmt.Printf("%d\n", runtime.MemStats.Alloc/1024) +// 此处代码在 Go 1.5.1下不再有效,更正为 +var m runtime.MemStats +runtime.ReadMemStats(&m) +fmt.Printf("%d Kb\n", m.Alloc / 1024) ``` 上面的程序会给出已分配内存的总量,单位是 Kb。进一步的测量参考 [文档页面](http://golang.org/pkg/runtime/#MemStatsType)。 @@ -30,4 +34,4 @@ runtime.SetFinalizer(obj, func(obj *typeObj)) - [目录](directory.md) - 上一节:[类型的 String() 方法和格式化描述符](10.7.md) -- 下一章:[接口(Interfaces)与反射(reflection)](11.0.md) \ No newline at end of file +- 下一章:[接口(Interfaces)与反射(reflection)](11.0.md) From a0e6514a5f0eb0f0ed9db72763715026402bb2e2 Mon Sep 17 00:00:00 2001 From: skiy Date: Fri, 23 Oct 2015 17:57:16 +0800 Subject: [PATCH 04/12] Update 07.5.md MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 文字错误 --- eBook/07.5.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/eBook/07.5.md b/eBook/07.5.md index 5014788..92d9a19 100644 --- a/eBook/07.5.md +++ b/eBook/07.5.md @@ -44,7 +44,7 @@ func AppendByte(slice []byte, data ...byte) []byte { } ``` -`func copy(dst, src []T) int` copy 方法将类型为 T 的切片从源地址 src 拷贝到目标地址 dst,覆盖 dst 的相关元素,并且返回拷贝的元素个数。源地址和目标地址可能会有重叠。拷贝个数是 src 和 dst 的长度最小值。如果 src 是字符串那么元素类型就是 byte。如果你还想继续使用 src,在拷贝技术后执行 `src = dst`。 +`func copy(dst, src []T) int` copy 方法将类型为 T 的切片从源地址 src 拷贝到目标地址 dst,覆盖 dst 的相关元素,并且返回拷贝的元素个数。源地址和目标地址可能会有重叠。拷贝个数是 src 和 dst 的长度最小值。如果 src 是字符串那么元素类型就是 byte。如果你还想继续使用 src,在拷贝结束后执行 `src = dst`。 **练习 7.9** From aa3618337c5cb09b556b76a071beb13d5d7ce13a Mon Sep 17 00:00:00 2001 From: skiy Date: Sat, 24 Oct 2015 09:03:40 +0800 Subject: [PATCH 05/12] Update 07.2.md MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 文字错误 --- eBook/07.2.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/eBook/07.2.md b/eBook/07.2.md index c9c47c4..3ef315d 100644 --- a/eBook/07.2.md +++ b/eBook/07.2.md @@ -226,7 +226,7 @@ v := make([]int, 10, 50) 这样分配一个有 50 个 int 值的数组,并且创建了一个长度为 10,容量为 50 的 切片 v,该 切片 指向数组的前 10 个元素。 **问题 7.3** 给定 `s := make([]byte, 5)`,len(s) 和 cap(s) 分别是多少?`s = s[2:4]`,len(s) 和 cap(s) 又分别是多少? -**问题 7.4** 假设 `s1 := []byte{'p', 'o', 'e', 'm'}` 且 `s2 := s1[2:]`,s2 的值是多少?如果我们执行 `s2[1] = 't'`,s1 和 s2 现在的值又分配是多少? +**问题 7.4** 假设 `s1 := []byte{'p', 'o', 'e', 'm'}` 且 `s2 := s1[2:]`,s2 的值是多少?如果我们执行 `s2[1] = 't'`,s1 和 s2 现在的值又分别是多少? ## 7.2.5 多维 切片 From 601cecff66aface9a772cc04ef5092effd8f4889 Mon Sep 17 00:00:00 2001 From: skiy Date: Sat, 24 Oct 2015 17:55:48 +0800 Subject: [PATCH 06/12] update --- eBook/08.1.md | 2 +- eBook/08.5.md | 2 +- eBook/08.6.md | 1 - 3 files changed, 2 insertions(+), 3 deletions(-) diff --git a/eBook/08.1.md b/eBook/08.1.md index e860492..edcc530 100644 --- a/eBook/08.1.md +++ b/eBook/08.1.md @@ -27,7 +27,7 @@ map 也可以用函数作为自己的值,这样就可以用来做分支结构 key1 对应的值可以通过赋值符号来设置为 `val1:map1[key1] = val1`。 -令 `v: = map1[key1]` 可以将 key1 对应的值赋值为 v;如果 map 中没有 key1 存在,那么 v 将被赋值为 map1 的值类型的空值。 +令 `v := map1[key1]` 可以将 key1 对应的值赋值为 v;如果 map 中没有 key1 存在,那么 v 将被赋值为 map1 的值类型的空值。 常用的 `len(map1)` 方法可以获得 map 中的 pair 数目,这个数目是可以伸缩的,因为 map-pairs 在运行时可以动态添加和删除。 diff --git a/eBook/08.5.md b/eBook/08.5.md index d404b1d..495345a 100644 --- a/eBook/08.5.md +++ b/eBook/08.5.md @@ -53,7 +53,7 @@ func main() { 但是如果你想要一个排序的列表你最好使用结构体切片,这样会更有效: ```go -type struct { +type name struct { key string value int } diff --git a/eBook/08.6.md b/eBook/08.6.md index 004c618..3daef1b 100644 --- a/eBook/08.6.md +++ b/eBook/08.6.md @@ -26,7 +26,6 @@ func main() { for k, v := range invMap { fmt.Printf("Key: %v, Value: %v / ", k, v) } - fmt.Println() } ``` From 71ca3284358eae24c7a5dafd1a63a9bcb2fabe62 Mon Sep 17 00:00:00 2001 From: ArkBriar Date: Fri, 23 Oct 2015 22:39:35 +0800 Subject: [PATCH 07/12] Add chapter 11.11: Printf and reflection chmod -x 09.5.md 09.8.md --- eBook/09.5.md | 0 eBook/09.8.md | 0 eBook/11.11.md | 68 ++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 68 insertions(+) mode change 100755 => 100644 eBook/09.5.md mode change 100755 => 100644 eBook/09.8.md create mode 100644 eBook/11.11.md diff --git a/eBook/09.5.md b/eBook/09.5.md old mode 100755 new mode 100644 diff --git a/eBook/09.8.md b/eBook/09.8.md old mode 100755 new mode 100644 diff --git a/eBook/11.11.md b/eBook/11.11.md new file mode 100644 index 0000000..c520718 --- /dev/null +++ b/eBook/11.11.md @@ -0,0 +1,68 @@ +# 11.11 Printf和反射 + +在Go语言的标准库中,前几节所述的反射的功能被大量地使用。举个例子,fmt包中的Printf(以及其他格式化输出函数)都会使用反射来分析它的`...`参数。 + +Printf的函数声明为: + +```go +func Printf(format string, args ... interface{}) (n int, err error) +``` + +Printf中的`...`参数为空接口类型。Printf使用反射包来解析这个参数列表。所以,Printf能够知道它每个参数的类型。因此格式化字符串中只有%d而没有%u和%ld,因为它知道这个参数是unsigned还是long。这也是为什么Print和Println在没有格式字符串的情况下还能如此漂亮地输出。 + +为了让大家更加具体地了解Printf中的反射,我们实现了一个简单的通用输出函数。其中使用了type-switch来推导参数类型,并根据类型来输出每个参数的值(这里用了10.7节中练习10.13的部分代码) + +示例 11.15 print.go: +```go +// print.go +package main + +import ( + "os" + "strconv" +) + +type Stringer interface { + String() string +} + +type Celsius float64 + +func (c Celsius) String() string { + return strconv.FormatFloat(float64(c),'f', 1, 64) + " °C" +} + +type Day int + +var dayName = []string{"Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"} + +func (day Day) String() string { + return dayName[day] +} + +func print(args ...interface{}) { + for i, arg := range args { + if i > 0 {os.Stdout.WriteString(" ")} + switch a := arg.(type) { // type switch + case Stringer: os.Stdout.WriteString(a.String()) + case int: os.Stdout.WriteString(strconv.Itoa(a)) + case string: os.Stdout.WriteString(a) + // more types + default: os.Stdout.WriteString("???") + } + } +} + +func main() { + print(Day(1), "was", Celsius(18.36)) // Tuesday was 18.4 °C +} +// Tuesday was 18.4 °C +``` + +在12.8节中我们将阐释fmt.Fprintf()是怎么运用同样的反射原则的。 + +## 链接 + +- [目录](directory.md) +- 上一节:[反射包](11.10.md) +- 下一节:[接口和动态类型](11.12.md) From 0263c90f147c58305e32cbadb15cf2f874be3938 Mon Sep 17 00:00:00 2001 From: Unknwon Date: Sat, 24 Oct 2015 13:28:31 -0400 Subject: [PATCH 08/12] fix file location --- README_gc.md | 17 +++++++++-------- 12.6.md => eBook/12.6.md | 0 2 files changed, 9 insertions(+), 8 deletions(-) rename 12.6.md => eBook/12.6.md (100%) diff --git a/README_gc.md b/README_gc.md index b25eaad..852ed87 100644 --- a/README_gc.md +++ b/README_gc.md @@ -9,13 +9,14 @@ 本书的主要译者是 [@无闻Unknwon](http://www.weibo.com/Obahua),是一名 Go 语言爱好者和传播者,目前是 [Go Walker](https://gowalker.org)、Gopm、[Gogs](http://gogs.io) 和 [Macaron](https://github.com/Unknwon/macaron) 创始人,极客学院签约讲师。 目前已由多位 Go 语言爱好者共同提交翻译内容,主要包括: - - [@zhanming](https://github.com/zhanming) - - themorecolor - - [@everyx](https://github.com/everyx) - - [@chidouhu](https://github.com/chidouhu) - - [@spawnris](https://github.com/spawnris) - - [@domainname](https://github.com/domainname) - - [@leisore](https://github.com/leisore) + +- [@zhanming](https://github.com/zhanming) +- themorecolor +- [@everyx](https://github.com/everyx) +- [@chidouhu](https://github.com/chidouhu) +- [@spawnris](https://github.com/spawnris) +- [@domainname](https://github.com/domainname) +- [@leisore](https://github.com/leisore) ## 适用人群 @@ -28,4 +29,4 @@ Golang 编程:245386165 |更新日期 |更新内容 |----------|------------------ -|2015-09-13|11.9 空接口 \ No newline at end of file +|2015-09-13|11.9 空接口 diff --git a/12.6.md b/eBook/12.6.md similarity index 100% rename from 12.6.md rename to eBook/12.6.md From 6e7aa462fced37e00a08bf0ba00e23b05d39e897 Mon Sep 17 00:00:00 2001 From: joezou Date: Sun, 25 Oct 2015 21:17:38 +0800 Subject: [PATCH 09/12] =?UTF-8?q?=E5=9B=A0=E8=AF=A5=E6=96=B9=E6=B3=95?= =?UTF-8?q?=E4=B8=BA=E5=86=85=E9=83=A8=E8=B0=83=E7=94=A8=E5=BB=BA=E8=AE=AE?= =?UTF-8?q?=E6=94=B9=E4=B8=BA=E7=A7=81=E6=9C=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- eBook/06.3.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/eBook/06.3.md b/eBook/06.3.md index 59510ef..dc1c37f 100644 --- a/eBook/06.3.md +++ b/eBook/06.3.md @@ -27,14 +27,14 @@ package main import "fmt" func main() { - x := Min(1, 3, 2, 0) + x := min(1, 3, 2, 0) fmt.Printf("The minimum is: %d\n", x) arr := []int{7,9,3,5,1} - x = Min(arr...) + x = min(arr...) fmt.Printf("The minimum in the array arr is: %d", x) } -func Min(a ...int) int { +func min(a ...int) int { if len(a)==0 { return 0 } From ccfefe99dc53e80ede462f120a4c9c1cae7d916e Mon Sep 17 00:00:00 2001 From: joezou Date: Sun, 25 Oct 2015 22:22:38 +0800 Subject: [PATCH 10/12] =?UTF-8?q?=E5=9B=A0=E8=AF=A5=E6=96=B9=E6=B3=95?= =?UTF-8?q?=E4=B8=BA=E5=86=85=E9=83=A8=E8=B0=83=E7=94=A8=E5=BB=BA=E8=AE=AE?= =?UTF-8?q?=E6=94=B9=E4=B8=BA=E7=A7=81=E6=9C=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- eBook/06.4.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/eBook/06.4.md b/eBook/06.4.md index 800f8a7..b0682cf 100644 --- a/eBook/06.4.md +++ b/eBook/06.4.md @@ -11,16 +11,16 @@ package main import "fmt" func main() { - Function1() + function1() } -func Function1() { +func function1() { fmt.Printf("In Function1 at the top\n") - defer Function2() + defer function2() fmt.Printf("In Function1 at the bottom!\n") } -func Function2() { +func function2() { fmt.Printf("Function2: Deferred until the end of the calling function!") } ``` From c3490309469945fcf2b9824f8ba45216f7a62362 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=97=A0=E9=97=BB?= Date: Sun, 25 Oct 2015 13:02:34 -0400 Subject: [PATCH 11/12] Update 04.5.md --- eBook/04.5.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/eBook/04.5.md b/eBook/04.5.md index 7abab04..20e6be2 100644 --- a/eBook/04.5.md +++ b/eBook/04.5.md @@ -474,7 +474,7 @@ var ch byte = 65 或 var ch byte = '\x41' (`\x` 总是紧跟着长度为 2 的 16 进制数) -另外一种可能的写法是 `\` 后面紧跟着长度为 3 的十进制数,例如:`\377`。 +另外一种可能的写法是 `\` 后面紧跟着长度为 3 的八进制数,例如:`\377`。 不过 Go 同样支持 Unicode(UTF-8),因此字符同样称为 Unicode 代码点或者 runes,并在内存中使用 int 来表示。在文档中,一般使用格式 U+hhhh 来表示,其中 h 表示一个 16 进制数。其实 `rune` 也是 Go 当中的一个类型,并且是 `int32` 的别名。 From 97c6a9f796d601dc4c89a76bf6fd18fb90682a97 Mon Sep 17 00:00:00 2001 From: joezou Date: Mon, 26 Oct 2015 22:13:10 +0800 Subject: [PATCH 12/12] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E5=AD=97=E7=AC=A6?= =?UTF-8?q?=E4=B8=B2=E9=94=99=E8=AF=AF=E6=96=B9=E6=B3=95=E5=90=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- eBook/06.4.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/eBook/06.4.md b/eBook/06.4.md index b0682cf..1235ed0 100644 --- a/eBook/06.4.md +++ b/eBook/06.4.md @@ -15,13 +15,13 @@ func main() { } func function1() { - fmt.Printf("In Function1 at the top\n") + fmt.Printf("In function1 at the top\n") defer function2() - fmt.Printf("In Function1 at the bottom!\n") + fmt.Printf("In function1 at the bottom!\n") } func function2() { - fmt.Printf("Function2: Deferred until the end of the calling function!") + fmt.Printf("function2: Deferred until the end of the calling function!") } ```