2015-12-09 07:57:17 +00:00
<!DOCTYPE HTML>
< html lang = "zh-tw" >
< head >
< meta charset = "UTF-8" >
< meta http-equiv = "X-UA-Compatible" content = "IE=edge" / >
< title > 併髮的退齣 | Go编程语言< / title >
< meta content = "text/html; charset=utf-8" http-equiv = "Content-Type" >
< meta name = "description" content = "" >
< meta name = "generator" content = "GitBook 2.5.2" >
< meta name = "HandheldFriendly" content = "true" / >
< meta name = "viewport" content = "width=device-width, initial-scale=1, user-scalable=no" >
< meta name = "apple-mobile-web-app-capable" content = "yes" >
< meta name = "apple-mobile-web-app-status-bar-style" content = "black" >
< link rel = "apple-touch-icon-precomposed" sizes = "152x152" href = "../gitbook/images/apple-touch-icon-precomposed-152.png" >
< link rel = "shortcut icon" href = "../gitbook/images/favicon.ico" type = "image/x-icon" >
< link rel = "stylesheet" href = "../gitbook/style.css" >
< link rel = "stylesheet" href = "../gitbook/plugins/gitbook-plugin-highlight/website.css" >
< link rel = "stylesheet" href = "../gitbook/plugins/gitbook-plugin-search/search.css" >
< link rel = "stylesheet" href = "../gitbook/plugins/gitbook-plugin-fontsettings/website.css" >
< link rel = "next" href = "../ch8/ch8-10.html" / >
< link rel = "prev" href = "../ch8/ch8-08.html" / >
< / head >
< body >
2015-12-14 04:08:47 +00:00
< div class = "book" data-level = "8.9" data-chapter-title = "併髮的退齣" data-filepath = "ch8/ch8-09.md" data-basepath = ".." data-revision = "Mon Dec 14 2015 11:30:54 GMT+0800 (中国标准时间)" >
2015-12-09 07:57:17 +00:00
< div class = "book-summary" >
< nav role = "navigation" >
< ul class = "summary" >
< li class = "chapter " data-level = "0" data-path = "index.html" >
< a href = "../index.html" >
< i class = "fa fa-check" > < / i >
前言
< / a >
< ul class = "articles" >
< li class = "chapter " data-level = "0.1" data-path = "ch0/ch0-01.html" >
< a href = "../ch0/ch0-01.html" >
< i class = "fa fa-check" > < / i >
< b > 0.1.< / b >
Go語言起源
< / a >
< / li >
< li class = "chapter " data-level = "0.2" data-path = "ch0/ch0-02.html" >
< a href = "../ch0/ch0-02.html" >
< i class = "fa fa-check" > < / i >
< b > 0.2.< / b >
Go語言項目
< / a >
< / li >
< li class = "chapter " data-level = "0.3" data-path = "ch0/ch0-03.html" >
< a href = "../ch0/ch0-03.html" >
< i class = "fa fa-check" > < / i >
< b > 0.3.< / b >
本書的組織
< / a >
< / li >
< li class = "chapter " data-level = "0.4" data-path = "ch0/ch0-04.html" >
< a href = "../ch0/ch0-04.html" >
< i class = "fa fa-check" > < / i >
< b > 0.4.< / b >
更多的信息
< / a >
< / li >
< li class = "chapter " data-level = "0.5" data-path = "ch0/ch0-05.html" >
< a href = "../ch0/ch0-05.html" >
< i class = "fa fa-check" > < / i >
< b > 0.5.< / b >
緻謝
< / a >
< / li >
< / ul >
< / li >
< li class = "chapter " data-level = "1" data-path = "ch1/ch1.html" >
< a href = "../ch1/ch1.html" >
< i class = "fa fa-check" > < / i >
< b > 1.< / b >
入門
< / a >
< ul class = "articles" >
< li class = "chapter " data-level = "1.1" data-path = "ch1/ch1-01.html" >
< a href = "../ch1/ch1-01.html" >
< i class = "fa fa-check" > < / i >
< b > 1.1.< / b >
Hello, World
< / a >
< / li >
< li class = "chapter " data-level = "1.2" data-path = "ch1/ch1-02.html" >
< a href = "../ch1/ch1-02.html" >
< i class = "fa fa-check" > < / i >
< b > 1.2.< / b >
命令行參數
< / a >
< / li >
< li class = "chapter " data-level = "1.3" data-path = "ch1/ch1-03.html" >
< a href = "../ch1/ch1-03.html" >
< i class = "fa fa-check" > < / i >
< b > 1.3.< / b >
査找重復的行
< / a >
< / li >
< li class = "chapter " data-level = "1.4" data-path = "ch1/ch1-04.html" >
< a href = "../ch1/ch1-04.html" >
< i class = "fa fa-check" > < / i >
< b > 1.4.< / b >
GIF動畫
< / a >
< / li >
< li class = "chapter " data-level = "1.5" data-path = "ch1/ch1-05.html" >
< a href = "../ch1/ch1-05.html" >
< i class = "fa fa-check" > < / i >
< b > 1.5.< / b >
穫取URL
< / a >
< / li >
< li class = "chapter " data-level = "1.6" data-path = "ch1/ch1-06.html" >
< a href = "../ch1/ch1-06.html" >
< i class = "fa fa-check" > < / i >
< b > 1.6.< / b >
併髮穫取多個URL
< / a >
< / li >
< li class = "chapter " data-level = "1.7" data-path = "ch1/ch1-07.html" >
< a href = "../ch1/ch1-07.html" >
< i class = "fa fa-check" > < / i >
< b > 1.7.< / b >
Web服務
< / a >
< / li >
< li class = "chapter " data-level = "1.8" data-path = "ch1/ch1-08.html" >
< a href = "../ch1/ch1-08.html" >
< i class = "fa fa-check" > < / i >
< b > 1.8.< / b >
本章要點
< / a >
< / li >
< / ul >
< / li >
< li class = "chapter " data-level = "2" data-path = "ch2/ch2.html" >
< a href = "../ch2/ch2.html" >
< i class = "fa fa-check" > < / i >
< b > 2.< / b >
程序結構
< / a >
< ul class = "articles" >
< li class = "chapter " data-level = "2.1" data-path = "ch2/ch2-01.html" >
< a href = "../ch2/ch2-01.html" >
< i class = "fa fa-check" > < / i >
< b > 2.1.< / b >
命名
< / a >
< / li >
< li class = "chapter " data-level = "2.2" data-path = "ch2/ch2-02.html" >
< a href = "../ch2/ch2-02.html" >
< i class = "fa fa-check" > < / i >
< b > 2.2.< / b >
聲明
< / a >
< / li >
< li class = "chapter " data-level = "2.3" data-path = "ch2/ch2-03.html" >
< a href = "../ch2/ch2-03.html" >
< i class = "fa fa-check" > < / i >
< b > 2.3.< / b >
變量
< / a >
< / li >
< li class = "chapter " data-level = "2.4" data-path = "ch2/ch2-04.html" >
< a href = "../ch2/ch2-04.html" >
< i class = "fa fa-check" > < / i >
< b > 2.4.< / b >
賦值
< / a >
< / li >
< li class = "chapter " data-level = "2.5" data-path = "ch2/ch2-05.html" >
< a href = "../ch2/ch2-05.html" >
< i class = "fa fa-check" > < / i >
< b > 2.5.< / b >
類型
< / a >
< / li >
< li class = "chapter " data-level = "2.6" data-path = "ch2/ch2-06.html" >
< a href = "../ch2/ch2-06.html" >
< i class = "fa fa-check" > < / i >
< b > 2.6.< / b >
包和文件
< / a >
< / li >
< li class = "chapter " data-level = "2.7" data-path = "ch2/ch2-07.html" >
< a href = "../ch2/ch2-07.html" >
< i class = "fa fa-check" > < / i >
< b > 2.7.< / b >
作用域
< / a >
< / li >
< / ul >
< / li >
< li class = "chapter " data-level = "3" data-path = "ch3/ch3.html" >
< a href = "../ch3/ch3.html" >
< i class = "fa fa-check" > < / i >
< b > 3.< / b >
基礎數據類型
< / a >
< ul class = "articles" >
< li class = "chapter " data-level = "3.1" data-path = "ch3/ch3-01.html" >
< a href = "../ch3/ch3-01.html" >
< i class = "fa fa-check" > < / i >
< b > 3.1.< / b >
整型
< / a >
< / li >
< li class = "chapter " data-level = "3.2" data-path = "ch3/ch3-02.html" >
< a href = "../ch3/ch3-02.html" >
< i class = "fa fa-check" > < / i >
< b > 3.2.< / b >
浮點數
< / a >
< / li >
< li class = "chapter " data-level = "3.3" data-path = "ch3/ch3-03.html" >
< a href = "../ch3/ch3-03.html" >
< i class = "fa fa-check" > < / i >
< b > 3.3.< / b >
復數
< / a >
< / li >
< li class = "chapter " data-level = "3.4" data-path = "ch3/ch3-04.html" >
< a href = "../ch3/ch3-04.html" >
< i class = "fa fa-check" > < / i >
< b > 3.4.< / b >
佈爾型
< / a >
< / li >
< li class = "chapter " data-level = "3.5" data-path = "ch3/ch3-05.html" >
< a href = "../ch3/ch3-05.html" >
< i class = "fa fa-check" > < / i >
< b > 3.5.< / b >
字符串
< / a >
< / li >
< li class = "chapter " data-level = "3.6" data-path = "ch3/ch3-06.html" >
< a href = "../ch3/ch3-06.html" >
< i class = "fa fa-check" > < / i >
< b > 3.6.< / b >
常量
< / a >
< / li >
< / ul >
< / li >
< li class = "chapter " data-level = "4" data-path = "ch4/ch4.html" >
< a href = "../ch4/ch4.html" >
< i class = "fa fa-check" > < / i >
< b > 4.< / b >
復閤數據類型
< / a >
< ul class = "articles" >
< li class = "chapter " data-level = "4.1" data-path = "ch4/ch4-01.html" >
< a href = "../ch4/ch4-01.html" >
< i class = "fa fa-check" > < / i >
< b > 4.1.< / b >
數組
< / a >
< / li >
< li class = "chapter " data-level = "4.2" data-path = "ch4/ch4-02.html" >
< a href = "../ch4/ch4-02.html" >
< i class = "fa fa-check" > < / i >
< b > 4.2.< / b >
切片
< / a >
< / li >
< li class = "chapter " data-level = "4.3" data-path = "ch4/ch4-03.html" >
< a href = "../ch4/ch4-03.html" >
< i class = "fa fa-check" > < / i >
< b > 4.3.< / b >
字典
< / a >
< / li >
< li class = "chapter " data-level = "4.4" data-path = "ch4/ch4-04.html" >
< a href = "../ch4/ch4-04.html" >
< i class = "fa fa-check" > < / i >
< b > 4.4.< / b >
結構體
< / a >
< / li >
< li class = "chapter " data-level = "4.5" data-path = "ch4/ch4-05.html" >
< a href = "../ch4/ch4-05.html" >
< i class = "fa fa-check" > < / i >
< b > 4.5.< / b >
JSON
< / a >
< / li >
< li class = "chapter " data-level = "4.6" data-path = "ch4/ch4-06.html" >
< a href = "../ch4/ch4-06.html" >
< i class = "fa fa-check" > < / i >
< b > 4.6.< / b >
文本和HTML模闆
< / a >
< / li >
< / ul >
< / li >
< li class = "chapter " data-level = "5" data-path = "ch5/ch5.html" >
< a href = "../ch5/ch5.html" >
< i class = "fa fa-check" > < / i >
< b > 5.< / b >
函數
< / a >
< ul class = "articles" >
< li class = "chapter " data-level = "5.1" data-path = "ch5/ch5-01.html" >
< a href = "../ch5/ch5-01.html" >
< i class = "fa fa-check" > < / i >
< b > 5.1.< / b >
函數聲明
< / a >
< / li >
< li class = "chapter " data-level = "5.2" data-path = "ch5/ch5-02.html" >
< a href = "../ch5/ch5-02.html" >
< i class = "fa fa-check" > < / i >
< b > 5.2.< / b >
遞歸
< / a >
< / li >
< li class = "chapter " data-level = "5.3" data-path = "ch5/ch5-03.html" >
< a href = "../ch5/ch5-03.html" >
< i class = "fa fa-check" > < / i >
< b > 5.3.< / b >
多返迴值
< / a >
< / li >
< li class = "chapter " data-level = "5.4" data-path = "ch5/ch5-04.html" >
< a href = "../ch5/ch5-04.html" >
< i class = "fa fa-check" > < / i >
< b > 5.4.< / b >
錯誤
< / a >
< / li >
< li class = "chapter " data-level = "5.5" data-path = "ch5/ch5-05.html" >
< a href = "../ch5/ch5-05.html" >
< i class = "fa fa-check" > < / i >
< b > 5.5.< / b >
函數值
< / a >
< / li >
< li class = "chapter " data-level = "5.6" data-path = "ch5/ch5-06.html" >
< a href = "../ch5/ch5-06.html" >
< i class = "fa fa-check" > < / i >
< b > 5.6.< / b >
匿名函數
< / a >
< / li >
< li class = "chapter " data-level = "5.7" data-path = "ch5/ch5-07.html" >
< a href = "../ch5/ch5-07.html" >
< i class = "fa fa-check" > < / i >
< b > 5.7.< / b >
可變參數
< / a >
< / li >
< li class = "chapter " data-level = "5.8" data-path = "ch5/ch5-08.html" >
< a href = "../ch5/ch5-08.html" >
< i class = "fa fa-check" > < / i >
< b > 5.8.< / b >
Deferred函數
< / a >
< / li >
< li class = "chapter " data-level = "5.9" data-path = "ch5/ch5-09.html" >
< a href = "../ch5/ch5-09.html" >
< i class = "fa fa-check" > < / i >
< b > 5.9.< / b >
Panic異常
< / a >
< / li >
< li class = "chapter " data-level = "5.10" data-path = "ch5/ch5-10.html" >
< a href = "../ch5/ch5-10.html" >
< i class = "fa fa-check" > < / i >
< b > 5.10.< / b >
Recover捕穫異常
< / a >
< / li >
< / ul >
< / li >
< li class = "chapter " data-level = "6" data-path = "ch6/ch6.html" >
< a href = "../ch6/ch6.html" >
< i class = "fa fa-check" > < / i >
< b > 6.< / b >
方法
< / a >
< ul class = "articles" >
< li class = "chapter " data-level = "6.1" data-path = "ch6/ch6-01.html" >
< a href = "../ch6/ch6-01.html" >
< i class = "fa fa-check" > < / i >
< b > 6.1.< / b >
方法聲明
< / a >
< / li >
< li class = "chapter " data-level = "6.2" data-path = "ch6/ch6-02.html" >
< a href = "../ch6/ch6-02.html" >
< i class = "fa fa-check" > < / i >
< b > 6.2.< / b >
基於指鍼對象的方法
< / a >
< / li >
< li class = "chapter " data-level = "6.3" data-path = "ch6/ch6-03.html" >
< a href = "../ch6/ch6-03.html" >
< i class = "fa fa-check" > < / i >
< b > 6.3.< / b >
通過嵌入結構體來擴展類型
< / a >
< / li >
< li class = "chapter " data-level = "6.4" data-path = "ch6/ch6-04.html" >
< a href = "../ch6/ch6-04.html" >
< i class = "fa fa-check" > < / i >
< b > 6.4.< / b >
方法值和方法錶達式
< / a >
< / li >
< li class = "chapter " data-level = "6.5" data-path = "ch6/ch6-05.html" >
< a href = "../ch6/ch6-05.html" >
< i class = "fa fa-check" > < / i >
< b > 6.5.< / b >
示例: Bit數組
< / a >
< / li >
< li class = "chapter " data-level = "6.6" data-path = "ch6/ch6-06.html" >
< a href = "../ch6/ch6-06.html" >
< i class = "fa fa-check" > < / i >
< b > 6.6.< / b >
封裝
< / a >
< / li >
< / ul >
< / li >
< li class = "chapter " data-level = "7" data-path = "ch7/ch7.html" >
< a href = "../ch7/ch7.html" >
< i class = "fa fa-check" > < / i >
< b > 7.< / b >
接口
< / a >
< ul class = "articles" >
< li class = "chapter " data-level = "7.1" data-path = "ch7/ch7-01.html" >
< a href = "../ch7/ch7-01.html" >
< i class = "fa fa-check" > < / i >
< b > 7.1.< / b >
接口是閤約
< / a >
< / li >
< li class = "chapter " data-level = "7.2" data-path = "ch7/ch7-02.html" >
< a href = "../ch7/ch7-02.html" >
< i class = "fa fa-check" > < / i >
< b > 7.2.< / b >
接口類型
< / a >
< / li >
< li class = "chapter " data-level = "7.3" data-path = "ch7/ch7-03.html" >
< a href = "../ch7/ch7-03.html" >
< i class = "fa fa-check" > < / i >
< b > 7.3.< / b >
實現接口的條件
< / a >
< / li >
< li class = "chapter " data-level = "7.4" data-path = "ch7/ch7-04.html" >
< a href = "../ch7/ch7-04.html" >
< i class = "fa fa-check" > < / i >
< b > 7.4.< / b >
flag.Value接口
< / a >
< / li >
< li class = "chapter " data-level = "7.5" data-path = "ch7/ch7-05.html" >
< a href = "../ch7/ch7-05.html" >
< i class = "fa fa-check" > < / i >
< b > 7.5.< / b >
接口值
< / a >
< / li >
< li class = "chapter " data-level = "7.6" data-path = "ch7/ch7-06.html" >
< a href = "../ch7/ch7-06.html" >
< i class = "fa fa-check" > < / i >
< b > 7.6.< / b >
sort.Interface接口
< / a >
< / li >
< li class = "chapter " data-level = "7.7" data-path = "ch7/ch7-07.html" >
< a href = "../ch7/ch7-07.html" >
< i class = "fa fa-check" > < / i >
< b > 7.7.< / b >
http.Handler接口
< / a >
< / li >
< li class = "chapter " data-level = "7.8" data-path = "ch7/ch7-08.html" >
< a href = "../ch7/ch7-08.html" >
< i class = "fa fa-check" > < / i >
< b > 7.8.< / b >
error接口
< / a >
< / li >
< li class = "chapter " data-level = "7.9" data-path = "ch7/ch7-09.html" >
< a href = "../ch7/ch7-09.html" >
< i class = "fa fa-check" > < / i >
< b > 7.9.< / b >
示例: 錶達式求值
< / a >
< / li >
< li class = "chapter " data-level = "7.10" data-path = "ch7/ch7-10.html" >
< a href = "../ch7/ch7-10.html" >
< i class = "fa fa-check" > < / i >
< b > 7.10.< / b >
類型斷言
< / a >
< / li >
< li class = "chapter " data-level = "7.11" data-path = "ch7/ch7-11.html" >
< a href = "../ch7/ch7-11.html" >
< i class = "fa fa-check" > < / i >
< b > 7.11.< / b >
基於類型斷言識彆錯誤類型
< / a >
< / li >
< li class = "chapter " data-level = "7.12" data-path = "ch7/ch7-12.html" >
< a href = "../ch7/ch7-12.html" >
< i class = "fa fa-check" > < / i >
< b > 7.12.< / b >
通過類型斷言査詢接口
< / a >
< / li >
< li class = "chapter " data-level = "7.13" data-path = "ch7/ch7-13.html" >
< a href = "../ch7/ch7-13.html" >
< i class = "fa fa-check" > < / i >
< b > 7.13.< / b >
類型分支
< / a >
< / li >
< li class = "chapter " data-level = "7.14" data-path = "ch7/ch7-14.html" >
< a href = "../ch7/ch7-14.html" >
< i class = "fa fa-check" > < / i >
< b > 7.14.< / b >
示例: 基於標記的XML解碼
< / a >
< / li >
< li class = "chapter " data-level = "7.15" data-path = "ch7/ch7-15.html" >
< a href = "../ch7/ch7-15.html" >
< i class = "fa fa-check" > < / i >
< b > 7.15.< / b >
補充幾點
< / a >
< / li >
< / ul >
< / li >
< li class = "chapter " data-level = "8" data-path = "ch8/ch8.html" >
< a href = "../ch8/ch8.html" >
< i class = "fa fa-check" > < / i >
< b > 8.< / b >
Goroutines和Channels
< / a >
< ul class = "articles" >
< li class = "chapter " data-level = "8.1" data-path = "ch8/ch8-01.html" >
< a href = "../ch8/ch8-01.html" >
< i class = "fa fa-check" > < / i >
< b > 8.1.< / b >
Goroutines
< / a >
< / li >
< li class = "chapter " data-level = "8.2" data-path = "ch8/ch8-02.html" >
< a href = "../ch8/ch8-02.html" >
< i class = "fa fa-check" > < / i >
< b > 8.2.< / b >
示例: 併髮的Clock服務
< / a >
< / li >
< li class = "chapter " data-level = "8.3" data-path = "ch8/ch8-03.html" >
< a href = "../ch8/ch8-03.html" >
< i class = "fa fa-check" > < / i >
< b > 8.3.< / b >
示例: 併髮的Echo服務
< / a >
< / li >
< li class = "chapter " data-level = "8.4" data-path = "ch8/ch8-04.html" >
< a href = "../ch8/ch8-04.html" >
< i class = "fa fa-check" > < / i >
< b > 8.4.< / b >
Channels
< / a >
< / li >
< li class = "chapter " data-level = "8.5" data-path = "ch8/ch8-05.html" >
< a href = "../ch8/ch8-05.html" >
< i class = "fa fa-check" > < / i >
< b > 8.5.< / b >
併行的循環
< / a >
< / li >
< li class = "chapter " data-level = "8.6" data-path = "ch8/ch8-06.html" >
< a href = "../ch8/ch8-06.html" >
< i class = "fa fa-check" > < / i >
< b > 8.6.< / b >
示例: 併髮的Web爬蟲
< / a >
< / li >
< li class = "chapter " data-level = "8.7" data-path = "ch8/ch8-07.html" >
< a href = "../ch8/ch8-07.html" >
< i class = "fa fa-check" > < / i >
< b > 8.7.< / b >
基於select的多路復用
< / a >
< / li >
< li class = "chapter " data-level = "8.8" data-path = "ch8/ch8-08.html" >
< a href = "../ch8/ch8-08.html" >
< i class = "fa fa-check" > < / i >
< b > 8.8.< / b >
示例: 併髮的字典遍歷
< / a >
< / li >
< li class = "chapter active" data-level = "8.9" data-path = "ch8/ch8-09.html" >
< a href = "../ch8/ch8-09.html" >
< i class = "fa fa-check" > < / i >
< b > 8.9.< / b >
併髮的退齣
< / a >
< / li >
< li class = "chapter " data-level = "8.10" data-path = "ch8/ch8-10.html" >
< a href = "../ch8/ch8-10.html" >
< i class = "fa fa-check" > < / i >
< b > 8.10.< / b >
示例: 聊天服務
< / a >
< / li >
< / ul >
< / li >
< li class = "chapter " data-level = "9" data-path = "ch9/ch9.html" >
< a href = "../ch9/ch9.html" >
< i class = "fa fa-check" > < / i >
< b > 9.< / b >
基於共享變量的併髮
< / a >
< ul class = "articles" >
< li class = "chapter " data-level = "9.1" data-path = "ch9/ch9-01.html" >
< a href = "../ch9/ch9-01.html" >
< i class = "fa fa-check" > < / i >
< b > 9.1.< / b >
競爭條件
< / a >
< / li >
< li class = "chapter " data-level = "9.2" data-path = "ch9/ch9-02.html" >
< a href = "../ch9/ch9-02.html" >
< i class = "fa fa-check" > < / i >
< b > 9.2.< / b >
sync.Mutex互斥鎖
< / a >
< / li >
< li class = "chapter " data-level = "9.3" data-path = "ch9/ch9-03.html" >
< a href = "../ch9/ch9-03.html" >
< i class = "fa fa-check" > < / i >
< b > 9.3.< / b >
sync.RWMutex讀寫鎖
< / a >
< / li >
< li class = "chapter " data-level = "9.4" data-path = "ch9/ch9-04.html" >
< a href = "../ch9/ch9-04.html" >
< i class = "fa fa-check" > < / i >
< b > 9.4.< / b >
內存衕步
< / a >
< / li >
< li class = "chapter " data-level = "9.5" data-path = "ch9/ch9-05.html" >
< a href = "../ch9/ch9-05.html" >
< i class = "fa fa-check" > < / i >
< b > 9.5.< / b >
sync.Once初始化
< / a >
< / li >
< li class = "chapter " data-level = "9.6" data-path = "ch9/ch9-06.html" >
< a href = "../ch9/ch9-06.html" >
< i class = "fa fa-check" > < / i >
< b > 9.6.< / b >
競爭條件檢測
< / a >
< / li >
< li class = "chapter " data-level = "9.7" data-path = "ch9/ch9-07.html" >
< a href = "../ch9/ch9-07.html" >
< i class = "fa fa-check" > < / i >
< b > 9.7.< / b >
示例: 併髮的非阻塞緩存
< / a >
< / li >
< li class = "chapter " data-level = "9.8" data-path = "ch9/ch9-08.html" >
< a href = "../ch9/ch9-08.html" >
< i class = "fa fa-check" > < / i >
< b > 9.8.< / b >
Goroutines和綫程
< / a >
< / li >
< / ul >
< / li >
< li class = "chapter " data-level = "10" data-path = "ch10/ch10.html" >
< a href = "../ch10/ch10.html" >
< i class = "fa fa-check" > < / i >
< b > 10.< / b >
包和工具
< / a >
< ul class = "articles" >
< li class = "chapter " data-level = "10.1" data-path = "ch10/ch10-01.html" >
< a href = "../ch10/ch10-01.html" >
< i class = "fa fa-check" > < / i >
< b > 10.1.< / b >
簡介
< / a >
< / li >
< li class = "chapter " data-level = "10.2" data-path = "ch10/ch10-02.html" >
< a href = "../ch10/ch10-02.html" >
< i class = "fa fa-check" > < / i >
< b > 10.2.< / b >
導入路徑
< / a >
< / li >
< li class = "chapter " data-level = "10.3" data-path = "ch10/ch10-03.html" >
< a href = "../ch10/ch10-03.html" >
< i class = "fa fa-check" > < / i >
< b > 10.3.< / b >
包聲明
< / a >
< / li >
< li class = "chapter " data-level = "10.4" data-path = "ch10/ch10-04.html" >
< a href = "../ch10/ch10-04.html" >
< i class = "fa fa-check" > < / i >
< b > 10.4.< / b >
導入聲明
< / a >
< / li >
< li class = "chapter " data-level = "10.5" data-path = "ch10/ch10-05.html" >
< a href = "../ch10/ch10-05.html" >
< i class = "fa fa-check" > < / i >
< b > 10.5.< / b >
匿名導入
< / a >
< / li >
< li class = "chapter " data-level = "10.6" data-path = "ch10/ch10-06.html" >
< a href = "../ch10/ch10-06.html" >
< i class = "fa fa-check" > < / i >
< b > 10.6.< / b >
包和命名
< / a >
< / li >
< li class = "chapter " data-level = "10.7" data-path = "ch10/ch10-07.html" >
< a href = "../ch10/ch10-07.html" >
< i class = "fa fa-check" > < / i >
< b > 10.7.< / b >
工具
< / a >
< / li >
< / ul >
< / li >
< li class = "chapter " data-level = "11" data-path = "ch11/ch11.html" >
< a href = "../ch11/ch11.html" >
< i class = "fa fa-check" > < / i >
< b > 11.< / b >
測試
< / a >
< ul class = "articles" >
< li class = "chapter " data-level = "11.1" data-path = "ch11/ch11-01.html" >
< a href = "../ch11/ch11-01.html" >
< i class = "fa fa-check" > < / i >
< b > 11.1.< / b >
go test
< / a >
< / li >
< li class = "chapter " data-level = "11.2" data-path = "ch11/ch11-02.html" >
< a href = "../ch11/ch11-02.html" >
< i class = "fa fa-check" > < / i >
< b > 11.2.< / b >
測試函數
< / a >
< / li >
< li class = "chapter " data-level = "11.3" data-path = "ch11/ch11-03.html" >
< a href = "../ch11/ch11-03.html" >
< i class = "fa fa-check" > < / i >
< b > 11.3.< / b >
測試覆蓋率
< / a >
< / li >
< li class = "chapter " data-level = "11.4" data-path = "ch11/ch11-04.html" >
< a href = "../ch11/ch11-04.html" >
< i class = "fa fa-check" > < / i >
< b > 11.4.< / b >
基準測試
< / a >
< / li >
< li class = "chapter " data-level = "11.5" data-path = "ch11/ch11-05.html" >
< a href = "../ch11/ch11-05.html" >
< i class = "fa fa-check" > < / i >
< b > 11.5.< / b >
剖析
< / a >
< / li >
< li class = "chapter " data-level = "11.6" data-path = "ch11/ch11-06.html" >
< a href = "../ch11/ch11-06.html" >
< i class = "fa fa-check" > < / i >
< b > 11.6.< / b >
示例函數
< / a >
< / li >
< / ul >
< / li >
< li class = "chapter " data-level = "12" data-path = "ch12/ch12.html" >
< a href = "../ch12/ch12.html" >
< i class = "fa fa-check" > < / i >
< b > 12.< / b >
反射
< / a >
< ul class = "articles" >
< li class = "chapter " data-level = "12.1" data-path = "ch12/ch12-01.html" >
< a href = "../ch12/ch12-01.html" >
< i class = "fa fa-check" > < / i >
< b > 12.1.< / b >
為何需要反射?
< / a >
< / li >
< li class = "chapter " data-level = "12.2" data-path = "ch12/ch12-02.html" >
< a href = "../ch12/ch12-02.html" >
< i class = "fa fa-check" > < / i >
< b > 12.2.< / b >
reflect.Type和reflect.Value
< / a >
< / li >
< li class = "chapter " data-level = "12.3" data-path = "ch12/ch12-03.html" >
< a href = "../ch12/ch12-03.html" >
< i class = "fa fa-check" > < / i >
< b > 12.3.< / b >
Display遞歸打印
< / a >
< / li >
< li class = "chapter " data-level = "12.4" data-path = "ch12/ch12-04.html" >
< a href = "../ch12/ch12-04.html" >
< i class = "fa fa-check" > < / i >
< b > 12.4.< / b >
示例: 編碼S錶達式
< / a >
< / li >
< li class = "chapter " data-level = "12.5" data-path = "ch12/ch12-05.html" >
< a href = "../ch12/ch12-05.html" >
< i class = "fa fa-check" > < / i >
< b > 12.5.< / b >
通過reflect.Value脩改值
< / a >
< / li >
< li class = "chapter " data-level = "12.6" data-path = "ch12/ch12-06.html" >
< a href = "../ch12/ch12-06.html" >
< i class = "fa fa-check" > < / i >
< b > 12.6.< / b >
示例: 解碼S錶達式
< / a >
< / li >
< li class = "chapter " data-level = "12.7" data-path = "ch12/ch12-07.html" >
< a href = "../ch12/ch12-07.html" >
< i class = "fa fa-check" > < / i >
< b > 12.7.< / b >
穫取結構體字段標識
< / a >
< / li >
< li class = "chapter " data-level = "12.8" data-path = "ch12/ch12-08.html" >
< a href = "../ch12/ch12-08.html" >
< i class = "fa fa-check" > < / i >
< b > 12.8.< / b >
顯示一個類型的方法集
< / a >
< / li >
< li class = "chapter " data-level = "12.9" data-path = "ch12/ch12-09.html" >
< a href = "../ch12/ch12-09.html" >
< i class = "fa fa-check" > < / i >
< b > 12.9.< / b >
幾點忠告
< / a >
< / li >
< / ul >
< / li >
< li class = "chapter " data-level = "13" data-path = "ch13/ch13.html" >
< a href = "../ch13/ch13.html" >
< i class = "fa fa-check" > < / i >
< b > 13.< / b >
底層編程
< / a >
< ul class = "articles" >
< li class = "chapter " data-level = "13.1" data-path = "ch13/ch13-01.html" >
< a href = "../ch13/ch13-01.html" >
< i class = "fa fa-check" > < / i >
< b > 13.1.< / b >
unsafe.Sizeof, Alignof 和 Offsetof
< / a >
< / li >
< li class = "chapter " data-level = "13.2" data-path = "ch13/ch13-02.html" >
< a href = "../ch13/ch13-02.html" >
< i class = "fa fa-check" > < / i >
< b > 13.2.< / b >
unsafe.Pointer
< / a >
< / li >
< li class = "chapter " data-level = "13.3" data-path = "ch13/ch13-03.html" >
< a href = "../ch13/ch13-03.html" >
< i class = "fa fa-check" > < / i >
< b > 13.3.< / b >
示例: 深度相等判斷
< / a >
< / li >
< li class = "chapter " data-level = "13.4" data-path = "ch13/ch13-04.html" >
< a href = "../ch13/ch13-04.html" >
< i class = "fa fa-check" > < / i >
< b > 13.4.< / b >
通過cgo調用C代碼
< / a >
< / li >
< li class = "chapter " data-level = "13.5" data-path = "ch13/ch13-05.html" >
< a href = "../ch13/ch13-05.html" >
< i class = "fa fa-check" > < / i >
< b > 13.5.< / b >
幾點忠告
< / a >
< / li >
< / ul >
< / li >
< li class = "chapter " data-level = "14" data-path = "exercise/ex.html" >
< a href = "../exercise/ex.html" >
< i class = "fa fa-check" > < / i >
< b > 14.< / b >
習題解答
< / a >
< ul class = "articles" >
< li class = "chapter " data-level = "14.1" data-path = "exercise/ex-ch1.html" >
< a href = "../exercise/ex-ch1.html" >
< i class = "fa fa-check" > < / i >
< b > 14.1.< / b >
第一章 入門
< / a >
< / li >
< / ul >
< / li >
< li class = "chapter " data-level = "15" data-path = "errata.html" >
< a href = "../errata.html" >
< i class = "fa fa-check" > < / i >
< b > 15.< / b >
勘誤
< / a >
< / li >
< li class = "divider" > < / li >
< li >
< a href = "https://www.gitbook.com" target = "blank" class = "gitbook-link" >
本書使用 GitBook 釋出
< / a >
< / li >
< / ul >
< / nav >
< / div >
< div class = "book-body" >
< div class = "body-inner" >
< div class = "book-header" role = "navigation" >
<!-- Actions Left -->
<!-- Title -->
< h1 >
< i class = "fa fa-circle-o-notch fa-spin" > < / i >
< a href = "../" > Go编程语言< / a >
< / h1 >
< / div >
< div class = "page-wrapper" tabindex = "-1" role = "main" >
< div class = "page-inner" >
< section class = "normal" id = "section-" >
< h2 id = "89-併髮的退齣" > 8.9. 併 髮 的 退 齣 < / h2 >
2015-12-14 04:08:47 +00:00
< p > 有 時 候 我 們 需 要 通 知 goroutine停 止 它 正 在 乾 的 事 情 , 比 如 一 箇 正 在 執 行 計 算 的 web服 務 , 然 而 它 的 客 戶 端 已 經 斷 開 了 和 服 務 端 的 連 接 。 < / p >
< p > Go語 言 併 沒 有 提 供 在 一 箇 goroutine中 終 止 另 一 箇 goroutine的 方 法 , 由 於 這 樣 會 導 緻 goroutine之 間 的 共 享 變 量 落 在 未 定 義 的 狀 態 上 。 在 8.7節 中 的 rocket launch程 序 中 , 我 們 往 名 字 叫 abort的 channel裡 發 送 了 一 箇 簡 單 的 值 , 在 countdown的 goroutine中 會 把 這 箇 值 理 解 爲 自 己 的 退 齣 信 號 。 但 是 如 果 我 們 想 要 退 齣 兩 箇 或 者 任 意 多 箇 goroutine怎 麼 辦 呢 ? < / p >
< p > 一 種 可 能 的 手 段 是 嚮 abort的 channel裡 發 送 和 goroutine數 目 一 樣 多 的 事 件 來 退 齣 它 們 。 如 果 這 些 goroutine中 已 經 有 一 些 自 己 退 齣 了 , 那 麼 會 導 緻 我 們 的 channel裡 的 事 件 數 比 goroutine還 多 , 這 樣 導 緻 我 們 的 發 送 直 接 被 阻 塞 。 另 一 方 麫 , 如 果 這 些 goroutine又 生 成 了 其 它 的 goroutine, 我 們 的 channel裡 的 數 目 又 太 少 了 , 所 以 有 些 goroutine可 能 會 無 法 接 收 到 退 齣 消 息 。 一 般 情 況 下 我 們 是 很 難 知 道 在 某 一 箇 時 刻 具 體 有 多 少 箇 goroutine在 運 行 着 的 。 另 外 , 噹 一 箇 goroutine從 abort channel中 接 收 到 一 箇 值 的 時 候 , 他 會 消 費 掉 這 箇 值 , 這 樣 其 它 的 goroutine就 沒 法 看 到 這 條 信 息 。 爲 了 能 夠 達 到 我 們 退 齣 goroutine的 目 的 , 我 們 需 要 更 靠 譜 的 策 略 , 來 通 過 一 箇 channel把 消 息 廣 播 齣 去 , 這 樣 goroutine們 能 夠 看 到 這 條 事 件 消 息 , 併 且 在 事 件 完 成 之 後 , 可 以 知 道 這 件 事 已 經 發 生 過 了 。 < / p >
< p > 迴 憶 一 下 我 們 關 閉 了 一 箇 channel併 且 被 消 費 掉 了 所 有 已 發 送 的 值 , 操 作 channel之 後 的 代 碼 可 以 立 卽 被 執 行 , 併 且 會 產 生 零 值 。 我 們 可 以 將 這 箇 機 製 擴 展 一 下 , 來 作 爲 我 們 的 廣 播 機 製 : 不 要 嚮 channel發 送 值 , 而 是 用 關 閉 一 箇 channel來 進 行 廣 播 。 < / p >
< p > 隻 要 一 些 小 脩 改 , 我 們 就 可 以 把 退 齣 邏 輯 加 入 到 前 一 節 的 du程 序 。 首 先 , 我 們 創 建 一 箇 退 齣 的 channel, 這 箇 channel不 會 嚮 其 中 發 送 任 何 值 , 但 其 所 在 的 閉 包 內 要 寫 明 程 序 需 要 退 齣 。 我 們 衕 時 還 定 義 了 一 箇 工 具 函 數 , cancelled, 這 箇 函 數 在 被 調 用 的 時 候 會 輪 詢 退 齣 狀 態 。 < / p >
< pre > < code class = "lang-go" > gopl.io/ch8/du4
< span class = "hljs-keyword" > var< / span > done = < span class = "hljs-built_in" > make< / span > (< span class = "hljs-keyword" > chan< / span > < span class = "hljs-keyword" > struct< / span > {})
< span class = "hljs-keyword" > func< / span > cancelled() < span class = "hljs-typename" > bool< / span > {
< span class = "hljs-keyword" > select< / span > {
< span class = "hljs-keyword" > case< / span > < -done:
< span class = "hljs-keyword" > return< / span > < span class = "hljs-constant" > true< / span >
< span class = "hljs-keyword" > default< / span > :
< span class = "hljs-keyword" > return< / span > < span class = "hljs-constant" > false< / span >
}
}
< / code > < / pre >
< p > 下 麫 我 們 創 建 一 箇 從 標 準 輸 入 流 中 讀 取 內 容 的 goroutine, 這 是 一 箇 比 較 典 型 的 連 接 到 終 端 的 程 序 。 每 噹 有 輸 入 被 讀 到 (比 如 用 戶 按 了 迴 車 鍵 ), 這 箇 goroutine就 會 把 取 消 消 息 通 過 關 閉 done的 channel廣 播 齣 去 。 < / p >
< pre > < code class = "lang-go" > < span class = "hljs-comment" > // Cancel traversal when input is detected.< / span >
< span class = "hljs-keyword" > go< / span > < span class = "hljs-keyword" > func< / span > () {
os.Stdin.Read(< span class = "hljs-built_in" > make< / span > ([]< span class = "hljs-typename" > byte< / span > , < span class = "hljs-number" > 1< / span > )) < span class = "hljs-comment" > // read a single byte< / span >
< span class = "hljs-built_in" > close< / span > (done)
}()
< / code > < / pre >
< p > 現 在 我 們 需 要 使 我 們 的 goroutine來 對 取 消 進 行 響 應 。 在 main goroutine中 , 我 們 添 加 了 select的 第 三 箇 case語 句 , 嘗 試 從 done channel中 接 收 內 容 。 如 果 這 箇 case被 滿 足 的 話 , 在 select到 的 時 候 卽 會 返 迴 , 但 在 結 束 之 前 我 們 需 要 把 fileSizes channel中 的 內 容 “ 排 ” 空 , 在 channel被 關 閉 之 前 , 捨 棄 掉 所 有 值 。 這 樣 可 以 保 証 對 walkDir的 調 用 不 要 被 嚮 fileSizes發 送 信 息 阻 塞 住 , 可 以 正 確 地 完 成 。 < / p >
< pre > < code class = "lang-go" > < span class = "hljs-keyword" > for< / span > {
< span class = "hljs-keyword" > select< / span > {
< span class = "hljs-keyword" > case< / span > < -done:
< span class = "hljs-comment" > // Drain fileSizes to allow existing goroutines to finish.< / span >
< span class = "hljs-keyword" > for< / span > < span class = "hljs-keyword" > range< / span > fileSizes {
< span class = "hljs-comment" > // Do nothing.< / span >
}
< span class = "hljs-keyword" > return< / span >
< span class = "hljs-keyword" > case< / span > size, ok := < -fileSizes:
< span class = "hljs-comment" > // ...< / span >
}
}
< / code > < / pre >
< p > walkDir這 箇 goroutine一 啟 動 就 會 輪 詢 取 消 狀 態 , 如 果 取 消 狀 態 被 設 置 的 話 會 直 接 返 迴 , 併 且 不 做 額 外 的 事 情 。 這 樣 我 們 將 所 有 在 取 消 事 件 之 後 創 建 的 goroutine改 變 爲 無 操 作 。 < / p >
< pre > < code class = "lang-go" > < span class = "hljs-keyword" > func< / span > walkDir(dir < span class = "hljs-typename" > string< / span > , n *sync.WaitGroup, fileSizes < span class = "hljs-keyword" > chan< / span > < - < span class = "hljs-typename" > int64< / span > ) {
< span class = "hljs-keyword" > defer< / span > n.Done()
< span class = "hljs-keyword" > if< / span > cancelled() {
< span class = "hljs-keyword" > return< / span >
}
< span class = "hljs-keyword" > for< / span > _, entry := < span class = "hljs-keyword" > range< / span > dirents(dir) {
< span class = "hljs-comment" > // ...< / span >
}
}
< / code > < / pre >
< p > 在 walkDir函 數 的 循 環 中 我 們 對 取 消 狀 態 進 行 輪 詢 可 以 帶 來 明 顯 的 益 處 , 可 以 避 免 在 取 消 事 件 發 生 時 還 去 創 建 goroutine。 取 消 本 身 是 有 一 些 代 價 的 ; 想 要 快 速 的 響 應 需 要 對 程 序 邏 輯 進 行 侵 入 式 的 脩 改 。 確 保 在 取 消 發 生 之 後 不 要 有 代 價 太 大 的 操 作 可 能 會 需 要 脩 改 你 代 碼 裡 的 很 多 地 方 , 但 是 在 一 些 重 要 的 地 方 去 檢 査 取 消 事 件 也 確 實 能 帶 來 很 大 的 好 處 。 < / p >
< p > 對 這 箇 程 序 的 一 箇 簡 單 的 性 能 分 析 可 以 揭 示 瓶 頸 在 dirents函 數 中 穫 取 一 箇 信 號 量 。 下 麫 的 select可 以 讓 這 種 操 作 可 以 被 取 消 , 併 且 可 以 將 取 消 時 的 延 遲 從 幾 百 毫 秒 降 低 到 幾 十 毫 秒 。 < / p >
< pre > < code class = "lang-go" > < span class = "hljs-keyword" > func< / span > dirents(dir < span class = "hljs-typename" > string< / span > ) []os.FileInfo {
< span class = "hljs-keyword" > select< / span > {
< span class = "hljs-keyword" > case< / span > sema < - < span class = "hljs-keyword" > struct< / span > {}{}: < span class = "hljs-comment" > // acquire token< / span >
< span class = "hljs-keyword" > case< / span > < -done:
< span class = "hljs-keyword" > return< / span > < span class = "hljs-constant" > nil< / span > < span class = "hljs-comment" > // cancelled< / span >
}
< span class = "hljs-keyword" > defer< / span > < span class = "hljs-keyword" > func< / span > () { < -sema }() < span class = "hljs-comment" > // release token< / span >
< span class = "hljs-comment" > // ...read directory...< / span >
}
< / code > < / pre >
< p > 現 在 噹 取 消 發 生 時 , 所 有 後 檯 的 goroutine都 會 迅 速 停 止 併 且 主 函 數 會 返 迴 。 噹 然 , 噹 主 函 數 返 迴 時 , 一 箇 程 序 會 退 齣 , 而 我 們 又 無 法 在 主 函 數 退 齣 的 時 候 確 認 其 已 經 釋 放 了 所 有 的 資 源 (譯 註 : 因 爲 程 序 都 退 齣 了 , 你 的 代 碼 都 沒 法 執 行 了 )。 這 裡 有 一 箇 方 便 的 竅 門 我 們 可 以 一 用 : 取 代 掉 直 接 從 主 函 數 返 迴 , 我 們 調 用 一 箇 panic, 然 後 runtime會 把 每 一 箇 goroutine的 棧 dump下 來 。 如 果 main goroutine是 唯 一 一 箇 剩 下 的 goroutine的 話 , 他 會 清 理 掉 自 己 的 一 切 資 源 。 但 是 如 果 還 有 其 它 的 goroutine沒 有 退 齣 , 他 們 可 能 沒 辦 法 被 正 確 地 取 消 掉 , 也 有 可 能 被 取 消 但 是 取 消 操 作 會 很 花 時 間 ; 所 以 這 裡 的 一 箇 調 研 還 是 很 有 必 要 的 。 我 們 用 panic來 穫 取 到 足 夠 的 信 息 來 驗 証 我 們 上 麫 的 判 斷 , 看 看 最 終 到 底 是 什 麼 樣 的 情 況 。 < / p >
< p > 練 習 8.10: HTTP請 求 可 能 會 因 http.Request結 構 體 中 Cancel channel的 關 閉 而 取 消 。 脩 改 8.6節 中 的 web crawler來 支 持 取 消 http請 求 。 < / p >
< p > 提 示 : http.Get併 沒 有 提 供 方 便 地 定 製 一 箇 請 求 的 方 法 。 你 可 以 用 http.NewRequest來 取 而 代 之 , 設 置 它 的 Cancel字 段 , 然 後 用 http.DefaultClient.Do(req)來 進 行 這 箇 http請 求 。 < / p >
< p > 練 習 8.11:緊 接 着 8.4.4中 的 mirroredQuery流 程 , 實 現 一 箇 併 發 請 求 url的 fetch的 變 種 。 噹 第 一 箇 請 求 返 迴 時 , 直 接 取 消 其 它 的 請 求 。 < / p >
2015-12-09 07:57:17 +00:00
< / section >
< / div >
< / div >
< / div >
< a href = "../ch8/ch8-08.html" class = "navigation navigation-prev " aria-label = "Previous page: 示例: 併髮的字典遍歷" > < i class = "fa fa-angle-left" > < / i > < / a >
< a href = "../ch8/ch8-10.html" class = "navigation navigation-next " aria-label = "Next page: 示例: 聊天服務" > < i class = "fa fa-angle-right" > < / i > < / a >
< / div >
< / div >
< script src = "../gitbook/app.js" > < / script >
< script src = "../gitbook/plugins/gitbook-plugin-search/lunr.min.js" > < / script >
< script src = "../gitbook/plugins/gitbook-plugin-search/search.js" > < / script >
< script src = "../gitbook/plugins/gitbook-plugin-sharing/buttons.js" > < / script >
< script src = "../gitbook/plugins/gitbook-plugin-fontsettings/buttons.js" > < / script >
< script >
require(["gitbook"], function(gitbook) {
var config = {"highlight":{},"search":{},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"fontsettings":{"theme":"white","family":"sans","size":2}};
gitbook.start(config);
});
< / script >
< / body >
< / html >