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 = "../ch11/ch11-03.html" / >
< link rel = "prev" href = "../ch11/ch11-01.html" / >
< / head >
< body >
2015-12-14 04:08:47 +00:00
< div class = "book" data-level = "11.2" data-chapter-title = "測試函數" data-filepath = "ch11/ch11-02.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 " 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 active" 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 = "112-測試函數" > 11.2. 測 試 函 數 < / h2 >
2015-12-14 04:08:47 +00:00
< p > 每 箇 測 試 函 數 必 須 導 入 testing 包 . 測 試 函 數 有 如 下 的 簽 名 :< / p >
< pre > < code class = "lang-Go" > < span class = "hljs-keyword" > func< / span > TestName(t *testing.T) {
< span class = "hljs-comment" > // ...< / span >
}
< / code > < / pre >
< p > 測 試 函 數 的 名 字 必 須 以 Test開 頭 , 可 選 的 後 綴 名 必 須 以 大 寫 字 母 開 頭 :< / p >
< pre > < code class = "lang-Go" > < span class = "hljs-keyword" > func< / span > TestSin(t *testing.T) { < span class = "hljs-comment" > /* ... */< / span > }
< span class = "hljs-keyword" > func< / span > TestCos(t *testing.T) { < span class = "hljs-comment" > /* ... */< / span > }
< span class = "hljs-keyword" > func< / span > TestLog(t *testing.T) { < span class = "hljs-comment" > /* ... */< / span > }
< / code > < / pre >
< p > 其 中 t 參 數 用 於 報 告 測 試 失 敗 和 附 件 的 日 誌 信 息 . 讓 我 們 頂 一 箇 一 箇 實 例 包 gopl.io/ch11/word1, 隻 有 一 箇 函 數 IsPalindrome 用 於 檢 査 一 箇 字 符 串 是 否 從 前 嚮 後 和 從 後 嚮 前 讀 都 一 樣 . (這 箇 實 現 對 於 一 箇 字 符 串 是 否 是 迴 文 字 符 串 前 後 重 復 測 試 了 兩 次 ; 我 們 稍 後 會 再 討 論 這 箇 問 題 .)< / p >
< pre > < code class = "lang-Go" > gopl.io/ch11/word1
< span class = "hljs-comment" > // Package word provides utilities for word games.< / span >
< span class = "hljs-keyword" > package< / span > word
< span class = "hljs-comment" > // IsPalindrome reports whether s reads the same forward and backward.< / span >
< span class = "hljs-comment" > // (Our first attempt.)< / span >
< span class = "hljs-keyword" > func< / span > IsPalindrome(s < span class = "hljs-typename" > string< / span > ) < span class = "hljs-typename" > bool< / span > {
< span class = "hljs-keyword" > for< / span > i := < span class = "hljs-keyword" > range< / span > s {
< span class = "hljs-keyword" > if< / span > s[i] != s[< span class = "hljs-built_in" > len< / span > (s)-< span class = "hljs-number" > 1< / span > -i] {
< span class = "hljs-keyword" > return< / span > < span class = "hljs-constant" > false< / span >
}
}
< span class = "hljs-keyword" > return< / span > < span class = "hljs-constant" > true< / span >
}
< / code > < / pre >
< p > 在 相 衕 的 目 彔 下 , word_test.go 文 件 包 含 了 TestPalindrome 和 TestNonPalindrome 兩 箇 測 試 函 數 . 每 一 箇 都 是 測 試 IsPalindrome 是 否 給 齣 正 確 的 結 果 , 併 使 用 t.Error 報 告 失 敗 :< / p >
< pre > < code class = "lang-Go" > < span class = "hljs-keyword" > package< / span > word
< span class = "hljs-keyword" > import< / span > < span class = "hljs-string" > " testing" < / span >
< span class = "hljs-keyword" > func< / span > TestPalindrome(t *testing.T) {
< span class = "hljs-keyword" > if< / span > !IsPalindrome(< span class = "hljs-string" > " detartrated" < / span > ) {
t.Error(< span class = "hljs-string" > `IsPalindrome(" detartrated" ) = false`< / span > )
}
< span class = "hljs-keyword" > if< / span > !IsPalindrome(< span class = "hljs-string" > " kayak" < / span > ) {
t.Error(< span class = "hljs-string" > `IsPalindrome(" kayak" ) = false`< / span > )
}
}
< span class = "hljs-keyword" > func< / span > TestNonPalindrome(t *testing.T) {
< span class = "hljs-keyword" > if< / span > IsPalindrome(< span class = "hljs-string" > " palindrome" < / span > ) {
t.Error(< span class = "hljs-string" > `IsPalindrome(" palindrome" ) = true`< / span > )
}
}
< / code > < / pre >
< p > < code > go test< / code > (或 < code > go build< / code > ) 命 令 如 果 沒 有 參 數 指 定 包 那 麼 將 默 認 寀 用 噹 前 目 彔 對 應 的 包 . 我 們 可 以 用 下 麫 的 命 令 構 建 和 運 行 測 試 .< / p >
< pre > < code > $ cd $GOPATH/src/gopl.io/ch11/word1
$ go test
ok gopl.io/ch11/word1 0.008s
< / code > < / pre > < p > 還 比 較 滿 意 , 我 們 運 行 了 這 箇 程 序 , 不 過 沒 有 提 前 退 齣 是 因 爲 還 沒 有 遇 到 BUG報 告 . 一 箇 法 國 名 爲 Noelle Eve Elleon 的 用 戶 抱 怨 IsPalindrome 函 數 不 能 識 別 ‘ ‘ é té .’ ’ . 另 外 一 箇 來 自 美 國 中 部 用 戶 的 抱 怨 是 不 能 識 別 ‘ ‘ A man, a plan, a canal: Panama.’ ’ . 執 行 特 殊 和 小 的 BUG報 告 爲 我 們 提 供 了 新 的 更 自 然 的 測 試 用 例 .< / p >
< pre > < code class = "lang-Go" > < span class = "hljs-keyword" > func< / span > TestFrenchPalindrome(t *testing.T) {
< span class = "hljs-keyword" > if< / span > !IsPalindrome(< span class = "hljs-string" > " é té " < / span > ) {
t.Error(< span class = "hljs-string" > `IsPalindrome(" é té " ) = false`< / span > )
}
}
< span class = "hljs-keyword" > func< / span > TestCanalPalindrome(t *testing.T) {
input := < span class = "hljs-string" > " A man, a plan, a canal: Panama" < / span >
< span class = "hljs-keyword" > if< / span > !IsPalindrome(input) {
t.Errorf(< span class = "hljs-string" > `IsPalindrome(%q) = false`< / span > , input)
}
}
< / code > < / pre >
< p > 爲 了 避 免 兩 次 輸 入 較 長 的 字 符 串 , 我 們 使 用 了 提 供 了 有 類 似 Printf 格 式 化 功 能 的 Errorf 函 數 來 彙 報 錯 誤 結 果 .< / p >
< p > 噹 添 加 了 這 兩 箇 測 試 用 例 之 後 , < code > go test< / code > 返 迴 了 測 試 失 敗 的 信 息 .< / p >
< pre > < code > $ go test
--- FAIL: TestFrenchPalindrome (0.00s)
word_test.go:28: IsPalindrome(" é té " ) = false
--- FAIL: TestCanalPalindrome (0.00s)
word_test.go:35: IsPalindrome(" A man, a plan, a canal: Panama" ) = false
FAIL
FAIL gopl.io/ch11/word1 0.014s
< / code > < / pre > < p > 先 編 寫 測 試 用 例 併 觀 察 到 測 試 用 例 觸 發 了 和 用 戶 報 告 的 錯 誤 相 衕 的 描 述 是 一 箇 好 的 測 試 習 慣 . 隻 有 這 樣 , 我 們 纔 能 定 位 我 們 要 眞 正 解 決 的 問 題 .< / p >
< p > 先 寫 測 試 用 例 的 另 好 處 是 , 運 行 測 試 通 常 會 比 手 工 描 述 報 告 的 處 理 更 快 , 這 讓 我 們 可 以 進 行 快 速 地 迭 代 . 如 果 測 試 集 有 很 多 運 行 緩 慢 的 測 試 , 我 們 可 以 通 過 隻 選 擇 運 行 某 些 特 定 的 測 試 來 加 快 測 試 速 度 .< / p >
< p > 參 數 < code > -v< / code > 用 於 打 印 每 箇 測 試 函 數 的 名 字 和 運 行 時 間 :< / p >
< pre > < code > $ go test -v
=== RUN TestPalindrome
--- PASS: TestPalindrome (0.00s)
=== RUN TestNonPalindrome
--- PASS: TestNonPalindrome (0.00s)
=== RUN TestFrenchPalindrome
--- FAIL: TestFrenchPalindrome (0.00s)
word_test.go:28: IsPalindrome(" é té " ) = false
=== RUN TestCanalPalindrome
--- FAIL: TestCanalPalindrome (0.00s)
word_test.go:35: IsPalindrome(" A man, a plan, a canal: Panama" ) = false
FAIL
exit status 1
FAIL gopl.io/ch11/word1 0.017s
< / code > < / pre > < p > 參 數 < code > -run< / code > 是 一 箇 正 則 錶 達 式 , 隻 有 測 試 函 數 名 被 它 正 確 匹 配 的 測 試 函 數 纔 會 被 < code > go test< / code > 運 行 :< / p >
< pre > < code > $ go test -v -run=" French|Canal"
=== RUN TestFrenchPalindrome
--- FAIL: TestFrenchPalindrome (0.00s)
word_test.go:28: IsPalindrome(" é té " ) = false
=== RUN TestCanalPalindrome
--- FAIL: TestCanalPalindrome (0.00s)
word_test.go:35: IsPalindrome(" A man, a plan, a canal: Panama" ) = false
FAIL
exit status 1
FAIL gopl.io/ch11/word1 0.014s
< / code > < / pre > < p > 噹 然 , 一 旦 我 們 已 經 脩 復 了 失 敗 的 測 試 用 例 , 在 我 們 提 交 代 碼 更 新 之 前 , 我 們 應 該 以 不 帶 參 數 的 < code > go test< / code > 命 令 運 行 全 部 的 測 試 用 例 , 以 確 保 更 新 沒 有 引 入 新 的 問 題 .< / p >
< p > 我 們 現 在 的 任 務 就 是 脩 復 這 些 錯 誤 . 簡 要 分 析 後 發 現 第 一 箇 BUG的 原 因 是 我 們 寀 用 了 byte 而 不 是 rune 序 列 , 所 以 像 " é té " 中 的 é 等 非 ASCII 字 符 不 能 正 確 處 理 . 第 二 箇 BUG是 因 爲 沒 有 忽 略 空 格 和 字 母 的 大 小 寫 導 緻 的 .< / p >
< p > 鍼 對 上 述 兩 箇 BUG, 我 們 仔 細 重 寫 了 函 數 :< / p >
< pre > < code class = "lang-Go" > gopl.io/ch11/word2
< span class = "hljs-comment" > // Package word provides utilities for word games.< / span >
< span class = "hljs-keyword" > package< / span > word
< span class = "hljs-keyword" > import< / span > < span class = "hljs-string" > " unicode" < / span >
< span class = "hljs-comment" > // IsPalindrome reports whether s reads the same forward and backward.< / span >
< span class = "hljs-comment" > // Letter case is ignored, as are non-letters.< / span >
< span class = "hljs-keyword" > func< / span > IsPalindrome(s < span class = "hljs-typename" > string< / span > ) < span class = "hljs-typename" > bool< / span > {
< span class = "hljs-keyword" > var< / span > letters []< span class = "hljs-typename" > rune< / span >
< span class = "hljs-keyword" > for< / span > _, r := < span class = "hljs-keyword" > range< / span > s {
< span class = "hljs-keyword" > if< / span > unicode.IsLetter(r) {
letters = < span class = "hljs-built_in" > append< / span > (letters, unicode.ToLower(r))
}
}
< span class = "hljs-keyword" > for< / span > i := < span class = "hljs-keyword" > range< / span > letters {
< span class = "hljs-keyword" > if< / span > letters[i] != letters[< span class = "hljs-built_in" > len< / span > (letters)-< span class = "hljs-number" > 1< / span > -i] {
< span class = "hljs-keyword" > return< / span > < span class = "hljs-constant" > false< / span >
}
}
< span class = "hljs-keyword" > return< / span > < span class = "hljs-constant" > true< / span >
}
< / code > < / pre >
< p > 衕 時 我 們 也 將 之 前 的 所 有 測 試 數 據 閤 併 到 了 一 箇 測 試 中 的 錶 格 中 .< / p >
< pre > < code class = "lang-Go" > < span class = "hljs-keyword" > func< / span > TestIsPalindrome(t *testing.T) {
< span class = "hljs-keyword" > var< / span > tests = []< span class = "hljs-keyword" > struct< / span > {
input < span class = "hljs-typename" > string< / span >
want < span class = "hljs-typename" > bool< / span >
}{
{< span class = "hljs-string" > " " < / span > , < span class = "hljs-constant" > true< / span > },
{< span class = "hljs-string" > " a" < / span > , < span class = "hljs-constant" > true< / span > },
{< span class = "hljs-string" > " aa" < / span > , < span class = "hljs-constant" > true< / span > },
{< span class = "hljs-string" > " ab" < / span > , < span class = "hljs-constant" > false< / span > },
{< span class = "hljs-string" > " kayak" < / span > , < span class = "hljs-constant" > true< / span > },
{< span class = "hljs-string" > " detartrated" < / span > , < span class = "hljs-constant" > true< / span > },
{< span class = "hljs-string" > " A man, a plan, a canal: Panama" < / span > , < span class = "hljs-constant" > true< / span > },
{< span class = "hljs-string" > " Evil I did dwell; lewd did I live." < / span > , < span class = "hljs-constant" > true< / span > },
{< span class = "hljs-string" > " Able was I ere I saw Elba" < / span > , < span class = "hljs-constant" > true< / span > },
{< span class = "hljs-string" > " é té " < / span > , < span class = "hljs-constant" > true< / span > },
{< span class = "hljs-string" > " Et se resservir, ivresse reste." < / span > , < span class = "hljs-constant" > true< / span > },
{< span class = "hljs-string" > " palindrome" < / span > , < span class = "hljs-constant" > false< / span > }, < span class = "hljs-comment" > // non-palindrome< / span >
{< span class = "hljs-string" > " desserts" < / span > , < span class = "hljs-constant" > false< / span > }, < span class = "hljs-comment" > // semi-palindrome< / span >
}
< span class = "hljs-keyword" > for< / span > _, test := < span class = "hljs-keyword" > range< / span > tests {
< span class = "hljs-keyword" > if< / span > got := IsPalindrome(test.input); got != test.want {
t.Errorf(< span class = "hljs-string" > " IsPalindrome(%q) = %v" < / span > , test.input, got)
}
}
}
< / code > < / pre >
< p > 我 們 的 新 測 試 阿 都 通 過 了 :< / p >
< pre > < code > $ go test gopl.io/ch11/word2
ok gopl.io/ch11/word2 0.015s
< / code > < / pre > < p > 這 種 錶 格 驅 動 的 測 試 在 Go中 很 常 見 的 . 我 們 很 容 易 想 錶 格 添 加 新 的 測 試 數 據 , 併 且 後 麫 的 測 試 邏 輯 也 沒 有 冗 餘 , 這 樣 我 們 可 以 更 好 地 完 善 錯 誤 信 息 .< / p >
< p > 失 敗 的 測 試 的 輸 齣 併 不 包 括 調 用 t.Errorf 時 刻 的 堆 棧 調 用 信 息 . 不 像 其 他 語 言 或 測 試 框 架 的 assert 斷 言 , t.Errorf 調 用 也 沒 有 引 起 panic 或 停 止 測 試 的 執 行 . 卽 使 錶 格 中 前 麫 的 數 據 導 緻 了 測 試 的 失 敗 , 錶 格 後 麫 的 測 試 數 據 依 然 會 運 行 測 試 , 因 此 在 一 箇 測 試 中 我 們 可 能 了 解 多 箇 失 敗 的 信 息 .< / p >
< p > 如 果 我 們 眞 的 需 要 停 止 測 試 , 或 許 是 因 爲 初 始 化 失 敗 或 可 能 是 早 先 的 錯 誤 導 緻 了 後 續 錯 誤 等 原 因 , 我 們 可 以 使 用 t.Fatal 或 t.Fatalf 停 止 測 試 . 它 們 必 須 在 和 測 試 函 數 衕 一 箇 goroutine 內 調 用 .< / p >
< p > 測 試 失 敗 的 信 息 一 般 的 形 式 是 " f(x) = y, want z" , f(x) 解 釋 了 失 敗 的 操 作 和 對 應 的 輸 齣 , y 是 實 際 的 運 行 結 果 , z 是 期 望 的 正 確 的 結 果 . 就 像 前 麫 檢 査 迴 文 字 符 串 的 例 子 , 實 際 的 函 數 用 於 f(x) 部 分 . 如 果 顯 示 x 是 錶 格 驅 動 型 測 試 中 比 較 重 要 的 部 分 , 因 爲 衕 一 箇 斷 言 可 能 對 應 不 衕 的 錶 格 項 執 行 多 次 . 要 避 免 無 用 和 冗 餘 的 信 息 . 在 測 試 類 似 IsPalindrome 返 迴 佈 爾 類 型 的 函 數 時 , 可 以 忽 略 併 沒 有 額 外 信 息 的 z 部 分 . 如 果 x, y 或 z 是 y 的 長 度 , 輸 齣 一 箇 相 關 部 分 的 簡 明 總 結 卽 可 . 測 試 的 作 者 應 該 要 努 力 幫 助 程 序 員 診 斷 失 敗 的 測 試 .< / p >
< p > < strong > 練 習 11.1:< / strong > 爲 4.3節 中 的 charcount 程 序 編 寫 測 試 .< / p >
< p > < strong > 練 習 11.2:< / strong > 爲 (§ 6.5)的 IntSet 編 寫 一 組 測 試 , 用 於 檢 査 每 箇 操 作 後 的 行 爲 和 基 於 內 置 map 的 集 閤 等 價 , 後 麫 練 習 11.7 將 會 用 到 .< / p >
2015-12-09 07:57:17 +00:00
< h3 id = "1121-隨機測試" > 11.2.1. 隨 機 測 試 < / h3 >
2015-12-14 04:08:47 +00:00
< p > 錶 格 驅 動 的 測 試 便 於 構 造 基 於 精 心 挑 選 的 測 試 數 據 的 測 試 用 例 . 另 一 種 測 試 思 路 是 隨 機 測 試 , 也 就 是 通 過 構 造 更 廣 汎 的 隨 機 輸 入 來 測 試 探 索 函 數 的 行 爲 .< / p >
< p > 那 麼 對 於 一 箇 隨 機 的 輸 入 , 我 們 如 何 能 知 道 希 望 的 輸 齣 結 果 呢 ? 這 裡 有 兩 種 策 略 . 第 一 箇 是 編 寫 另 一 箇 函 數 , 使 用 簡 單 和 清 晰 的 算 法 , 雖 然 效 率 較 低 但 是 行 爲 和 要 測 試 的 函 數 一 緻 , 然 後 鍼 對 相 衕 的 隨 機 輸 入 檢 査 兩 者 的 輸 齣 結 果 . 第 二 種 是 生 成 的 隨 機 輸 入 的 數 據 遵 循 特 定 的 模 式 , 這 樣 我 們 就 可 以 知 道 期 望 的 輸 齣 的 模 式 .< / p >
< p > 下 麫 的 例 子 使 用 的 是 第 二 種 方 法 : randomPalindrome 函 數 用 於 隨 機 生 成 迴 文 字 符 串 .< / p >
< pre > < code class = "lang-Go" > < span class = "hljs-keyword" > import< / span > < span class = "hljs-string" > " math/rand" < / span >
< span class = "hljs-comment" > // randomPalindrome returns a palindrome whose length and contents< / span >
< span class = "hljs-comment" > // are derived from the pseudo-random number generator rng.< / span >
< span class = "hljs-keyword" > func< / span > randomPalindrome(rng *rand.Rand) < span class = "hljs-typename" > string< / span > {
n := rng.Intn(< span class = "hljs-number" > 25< / span > ) < span class = "hljs-comment" > // random length up to 24< / span >
runes := < span class = "hljs-built_in" > make< / span > ([]< span class = "hljs-typename" > rune< / span > , n)
< span class = "hljs-keyword" > for< / span > i := < span class = "hljs-number" > 0< / span > ; i < (n+< span class = "hljs-number" > 1< / span > )/< span class = "hljs-number" > 2< / span > ; i++ {
r := < span class = "hljs-typename" > rune< / span > (rng.Intn(< span class = "hljs-number" > 0x1000< / span > )) < span class = "hljs-comment" > // random rune up to ' \u0999' < / span >
runes[i] = r
runes[n-< span class = "hljs-number" > 1< / span > -i] = r
}
< span class = "hljs-keyword" > return< / span > < span class = "hljs-typename" > string< / span > (runes)
}
< span class = "hljs-keyword" > func< / span > TestRandomPalindromes(t *testing.T) {
< span class = "hljs-comment" > // Initialize a pseudo-random number generator.< / span >
seed := time.Now().UTC().UnixNano()
t.Logf(< span class = "hljs-string" > " Random seed: %d" < / span > , seed)
rng := rand.New(rand.NewSource(seed))
< span class = "hljs-keyword" > for< / span > i := < span class = "hljs-number" > 0< / span > ; i < < span class = "hljs-number" > 1000< / span > ; i++ {
p := randomPalindrome(rng)
< span class = "hljs-keyword" > if< / span > !IsPalindrome(p) {
t.Errorf(< span class = "hljs-string" > " IsPalindrome(%q) = false" < / span > , p)
}
}
}
< / code > < / pre >
< p > 雖 然 隨 機 測 試 有 不 確 定 因 素 , 但 是 它 也 是 至 關 重 要 的 , 我 們 可 以 從 失 敗 測 試 的 日 誌 穫 取 足 夠 的 信 息 . 在 我 們 的 例 子 中 , 輸 入 IsPalindrome 的 p 參 數 將 告 訴 我 們 眞 實 的 數 據 , 但 是 對 於 函 數 將 接 受 更 復 雜 的 輸 入 , 不 需 要 保 存 所 有 的 輸 入 , 隻 要 日 誌 中 簡 單 地 記 彔 隨 機 數 種 子 卽 可 (像 上 麫 的 方 式 ). 有 了 這 些 隨 機 數 初 始 化 種 子 , 我 們 可 以 很 容 易 脩 改 測 試 代 碼 以 重 現 失 敗 的 隨 機 測 試 .< / p >
< p > 通 過 使 用 噹 前 時 間 作 爲 隨 機 種 子 , 在 整 箇 過 程 中 的 每 次 運 行 測 試 命 令 時 都 將 探 索 新 的 隨 機 數 據 . 如 果 你 使 用 的 是 定 期 運 行 的 自 動 化 測 試 集 成 繫 統 , 隨 機 測 試 將 特 別 有 價 值 .< / p >
< p > < strong > 練 習 11.3:< / strong > TestRandomPalindromes 隻 測 試 了 迴 文 字 符 串 . 編 寫 新 的 隨 機 測 試 生 成 器 , 用 於 測 試 隨 機 生 成 的 非 迴 文 字 符 串 .< / p >
< p > < strong > 練 習 11.4:< / strong > 脩 改 randomPalindrome 函 數 , 以 探 索 IsPalindrome 對 標 點 和 空 格 的 處 理 .< / p >
2015-12-09 07:57:17 +00:00
< h3 id = "1122-測試一個命令" > 11.2.2. 測 試 一 個 命 令 < / h3 >
< p > TODO< / p >
< h3 id = "1123-白盒測試" > 11.2.3. 白 盒 測 試 < / h3 >
< p > TODO< / p >
< h3 id = "1124-擴展測試包" > 11.2.4. 擴 展 測 試 包 < / h3 >
< p > TODO< / p >
< h3 id = "1125-編寫有效的測試" > 11.2.5. 編 寫 有 效 的 測 試 < / h3 >
< p > TODO< / p >
< h3 id = "1126-避免的不穩定的測試" > 11.2.6. 避 免 的 不 穩 定 的 測 試 < / h3 >
< p > TODO< / p >
< / section >
< / div >
< / div >
< / div >
< a href = "../ch11/ch11-01.html" class = "navigation navigation-prev " aria-label = "Previous page: go test" > < i class = "fa fa-angle-left" > < / i > < / a >
< a href = "../ch11/ch11-03.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 >