mirror of
https://github.com/gopl-zh/gopl-zh.github.com.git
synced 2024-11-05 05:53:45 +00:00
2542 lines
127 KiB
HTML
2542 lines
127 KiB
HTML
<!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>
|
|
|
|
|
|
<div class="book" data-level="11.2" data-chapter-title="測試函數" data-filepath="ch11/ch11-02.md" data-basepath=".." data-revision="Mon Dec 21 2015 12:51:02 GMT+0800 (中国标准时间)">
|
|
|
|
|
|
<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="errata.html">
|
|
|
|
|
|
<a href="../errata.html">
|
|
|
|
<i class="fa fa-check"></i>
|
|
|
|
<b>14.</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>
|
|
<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>
|
|
<h3 id="1121-隨機測試">11.2.1. 隨機測試</h3>
|
|
<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>
|
|
<h3 id="1122-測試一個命令">11.2.2. 測試一個命令</h3>
|
|
<p>對於測試包 <code>go test</code> 是一個的有用的工具, 但是稍加努力我們也可以用它來測試可執行程序. 如果一個包的名字是 main, 那麽在構建時會生成一個可執行程序, 不過 main 包可以作爲一個包被測試器代碼導入.</p>
|
|
<p>讓我們爲 2.3.2節 的 echo 程序編寫一個測試. 我們先將程序拆分爲兩個函數: echo 函數完成眞正的工作, main 函數用於處理命令行輸入參數和echo可能返迴的錯誤.</p>
|
|
<pre><code class="lang-Go">gopl.io/ch11/echo
|
|
<span class="hljs-comment">// Echo prints its command-line arguments.</span>
|
|
<span class="hljs-keyword">package</span> main
|
|
|
|
<span class="hljs-keyword">import</span> (
|
|
<span class="hljs-string">"flag"</span>
|
|
<span class="hljs-string">"fmt"</span>
|
|
<span class="hljs-string">"io"</span>
|
|
<span class="hljs-string">"os"</span>
|
|
<span class="hljs-string">"strings"</span>
|
|
)
|
|
|
|
<span class="hljs-keyword">var</span> (
|
|
n = flag.Bool(<span class="hljs-string">"n"</span>, <span class="hljs-constant">false</span>, <span class="hljs-string">"omit trailing newline"</span>)
|
|
s = flag.String(<span class="hljs-string">"s"</span>, <span class="hljs-string">" "</span>, <span class="hljs-string">"separator"</span>)
|
|
)
|
|
|
|
<span class="hljs-keyword">var</span> out io.Writer = os.Stdout <span class="hljs-comment">// modified during testing</span>
|
|
|
|
<span class="hljs-keyword">func</span> main() {
|
|
flag.Parse()
|
|
<span class="hljs-keyword">if</span> err := echo(!*n, *s, flag.Args()); err != <span class="hljs-constant">nil</span> {
|
|
fmt.Fprintf(os.Stderr, <span class="hljs-string">"echo: %v\n"</span>, err)
|
|
os.Exit(<span class="hljs-number">1</span>)
|
|
}
|
|
}
|
|
|
|
<span class="hljs-keyword">func</span> echo(newline <span class="hljs-typename">bool</span>, sep <span class="hljs-typename">string</span>, args []<span class="hljs-typename">string</span>) error {
|
|
fmt.Fprint(out, strings.Join(args, sep))
|
|
<span class="hljs-keyword">if</span> newline {
|
|
fmt.Fprintln(out)
|
|
}
|
|
<span class="hljs-keyword">return</span> <span class="hljs-constant">nil</span>
|
|
}
|
|
</code></pre>
|
|
<p>在測試中嗎我們可以用各種參數和標標誌調用 echo 函數, 然後檢測它的輸齣是否正確, 我們通過增加參數來減少 echo 函數對全局變量的依賴. 我們還增加了一個全局名爲 out 的變量來替代直接使用 os.Stdout, 這樣測試代碼可以根據需要將 out 脩改爲不同的對象以便於檢査. 下面就是 echo_test.go 文件中的測試代碼:</p>
|
|
<pre><code class="lang-Go"><span class="hljs-keyword">package</span> main
|
|
|
|
<span class="hljs-keyword">import</span> (
|
|
<span class="hljs-string">"bytes"</span>
|
|
<span class="hljs-string">"fmt"</span>
|
|
<span class="hljs-string">"testing"</span>
|
|
)
|
|
|
|
<span class="hljs-keyword">func</span> TestEcho(t *testing.T) {
|
|
<span class="hljs-keyword">var</span> tests = []<span class="hljs-keyword">struct</span> {
|
|
newline <span class="hljs-typename">bool</span>
|
|
sep <span class="hljs-typename">string</span>
|
|
args []<span class="hljs-typename">string</span>
|
|
want <span class="hljs-typename">string</span>
|
|
}{
|
|
{<span class="hljs-constant">true</span>, <span class="hljs-string">""</span>, []<span class="hljs-typename">string</span>{}, <span class="hljs-string">"\n"</span>},
|
|
{<span class="hljs-constant">false</span>, <span class="hljs-string">""</span>, []<span class="hljs-typename">string</span>{}, <span class="hljs-string">""</span>},
|
|
{<span class="hljs-constant">true</span>, <span class="hljs-string">"\t"</span>, []<span class="hljs-typename">string</span>{<span class="hljs-string">"one"</span>, <span class="hljs-string">"two"</span>, <span class="hljs-string">"three"</span>}, <span class="hljs-string">"one\ttwo\tthree\n"</span>},
|
|
{<span class="hljs-constant">true</span>, <span class="hljs-string">","</span>, []<span class="hljs-typename">string</span>{<span class="hljs-string">"a"</span>, <span class="hljs-string">"b"</span>, <span class="hljs-string">"c"</span>}, <span class="hljs-string">"a,b,c\n"</span>},
|
|
{<span class="hljs-constant">false</span>, <span class="hljs-string">":"</span>, []<span class="hljs-typename">string</span>{<span class="hljs-string">"1"</span>, <span class="hljs-string">"2"</span>, <span class="hljs-string">"3"</span>}, <span class="hljs-string">"1:2:3"</span>},
|
|
}
|
|
<span class="hljs-keyword">for</span> _, test := <span class="hljs-keyword">range</span> tests {
|
|
descr := fmt.Sprintf(<span class="hljs-string">"echo(%v, %q, %q)"</span>,
|
|
test.newline, test.sep, test.args)
|
|
|
|
out = <span class="hljs-built_in">new</span>(bytes.Buffer) <span class="hljs-comment">// captured output</span>
|
|
<span class="hljs-keyword">if</span> err := echo(test.newline, test.sep, test.args); err != <span class="hljs-constant">nil</span> {
|
|
t.Errorf(<span class="hljs-string">"%s failed: %v"</span>, descr, err)
|
|
<span class="hljs-keyword">continue</span>
|
|
}
|
|
got := out.(*bytes.Buffer).String()
|
|
<span class="hljs-keyword">if</span> got != test.want {
|
|
t.Errorf(<span class="hljs-string">"%s = %q, want %q"</span>, descr, got, test.want)
|
|
}
|
|
}
|
|
}
|
|
</code></pre>
|
|
<p>要註意的是測試代碼和産品代碼在同一個包. 雖然是main包, 也有對應的 main 入口函數, 但是在測試的時候 main 包隻是 TestEcho 測試函數導入的一個普通包, 里面 main 函數併沒有被導齣是被忽略的.</p>
|
|
<p>通過將測試放到表格中, 我們很容易添加新的測試用例. 讓我通過增加下面的測試用例來看看失敗的情況是怎麽樣的:</p>
|
|
<pre><code class="lang-Go">{<span class="hljs-constant">true</span>, <span class="hljs-string">","</span>, []<span class="hljs-typename">string</span>{<span class="hljs-string">"a"</span>, <span class="hljs-string">"b"</span>, <span class="hljs-string">"c"</span>}, <span class="hljs-string">"a b c\n"</span>}, <span class="hljs-comment">// <span class="hljs-doctag">NOTE:</span> wrong expectation!</span>
|
|
</code></pre>
|
|
<p><code>go test</code> 輸齣如下:</p>
|
|
<pre><code>$ go test gopl.io/ch11/echo
|
|
--- FAIL: TestEcho (0.00s)
|
|
echo_test.go:31: echo(true, ",", ["a" "b" "c"]) = "a,b,c", want "a b c\n"
|
|
FAIL
|
|
FAIL gopl.io/ch11/echo 0.006s
|
|
</code></pre><p>錯誤信息描述了嚐試的操作(使用Go類似語法), 實際的行爲, 和期望的行爲. 通過這樣的錯誤信息, 你可以在檢視代碼之前就很容易定位錯誤的原因.</p>
|
|
<p>要註意的是在測試代碼中併沒有調用 log.Fatal 或 os.Exit, 因爲調用這類函數會導致程序提前退齣; 調用這些函數的特權應該放在 main 函數中. 如果眞的有以外的事情導致函數發送 panic, 測試驅動應該嚐試 recover, 然後將當前測試當作失敗處理. 如果是可預期的錯誤, 例如非法的用戶輸入, 找不到文件, 或配置文件不當等應該通過返迴一個非空的 error 的方式處理. 幸運的是(上面的意外隻是一個插麴), 我們的 echo 示例是比較簡單的也沒有需要返迴非空error的情況.</p>
|
|
<h3 id="1123-白盒測試">11.2.3. 白盒測試</h3>
|
|
<p>一個測試分類的方法是基於測試者是否需要了解被測試對象的內部工作原理. 黑盒測試隻需要測試包公開的文檔和API行爲, 內部實現對測試代碼是透明的. 相反, 白盒測試有訪問包內部函數和數據結構的權限, 因此可以做到一下普通客戶端無法實現的測試. 例如, 一個飽和測試可以在每個操作之後檢測不變量的數據類型. (白盒測試隻是一個傳統的名稱, 其實稱爲 clear box 會更準確.)</p>
|
|
<p>黑盒和白盒這兩種測試方法是互補的. 黑盒測試一般更健壯, 隨着軟件實現的完善測試代碼很少需要更新. 它們可以幫助測試者了解眞是客戶的需求, 可以幫助發現API設計的一些不足之處. 相反, 白盒測試則可以對內部一些棘手的實現提供更多的測試覆蓋.</p>
|
|
<p>我們已經看到兩種測試的例子. TestIsPalindrome 測試僅僅使用導齣的 IsPalindrome 函數, 因此它是一個黑盒測試. TestEcho 測試則調用了內部的 echo 函數, 併且更新了內部的 out 全局變量, 這兩個都是未導齣的, 因此它是白盒測試.</p>
|
|
<p>當我們開發TestEcho測試的時候, 我們脩改了 echo 函數使用包級的 out 作爲輸齣對象, 因此測試代碼可以用另一個實現代替標準輸齣, 這樣可以方便對比 echo 的輸齣數據. 使用類似的技術, 我們可以將産品代碼的其他部分也替換爲一個容易測試的僞對象. 使用僞對象的好處是我們可以方便配置, 容易預測, 更可靠, 也更容易觀察. 同時也可以避免一些不良的副作用, 例如更新生産數據庫或信用卡消費行爲.</p>
|
|
<p>下面的代碼演示了爲用戶提供網絡存儲的web服務中的配額檢測邏輯. 當用戶使用了超過 90% 的存儲配額之後將發送提醒郵件.</p>
|
|
<pre><code class="lang-Go">gopl.io/ch11/storage1
|
|
|
|
<span class="hljs-keyword">package</span> storage
|
|
|
|
<span class="hljs-keyword">import</span> (
|
|
<span class="hljs-string">"fmt"</span>
|
|
<span class="hljs-string">"log"</span>
|
|
<span class="hljs-string">"net/smtp"</span>
|
|
)
|
|
|
|
<span class="hljs-keyword">func</span> bytesInUse(username <span class="hljs-typename">string</span>) <span class="hljs-typename">int64</span> { <span class="hljs-keyword">return</span> <span class="hljs-number">0</span> <span class="hljs-comment">/* ... */</span> }
|
|
|
|
<span class="hljs-comment">// Email sender configuration.</span>
|
|
<span class="hljs-comment">// <span class="hljs-doctag">NOTE:</span> never put passwords in source code!</span>
|
|
<span class="hljs-keyword">const</span> sender = <span class="hljs-string">"notifications@example.com"</span>
|
|
<span class="hljs-keyword">const</span> password = <span class="hljs-string">"correcthorsebatterystaple"</span>
|
|
<span class="hljs-keyword">const</span> hostname = <span class="hljs-string">"smtp.example.com"</span>
|
|
|
|
<span class="hljs-keyword">const</span> template = <span class="hljs-string">`Warning: you are using %d bytes of storage,
|
|
%d%% of your quota.`</span>
|
|
|
|
<span class="hljs-keyword">func</span> CheckQuota(username <span class="hljs-typename">string</span>) {
|
|
used := bytesInUse(username)
|
|
<span class="hljs-keyword">const</span> quota = <span class="hljs-number">1000000000</span> <span class="hljs-comment">// 1GB</span>
|
|
percent := <span class="hljs-number">100</span> * used / quota
|
|
<span class="hljs-keyword">if</span> percent < <span class="hljs-number">90</span> {
|
|
<span class="hljs-keyword">return</span> <span class="hljs-comment">// OK</span>
|
|
}
|
|
msg := fmt.Sprintf(template, used, percent)
|
|
auth := smtp.PlainAuth(<span class="hljs-string">""</span>, sender, password, hostname)
|
|
err := smtp.SendMail(hostname+<span class="hljs-string">":587"</span>, auth, sender,
|
|
[]<span class="hljs-typename">string</span>{username}, []<span class="hljs-typename">byte</span>(msg))
|
|
<span class="hljs-keyword">if</span> err != <span class="hljs-constant">nil</span> {
|
|
log.Printf(<span class="hljs-string">"smtp.SendMail(%s) failed: %s"</span>, username, err)
|
|
}
|
|
}
|
|
</code></pre>
|
|
<p>我們想測試這個代碼, 但是我們併不希望發送眞實的郵件. 因此我們將郵件處理邏輯放到一個私有的 notifyUser 函數.</p>
|
|
<pre><code class="lang-Go">gopl.io/ch11/storage2
|
|
|
|
<span class="hljs-keyword">var</span> notifyUser = <span class="hljs-keyword">func</span>(username, msg <span class="hljs-typename">string</span>) {
|
|
auth := smtp.PlainAuth(<span class="hljs-string">""</span>, sender, password, hostname)
|
|
err := smtp.SendMail(hostname+<span class="hljs-string">":587"</span>, auth, sender,
|
|
[]<span class="hljs-typename">string</span>{username}, []<span class="hljs-typename">byte</span>(msg))
|
|
<span class="hljs-keyword">if</span> err != <span class="hljs-constant">nil</span> {
|
|
log.Printf(<span class="hljs-string">"smtp.SendEmail(%s) failed: %s"</span>, username, err)
|
|
}
|
|
}
|
|
|
|
<span class="hljs-keyword">func</span> CheckQuota(username <span class="hljs-typename">string</span>) {
|
|
used := bytesInUse(username)
|
|
<span class="hljs-keyword">const</span> quota = <span class="hljs-number">1000000000</span> <span class="hljs-comment">// 1GB</span>
|
|
percent := <span class="hljs-number">100</span> * used / quota
|
|
<span class="hljs-keyword">if</span> percent < <span class="hljs-number">90</span> {
|
|
<span class="hljs-keyword">return</span> <span class="hljs-comment">// OK</span>
|
|
}
|
|
msg := fmt.Sprintf(template, used, percent)
|
|
notifyUser(username, msg)
|
|
}
|
|
</code></pre>
|
|
<p>現在我們可以在測試中用僞郵件發送函數替代眞實的郵件發送函數. 它隻是簡單記録要通知的用戶和郵件的內容.</p>
|
|
<pre><code class="lang-Go"><span class="hljs-keyword">package</span> storage
|
|
|
|
<span class="hljs-keyword">import</span> (
|
|
<span class="hljs-string">"strings"</span>
|
|
<span class="hljs-string">"testing"</span>
|
|
)
|
|
<span class="hljs-keyword">func</span> TestCheckQuotaNotifiesUser(t *testing.T) {
|
|
<span class="hljs-keyword">var</span> notifiedUser, notifiedMsg <span class="hljs-typename">string</span>
|
|
notifyUser = <span class="hljs-keyword">func</span>(user, msg <span class="hljs-typename">string</span>) {
|
|
notifiedUser, notifiedMsg = user, msg
|
|
}
|
|
|
|
<span class="hljs-comment">// ...simulate a 980MB-used condition...</span>
|
|
|
|
<span class="hljs-keyword">const</span> user = <span class="hljs-string">"joe@example.org"</span>
|
|
CheckQuota(user)
|
|
<span class="hljs-keyword">if</span> notifiedUser == <span class="hljs-string">""</span> && notifiedMsg == <span class="hljs-string">""</span> {
|
|
t.Fatalf(<span class="hljs-string">"notifyUser not called"</span>)
|
|
}
|
|
<span class="hljs-keyword">if</span> notifiedUser != user {
|
|
t.Errorf(<span class="hljs-string">"wrong user (%s) notified, want %s"</span>,
|
|
notifiedUser, user)
|
|
}
|
|
<span class="hljs-keyword">const</span> wantSubstring = <span class="hljs-string">"98% of your quota"</span>
|
|
<span class="hljs-keyword">if</span> !strings.Contains(notifiedMsg, wantSubstring) {
|
|
t.Errorf(<span class="hljs-string">"unexpected notification message <<%s>>, "</span>+
|
|
<span class="hljs-string">"want substring %q"</span>, notifiedMsg, wantSubstring)
|
|
}
|
|
}
|
|
</code></pre>
|
|
<p>這里有一個問題: 當測試函數返迴後, CheckQuota 將不能正常工作, 因爲 notifyUsers 依然使用的是測試函數的僞發送郵件函數. (當更新全局對象的時候總會有這種風險.) 我們必鬚脩改測試代碼恢複 notifyUsers 原先的狀態以便後續其他的測試沒有影響, 要確保所有的執行路徑後都能恢複, 包括測試失敗或 panic 情形. 在這種情況下, 我們建議使用 defer 處理恢複的代碼.</p>
|
|
<pre><code class="lang-Go"><span class="hljs-keyword">func</span> TestCheckQuotaNotifiesUser(t *testing.T) {
|
|
<span class="hljs-comment">// Save and restore original notifyUser.</span>
|
|
saved := notifyUser
|
|
<span class="hljs-keyword">defer</span> <span class="hljs-keyword">func</span>() { notifyUser = saved }()
|
|
|
|
<span class="hljs-comment">// Install the test's fake notifyUser.</span>
|
|
<span class="hljs-keyword">var</span> notifiedUser, notifiedMsg <span class="hljs-typename">string</span>
|
|
notifyUser = <span class="hljs-keyword">func</span>(user, msg <span class="hljs-typename">string</span>) {
|
|
notifiedUser, notifiedMsg = user, msg
|
|
}
|
|
<span class="hljs-comment">// ...rest of test...</span>
|
|
}
|
|
</code></pre>
|
|
<p>這種處理模式可以用來暫時保存和恢複所有的全局變量, 包括命令行標誌參數, 調試選項, 和優化參數; 安裝和移除導致生産代碼産生一些調試信息的鉤子函數; 還有有些誘導生産代碼進入某些重要狀態的改變, 比如 超時, 錯誤, 甚至是一些刻意製造的併發行爲.</p>
|
|
<p>以這種方式使用全局變量是安全的, 因爲 go test 併不會同時併發地執行多個測試.</p>
|
|
<h3 id="1124-擴展測試包">11.2.4. 擴展測試包</h3>
|
|
<p>考慮下這兩個包: net/url 包, 提供了 URL 解析的功能; net/http 包, 提供了web服務和HTTP客戶端的功能. 如我們所料, 上層的 net/http 包依賴下層的 net/url 包. 然後, net/url 包中的一個測試是演示不同URL和HTTP客戶端的交互行爲. 也就是説, 一個下層包的測試代碼導入了上層的包.</p>
|
|
<p><img src="../images/ch11-01.png" alt=""></p>
|
|
<p>這樣的行爲在 net/url 包的測試代碼中會導致包的循環依賴, 正如 圖11.1中向上箭頭所示, 同時正如我們在 10.1節所説, Go語言規范是禁止包的循環依賴的.</p>
|
|
<p>我們可以通過測試擴展包的方式解決循環依賴的問題, 也就是在 net/url 包所在的目録聲明一個 url_test 測試擴展包. 其中測試擴展包名的 <code>_test</code> 後綴告訴 go test 工具它應該建立一個額外的包來運行測試. 我們將這個擴展測試包的導入路徑視作是 net/url_test 會更容易理解, 但實際上它併不能被其他任何包導入.</p>
|
|
<p>因爲測試擴展包是一個獨立的包, 因此可以導入測試代碼依賴的其他的輔助包; 包內的測試代碼可能無法做到. 在設計層面, 測試擴展包是在所以它依賴的包的上層, 正如 圖11.2所示.</p>
|
|
<p><img src="../images/ch11-02.png" alt=""></p>
|
|
<p>通過迴避循環導入依賴, 擴展測試包可以更靈活的測試, 特别是集成測試(需要測試多個組件之間的交互), 可以像普通應用程序那樣自由地導入其他包.</p>
|
|
<p>我們可以用 go list 工具査看包對應目録中哪些Go源文件是産品代碼, 哪些是包內測試, 還哪些測試擴展包. 我們以 fmt 包作爲一個例子. GoFiles 表示産品代碼對應的Go源文件列表; 也就是 go build 命令要編譯的部分:</p>
|
|
<pre><code>$ go list -f={{.GoFiles}} fmt
|
|
[doc.go format.go print.go scan.go]
|
|
</code></pre><p>TestGoFiles 表示的是 fmt 包內部測試測試代碼, 以 _test.go 爲後綴文件名, 不過隻在測試時被構建:</p>
|
|
<pre><code>$ go list -f={{.TestGoFiles}} fmt
|
|
[export_test.go]
|
|
</code></pre><p>包的測試代碼通常都在這些文件中, 不過 fmt 包併非如此; 稍後我們再解釋 export_test.go 文件的作用.</p>
|
|
<p>XTestGoFiles 表示的是屬於測試擴展包的測試代碼, 也就是 fmt_test 包, 因此它們必鬚先導入 fmt 包. 同樣, 這些文件也隻是在測試時被構建運行:</p>
|
|
<pre><code>$ go list -f={{.XTestGoFiles}} fmt
|
|
[fmt_test.go scan_test.go stringer_test.go]
|
|
</code></pre><p>有時候測試擴展包需要訪問被測試包內部的代碼, 例如在一個爲了避免循環導入而被獨立到外部測試擴展包的白盒測試. 在這種情況下, 我們可以通過一些技巧解決: 我們在包內的一個 _test.go 文件中導齣一個內部的實現給測試擴展包. 因爲這些代碼隻有在測試時纔需要, 因此一般放在 export_test.go 文件中.</p>
|
|
<p>例如, fmt 包的 fmt.Scanf 需要 unicode.IsSpace 函數提供的功能. 但是爲了避免太多的依賴, fmt 包併沒有導入包含鉅大表格數據的 unicode 包; 相反fmt包有一個叫 isSpace 內部的簡易實現.</p>
|
|
<p>爲了確保 fmt.isSpace 和 unicode.IsSpace 函數的行爲一致, fmt 包謹慎地包含了一個測試. 是一個在測試擴展包內的測試, 因此是無法直接訪問到 isSpace 內部函數的, 因此 fmt 通過一個祕密齣口導齣了 isSpace 函數. export_test.go 文件就是專門用於測試擴展包的祕密齣口.</p>
|
|
<pre><code class="lang-Go"><span class="hljs-keyword">package</span> fmt
|
|
|
|
<span class="hljs-keyword">var</span> IsSpace = isSpace
|
|
</code></pre>
|
|
<p>這個測試文件併沒有定義測試代碼; 它隻是通過 fmt.IsSpace 簡單導齣了內部的 isSpace 函數, 提供給測試擴展包使用. 這個技巧可以廣泛用於位於測試擴展包的白盒測試.</p>
|
|
<h3 id="1125-編寫有效的測試">11.2.5. 編寫有效的測試</h3>
|
|
<p>許多Go新人會驚異與它的極簡的測試框架. 很多其他語言的測試框架都提供了識别測試函數的機製(通常使用反射或元數據), 通過設置一些 ‘‘setup’’ 和 ‘‘teardown’’ 的鉤子函數來執行測試用例運行的初始化或之後的清理操作, 同時測試工具箱還提供了很多類似assert斷言, 比較值, 格式化輸齣錯誤信息和停止一個識别的測試等輔助函數(通常使用異常機製). 雖然這些機製可以使得測試非常簡潔, 但是測試輸齣的日誌卻像火星文一般難以理解. 此外, 雖然測試最終也會輸齣 PASS 或 FAIL 的報告, 但是它們提供的信息格式卻非常不利於代碼維護者快速定位問題, 因爲失敗的信息的具體含義是非常隱患的, 比如 "assert: 0 == 1" 或 成頁的海量跟蹤日誌.</p>
|
|
<p>Go語言的測試風格則形成鮮明對比. 它期望測試者自己完成大部分的工作, 定義函數避免重複, 就像普通編程那樣. 編寫測試併不是一個機械的填充過程; 一個測試也有自己的接口, 盡管它的維護者也是測試僅有的一個用戶. 一個好的測試不應該引發其他無關的錯誤信息, 它隻要清晰簡潔地描述問題的癥狀卽可, 有時候可能還需要一些上下文信息. 在理想情況下, 維護者可以在不看代碼的情況下就能根據錯誤信息定位錯誤産生的原因. 一個好的測試不應該在遇到一點小錯誤就立刻退齣測試, 它應該嚐試報告更多的測試, 因此我們可能從多個失敗測試的模式中發現錯誤産生的規律.</p>
|
|
<p>下面的斷言函數比較兩個值, 然後生成一個通用的錯誤信息, 併停止程序. 它很方便使用也確實有效果, 但是當識别的時候, 錯誤時打印的信息幾乎是沒有價值的. 它併沒有爲解決問題提供一個很好的入口.</p>
|
|
<pre><code class="lang-Go"><span class="hljs-keyword">import</span> (
|
|
<span class="hljs-string">"fmt"</span>
|
|
<span class="hljs-string">"strings"</span>
|
|
<span class="hljs-string">"testing"</span>
|
|
)
|
|
<span class="hljs-comment">// A poor assertion function.</span>
|
|
<span class="hljs-keyword">func</span> assertEqual(x, y <span class="hljs-typename">int</span>) {
|
|
<span class="hljs-keyword">if</span> x != y {
|
|
<span class="hljs-built_in">panic</span>(fmt.Sprintf(<span class="hljs-string">"%d != %d"</span>, x, y))
|
|
}
|
|
}
|
|
<span class="hljs-keyword">func</span> TestSplit(t *testing.T) {
|
|
words := strings.Split(<span class="hljs-string">"a:b:c"</span>, <span class="hljs-string">":"</span>)
|
|
assertEqual(<span class="hljs-built_in">len</span>(words), <span class="hljs-number">3</span>)
|
|
<span class="hljs-comment">// ...</span>
|
|
}
|
|
</code></pre>
|
|
<p>從這個意義上説, 斷言函數犯了過早抽象的錯誤: 僅僅測試兩個整數是否相同, 而放棄了根據上下文提供更有意義的錯誤信息的做法. 我們可以根據具體的錯誤打印一個更有價值的錯誤信息, 就像下面例子那樣. 測試在隻有一次重複的模式齣現時引入抽象.</p>
|
|
<pre><code class="lang-Go"><span class="hljs-keyword">func</span> TestSplit(t *testing.T) {
|
|
s, sep := <span class="hljs-string">"a:b:c"</span>, <span class="hljs-string">":"</span>
|
|
words := strings.Split(s, sep)
|
|
<span class="hljs-keyword">if</span> got, want := <span class="hljs-built_in">len</span>(words), <span class="hljs-number">3</span>; got != want {
|
|
t.Errorf(<span class="hljs-string">"Split(%q, %q) returned %d words, want %d"</span>,
|
|
s, sep, got, want)
|
|
}
|
|
<span class="hljs-comment">// ...</span>
|
|
}
|
|
</code></pre>
|
|
<p>現在的測試不僅報告了調用的具體函數, 它的輸入, 和結果的意義; 併且打印的眞實返迴的值和期望返迴的值; 併且卽使斷言失敗依然會繼續嚐試運行更多的測試. 一旦我們寫了這樣結構的測試, 下一步自然不是用更多的if語句來擴展測試用例, 我們可以用像 IsPalindrome 的表驅動測試那樣來準備更多的 s, sep 測試用例.</p>
|
|
<p>前面的例子併不需要額外的輔助函數, 如果如果有可以使測試代碼更簡單的方法我們也樂意接受. (我們將在 13.3節 看到一個 reflect.DeepEqual 輔助函數.) 開始一個好的測試的關鍵是通過實現你眞正想要的具體行爲, 然後纔是考慮然後簡化測試代碼. 最好的結果是直接從庫的抽象接口開始, 針對公共接口編寫一些測試函數.</p>
|
|
<p><strong>練習11.5:</strong> 用表格驅動的技術擴展TestSplit測試, 併打印期望的輸齣結果.</p>
|
|
<h3 id="1126-避免的不穩定的測試">11.2.6. 避免的不穩定的測試</h3>
|
|
<p>如果一個應用程序對於新齣現的但有效的輸入經常失敗説明程序不夠穩健; 同樣如果一個測試僅僅因爲聲音變化就會導致失敗也是不合邏輯的. 就像一個不夠穩健的程序會挫敗它的用戶一樣, 一個脆弱性測試同樣會激怒它的維護者. 最脆弱的測試代碼會在程序沒有任何變化的時候産生不同的結果, 時好時壞, 處理它們會耗費大量的時間但是併不會得到任何好處.</p>
|
|
<p>當一個測試函數産生一個複雜的輸齣如一個很長的字符串, 或一個精心設計的數據結構, 或一個文件, 它可以用於和預設的‘‘golden’’結果數據對比, 用這種簡單方式寫測試是誘人的. 但是隨着項目的發展, 輸齣的某些部分很可能會發生變化, 盡管很可能是一個改進的實現導致的. 而且不僅僅是輸齣部分, 函數複雜複製的輸入部分可能也跟着變化了, 因此測試使用的輸入也就不在有效了.</p>
|
|
<p>避免脆弱測試代碼的方法是隻檢測你眞正關心的屬性. 保存測試代碼的簡潔和內部結構的穩定. 特别是對斷言部分要有所選擇. 不要檢査字符串的全匹配, 但是尋找相關的子字符串, 因爲某些子字符串在項目的發展中是比較穩定不變的. 通常編寫一個重複雜的輸齣中提取必要精華信息以用於斷言是值得的, 雖然這可能會帶來很多前期的工作, 但是它可以幫助迅速及時脩複因爲項目演化而導致的不合邏輯的失敗測試.</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>
|