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
This commit is contained in:
songleo
2016-01-03 14:29:25 +08:00
parent 34534d240c
commit 0aefb4a032
6 changed files with 52 additions and 61 deletions

View File

@@ -33,9 +33,11 @@ for ix, ch := range str {
如何获取一个字符串的字符数:
最快速:
```go
utf8.RuneCountInString(str)
```
`len([]int(str)) //TBD`
5如何连接字符串

View File

@@ -26,5 +26,5 @@
## 链接
- [目录](directory.md)
- 上一[运算符模板和接口](17.4.md)
- 下一[字符串](18.1.md)
- 上一[其他](18.10.md)
- 下一[构建一个完整的应用程序](19.0.md)

View File

@@ -1,13 +1,10 @@
# 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如何截断数组或者切片的最后一个元素
@@ -25,7 +22,7 @@ for ix, value := range arr {
}
```
3如何在一个二维数组或者切片arr2Dim中查找一个指定值V
3如何在一个二维数组或者切片`arr2Dim`中查找一个指定值`V`
```go
found := false

View File

@@ -1,7 +1,6 @@
# 18.4 结构体
创建:
```go
type struct1 struct {
field1 type1
@@ -12,7 +11,6 @@ ms := new(struct1)
```
初始化:
```go
ms := &struct1{10, 15.5, "Chris"}
```

View File

@@ -5,19 +5,17 @@
实践经验表明,如果你使用并行性获得高于串行运算的效率:在协程内部已经完成的大部分工作,其开销比创建协程和协程间通信还高。
1 出于出于性能考虑建议使用带缓存的通道:
使用带缓存的通道很轻易成倍提高它的吞吐量某些场景其性能可以提高至10倍甚至更多。通过调整通道的容量你可以尝试着更进一步的优化其的性能。
2 限制一个通道的数据数量并将它们封装在成一个数组:
如果使用通道传递大量单独的数据那么通道将变成你的性能瓶颈。然而当将数据块打包封装成数组在接收端解压数据时性能可以提高至10倍。
创建:`ch := make(chan type, buf)`
1如何使用`for`或者`for-range`遍历一个通道:
```go
for v := range ch {
// do something with v
@@ -26,7 +24,6 @@ for v := range ch {
2如何检测一个通道`ch`是否是关闭的:
```go
//read channel until it closes or error-condition
for {
@@ -41,6 +38,7 @@ for {
3如何通过一个通道让主程序等待直到协程完成
(信号量模式):
```go
ch := make(chan int) // Allocate a channel.
// Start something in a goroutine; when it completes, signal on the channel.
@@ -52,10 +50,8 @@ doSomethingElseForAWhile()
<-ch // Wait for goroutine to finish; discard sent value.
```
如果希望程序必须一直阻塞,在匿名函数中省略 `ch <- 1`即可。
4通道的工厂模板下面的函数是一个通道工厂启动一个匿名函数作为协程以生产通道
```go
@@ -76,10 +72,8 @@ func pump() chan int {
7如何在多核CPU上实现并行计算参考14.13小节
(8)如何停止一个协程:`runtime.Goexit()`
9简单的超时模板
```go