gopl-zh.github.com/ch12/ch12-03.html
2016-01-02 16:04:45 +08:00

2248 lines
82 KiB
HTML

<!DOCTYPE HTML>
<html lang="zh-tw" >
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<title>Display遞歸打印 | Go语言圣经</title>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type">
<meta name="description" content="">
<meta name="generator" content="GitBook 2.6.6">
<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-katex/katex.min.css">
<link rel="stylesheet" href="../gitbook/plugins/gitbook-plugin-highlight/website.css">
<link rel="stylesheet" href="../gitbook/plugins/gitbook-plugin-fontsettings/website.css">
<link rel="next" href="../ch12/ch12-04.html" />
<link rel="prev" href="../ch12/ch12-02.html" />
</head>
<body>
<div class="book"
data-level="12.3"
data-chapter-title="Display遞歸打印"
data-filepath="ch12/ch12-03.md"
data-basepath=".."
data-revision="Sat Jan 02 2016 16:00:23 GMT+0800 (中国标准时间)"
data-innerlanguage="">
<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>
Slice
</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>
Map
</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 " 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 active" 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="CONTRIBUTORS.html">
<a href="../CONTRIBUTORS.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="123-display&#x905E;&#x6B78;&#x6253;&#x5370;">12.3. Display&#x905E;&#x6B78;&#x6253;&#x5370;</h2>
<p>&#x63A5;&#x4E0B;&#x4F86;&#xFF0C;&#x8B93;&#x6211;&#x5011;&#x770B;&#x770B;&#x5982;&#x4F55;&#x6539;&#x5584;&#x805A;&#x5408;&#x6578;&#x64DA;&#x985E;&#x578B;&#x7684;&#x986F;&#x793A;&#x3002;&#x6211;&#x5011;&#x4F75;&#x4E0D;&#x60F3;&#x5B8C;&#x5168;&#x524B;&#x9686;&#x4E00;&#x500B;fmt.Sprint&#x51FD;&#x6578;&#xFF0C;&#x6211;&#x5011;&#x96BB;&#x662F;&#x50CF;&#x69CB;&#x5EFA;&#x4E00;&#x500B;&#x7528;&#x65BC;&#x8ABF;&#x5F0F;&#x7528;&#x7684;Display&#x51FD;&#x6578;&#xFF0C;&#x7D66;&#x5B9A;&#x4E00;&#x500B;&#x805A;&#x5408;&#x985E;&#x578B;x&#xFF0C;&#x6253;&#x5370;&#x9019;&#x500B;&#x503C;&#x5C0D;&#x61C9;&#x7684;&#x5B8C;&#x6574;&#x7684;&#x7D50;&#x69CB;&#xFF0C;&#x540C;&#x6642;&#x8A18;&#x9332;&#x6BCF;&#x500B;&#x767C;&#x73FE;&#x7684;&#x6BCF;&#x500B;&#x5143;&#x7D20;&#x7684;&#x8DEF;&#x5F91;&#x3002;&#x8B93;&#x6211;&#x5011;&#x5F9E;&#x4E00;&#x500B;&#x4F8B;&#x5B50;&#x958B;&#x59CB;&#x3002;</p>
<pre><code class="lang-Go">e, _ := eval.Parse(<span class="hljs-string">&quot;sqrt(A / pi)&quot;</span>)
Display(<span class="hljs-string">&quot;e&quot;</span>, e)
</code></pre>
<p>&#x5728;&#x4E0A;&#x9762;&#x7684;&#x8ABF;&#x7528;&#x4E2D;&#xFF0C;&#x50B3;&#x5165;Display&#x51FD;&#x6578;&#x7684;&#x53C3;&#x6578;&#x662F;&#x5728;7.9&#x7BC0;&#x4E00;&#x500B;&#x8868;&#x9054;&#x5F0F;&#x6C42;&#x503C;&#x51FD;&#x6578;&#x8FD4;&#x8FF4;&#x7684;&#x8A9E;&#x6CD5;&#x6A39;&#x3002;Display&#x51FD;&#x6578;&#x7684;&#x8F38;&#x51FA;&#x5982;&#x4E0B;&#xFF1A;</p>
<pre><code class="lang-Go">Display e (eval.call):
e.fn = <span class="hljs-string">&quot;sqrt&quot;</span>
e.args[<span class="hljs-number">0</span>].<span class="hljs-keyword">type</span> = eval.binary
e.args[<span class="hljs-number">0</span>].value.op = <span class="hljs-number">47</span>
e.args[<span class="hljs-number">0</span>].value.x.<span class="hljs-keyword">type</span> = eval.Var
e.args[<span class="hljs-number">0</span>].value.x.value = <span class="hljs-string">&quot;A&quot;</span>
e.args[<span class="hljs-number">0</span>].value.y.<span class="hljs-keyword">type</span> = eval.Var
e.args[<span class="hljs-number">0</span>].value.y.value = <span class="hljs-string">&quot;pi&quot;</span>
</code></pre>
<p>&#x5728;&#x53EF;&#x80FD;&#x7684;&#x60C5;&#x6CC1;&#x4E0B;&#xFF0C;&#x4F60;&#x61C9;&#x8A72;&#x907F;&#x514D;&#x5728;&#x4E00;&#x500B;&#x5305;&#x4E2D;&#x66B4;&#x9732;&#x548C;&#x53CD;&#x5C04;&#x76F8;&#x95DC;&#x7684;&#x63A5;&#x53E3;&#x3002;&#x6211;&#x5011;&#x5C07;&#x5B9A;&#x7FA9;&#x4E00;&#x500B;&#x672A;&#x5C0E;&#x51FA;&#x7684;display&#x51FD;&#x6578;&#x7528;&#x65BC;&#x905E;&#x6B78;&#x8655;&#x7406;&#x5DE5;&#x4F5C;&#xFF0C;&#x5C0E;&#x51FA;&#x7684;&#x662F;Display&#x51FD;&#x6578;&#xFF0C;&#x5B83;&#x96BB;&#x662F;display&#x51FD;&#x6578;&#x7C21;&#x55AE;&#x7684;&#x5305;&#x88DD;&#x4EE5;&#x63A5;&#x53D7;interface{}&#x985E;&#x578B;&#x7684;&#x53C3;&#x6578;&#xFF1A;</p>
<pre><code class="lang-Go">gopl.io/ch12/display
<span class="hljs-keyword">func</span> Display(name <span class="hljs-typename">string</span>, x <span class="hljs-keyword">interface</span>{}) {
fmt.Printf(<span class="hljs-string">&quot;Display %s (%T):\n&quot;</span>, name, x)
display(name, reflect.ValueOf(x))
}
</code></pre>
<p>&#x5728;display&#x51FD;&#x6578;&#x4E2D;&#xFF0C;&#x6211;&#x5011;&#x4F7F;&#x7528;&#x4E86;&#x524D;&#x9762;&#x5B9A;&#x7FA9;&#x7684;&#x6253;&#x5370;&#x57FA;&#x790E;&#x985E;&#x578B;&#x2014;&#x2014;&#x57FA;&#x672C;&#x985E;&#x578B;&#x3001;&#x51FD;&#x6578;&#x548C;chan&#x7B49;&#x2014;&#x2014;&#x5143;&#x7D20;&#x503C;&#x7684;formatAtom&#x51FD;&#x6578;&#xFF0C;&#x4F46;&#x662F;&#x6211;&#x5011;&#x6703;&#x4F7F;&#x7528;reflect.Value&#x7684;&#x65B9;&#x6CD5;&#x4F86;&#x905E;&#x6B78;&#x986F;&#x793A;&#x805A;&#x5408;&#x985E;&#x578B;&#x7684;&#x6BCF;&#x4E00;&#x500B;&#x6210;&#x54E1;&#x6216;&#x5143;&#x7D20;&#x3002;&#x5728;&#x905E;&#x6B78;&#x4E0B;&#x964D;&#x904E;&#x7A0B;&#x4E2D;&#xFF0C;path&#x5B57;&#x7B26;&#x4E32;&#xFF0C;&#x5F9E;&#x6700;&#x958B;&#x59CB;&#x50B3;&#x5165;&#x7684;&#x8D77;&#x59CB;&#x503C;&#xFF08;&#x9019;&#x91CC;&#x662F;&#x201C;e&#x201D;&#xFF09;&#xFF0C;&#x5C07;&#x9010;&#x6B65;&#x589E;&#x9577;&#x4EE5;&#x8868;&#x793A;&#x5982;&#x4F55;&#x9054;&#x5230;&#x7576;&#x524D;&#x503C;&#xFF08;&#x4F8B;&#x5982;&#x201C;e.args[0].value&#x201D;&#xFF09;&#x3002;</p>
<p>&#x56E0;&#x7232;&#x6211;&#x5011;&#x4E0D;&#x518D;&#x6A21;&#x64EC;fmt.Sprint&#x51FD;&#x6578;&#xFF0C;&#x6211;&#x5011;&#x5C07;&#x76F4;&#x63A5;&#x4F7F;&#x7528;fmt&#x5305;&#x4F86;&#x7C21;&#x5316;&#x6211;&#x5011;&#x7684;&#x4F8B;&#x5B50;&#x5BE6;&#x73FE;&#x3002;</p>
<pre><code class="lang-Go"><span class="hljs-keyword">func</span> display(path <span class="hljs-typename">string</span>, v reflect.Value) {
<span class="hljs-keyword">switch</span> v.Kind() {
<span class="hljs-keyword">case</span> reflect.Invalid:
fmt.Printf(<span class="hljs-string">&quot;%s = invalid\n&quot;</span>, path)
<span class="hljs-keyword">case</span> reflect.Slice, reflect.Array:
<span class="hljs-keyword">for</span> i := <span class="hljs-number">0</span>; i &lt; v.Len(); i++ {
display(fmt.Sprintf(<span class="hljs-string">&quot;%s[%d]&quot;</span>, path, i), v.Index(i))
}
<span class="hljs-keyword">case</span> reflect.Struct:
<span class="hljs-keyword">for</span> i := <span class="hljs-number">0</span>; i &lt; v.NumField(); i++ {
fieldPath := fmt.Sprintf(<span class="hljs-string">&quot;%s.%s&quot;</span>, path, v.Type().Field(i).Name)
display(fieldPath, v.Field(i))
}
<span class="hljs-keyword">case</span> reflect.Map:
<span class="hljs-keyword">for</span> _, key := <span class="hljs-keyword">range</span> v.MapKeys() {
display(fmt.Sprintf(<span class="hljs-string">&quot;%s[%s]&quot;</span>, path,
formatAtom(key)), v.MapIndex(key))
}
<span class="hljs-keyword">case</span> reflect.Ptr:
<span class="hljs-keyword">if</span> v.IsNil() {
fmt.Printf(<span class="hljs-string">&quot;%s = nil\n&quot;</span>, path)
} <span class="hljs-keyword">else</span> {
display(fmt.Sprintf(<span class="hljs-string">&quot;(*%s)&quot;</span>, path), v.Elem())
}
<span class="hljs-keyword">case</span> reflect.Interface:
<span class="hljs-keyword">if</span> v.IsNil() {
fmt.Printf(<span class="hljs-string">&quot;%s = nil\n&quot;</span>, path)
} <span class="hljs-keyword">else</span> {
fmt.Printf(<span class="hljs-string">&quot;%s.type = %s\n&quot;</span>, path, v.Elem().Type())
display(path+<span class="hljs-string">&quot;.value&quot;</span>, v.Elem())
}
<span class="hljs-keyword">default</span>: <span class="hljs-comment">// basic types, channels, funcs</span>
fmt.Printf(<span class="hljs-string">&quot;%s = %s\n&quot;</span>, path, formatAtom(v))
}
}
</code></pre>
<p>&#x8B93;&#x6211;&#x5011;&#x91DD;&#x5C0D;&#x4E0D;&#x540C;&#x985E;&#x578B;&#x5206;&#x522B;&#x8A0E;&#x8AD6;&#x3002;</p>
<p><strong>Slice&#x548C;&#x6578;&#x7D44;&#xFF1A;</strong> &#x5169;&#x7A2E;&#x7684;&#x8655;&#x7406;&#x908F;&#x8F2F;&#x662F;&#x4E00;&#x6A23;&#x7684;&#x3002;Len&#x65B9;&#x6CD5;&#x8FD4;&#x8FF4;slice&#x6216;&#x6578;&#x7D44;&#x503C;&#x4E2D;&#x7684;&#x5143;&#x7D20;&#x500B;&#x6578;&#xFF0C;Index(i)&#x6D3B;&#x52D5;&#x7D22;&#x5F15;i&#x5C0D;&#x61C9;&#x7684;&#x5143;&#x7D20;&#xFF0C;&#x8FD4;&#x8FF4;&#x7684;&#x4E5F;&#x662F;&#x4E00;&#x500B;reflect.Value&#x985E;&#x578B;&#x7684;&#x503C;&#xFF1B;&#x5982;&#x679C;&#x7D22;&#x5F15;i&#x8D85;&#x51FA;&#x8303;&#x570D;&#x7684;&#x8A71;&#x5C07;&#x5C0E;&#x81F4;panic&#x7570;&#x5E38;&#xFF0C;&#x9019;&#x4E9B;&#x884C;&#x7232;&#x548C;&#x6578;&#x7D44;&#x6216;slice&#x985E;&#x578B;&#x5167;&#x5EFA;&#x7684;len(a)&#x548C;a[i]&#x7B49;&#x64CD;&#x4F5C;&#x985E;&#x4F3C;&#x3002;display&#x91DD;&#x5C0D;&#x5E8F;&#x5217;&#x4E2D;&#x7684;&#x6BCF;&#x500B;&#x5143;&#x7D20;&#x905E;&#x6B78;&#x8ABF;&#x7528;&#x81EA;&#x8EAB;&#x8655;&#x7406;&#xFF0C;&#x6211;&#x5011;&#x901A;&#x904E;&#x5728;&#x905E;&#x6B78;&#x8655;&#x7406;&#x6642;&#x5411;path&#x9644;&#x52A0;&#x201C;[i]&#x201D;&#x4F86;&#x8868;&#x793A;&#x8A2A;&#x554F;&#x8DEF;&#x5F91;&#x3002;</p>
<p>&#x96D6;&#x7136;reflect.Value&#x985E;&#x578B;&#x5E36;&#x6709;&#x5F88;&#x591A;&#x65B9;&#x6CD5;&#xFF0C;&#x4F46;&#x662F;&#x96BB;&#x6709;&#x5C11;&#x6578;&#x7684;&#x65B9;&#x6CD5;&#x5C0D;&#x4EFB;&#x610F;&#x503C;&#x90FD;&#x662F;&#x53EF;&#x4EE5;&#x5B89;&#x5168;&#x8ABF;&#x7528;&#x7684;&#x3002;&#x4F8B;&#x5982;&#xFF0C;Index&#x65B9;&#x6CD5;&#x96BB;&#x80FD;&#x5C0D;Slice&#x3001;&#x6578;&#x7D44;&#x6216;&#x5B57;&#x7B26;&#x4E32;&#x985E;&#x578B;&#x7684;&#x503C;&#x8ABF;&#x7528;&#xFF0C;&#x5176;&#x5B83;&#x985E;&#x578B;&#x5982;&#x679C;&#x8ABF;&#x7528;&#x5C07;&#x5C0E;&#x81F4;panic&#x7570;&#x5E38;&#x3002;</p>
<p><strong>&#x7D50;&#x69CB;&#x9AD4;&#xFF1A;</strong> NumField&#x65B9;&#x6CD5;&#x5831;&#x544A;&#x7D50;&#x69CB;&#x9AD4;&#x4E2D;&#x6210;&#x54E1;&#x7684;&#x6578;&#x91CF;&#xFF0C;Field(i)&#x4EE5;reflect.Value&#x985E;&#x578B;&#x8FD4;&#x8FF4;&#x7B2C;i&#x500B;&#x6210;&#x54E1;&#x7684;&#x503C;&#x3002;&#x6210;&#x54E1;&#x5217;&#x8868;&#x5305;&#x542B;&#x4E86;&#x533F;&#x540D;&#x6210;&#x54E1;&#x5728;&#x5167;&#x7684;&#x5168;&#x90E8;&#x6210;&#x54E1;&#x3002;&#x901A;&#x904E;&#x5728;path&#x6DFB;&#x52A0;&#x201C;.f&#x201D;&#x4F86;&#x8868;&#x793A;&#x6210;&#x54E1;&#x8DEF;&#x5F91;&#xFF0C;&#x6211;&#x5011;&#x5FC5;&#x9B1A;&#x7372;&#x5F97;&#x7D50;&#x69CB;&#x9AD4;&#x5C0D;&#x61C9;&#x7684;reflect.Type&#x985E;&#x578B;&#x4FE1;&#x606F;&#xFF0C;&#x5305;&#x542B;&#x7D50;&#x69CB;&#x9AD4;&#x985E;&#x578B;&#x548C;&#x7B2C;i&#x500B;&#x6210;&#x54E1;&#x7684;&#x540D;&#x5B57;&#x3002;</p>
<p><strong>Maps:</strong> MapKeys&#x65B9;&#x6CD5;&#x8FD4;&#x8FF4;&#x4E00;&#x500B;reflect.Value&#x985E;&#x578B;&#x7684;slice&#xFF0C;&#x6BCF;&#x4E00;&#x500B;&#x90FD;&#x5C0D;&#x61C9;map&#x7684;&#x53EF;&#x4EE5;&#x3002;&#x548C;&#x5F80;&#x5E38;&#x4E00;&#x6A23;&#xFF0C;&#x904D;&#x6B77;map&#x6642;&#x9806;&#x5E8F;&#x662F;&#x96A8;&#x6A5F;&#x7684;&#x3002;MapIndex(key)&#x8FD4;&#x8FF4;map&#x4E2D;key&#x5C0D;&#x61C9;&#x7684;value&#x3002;&#x6211;&#x5011;&#x5411;path&#x6DFB;&#x52A0;&#x201C;[key]&#x201D;&#x4F86;&#x8868;&#x793A;&#x8A2A;&#x554F;&#x8DEF;&#x5F91;&#x3002;&#xFF08;&#x6211;&#x5011;&#x9019;&#x91CC;&#x6709;&#x4E00;&#x500B;&#x672A;&#x5B8C;&#x6210;&#x7684;&#x5DE5;&#x4F5C;&#x3002;&#x5176;&#x5BE6;map&#x7684;key&#x7684;&#x985E;&#x578B;&#x4F75;&#x4E0D;&#x5C40;&#x9650;&#x65BC;formatAtom&#x80FD;&#x5B8C;&#x7F8E;&#x8655;&#x7406;&#x7684;&#x985E;&#x578B;&#xFF1B;&#x6578;&#x7D44;&#x3001;&#x7D50;&#x69CB;&#x9AD4;&#x548C;&#x63A5;&#x53E3;&#x90FD;&#x53EF;&#x4EE5;&#x4F5C;&#x7232;map&#x7684;key&#x3002;&#x91DD;&#x5C0D;&#x9019;&#x7A2E;&#x985E;&#x578B;&#xFF0C;&#x5B8C;&#x5584;key&#x7684;&#x986F;&#x793A;&#x4FE1;&#x606F;&#x662F;&#x7DF4;&#x7FD2;12.1&#x7684;&#x4EFB;&#x52D9;&#x3002;&#xFF09;</p>
<p><strong>&#x6307;&#x91DD;&#xFF1A;</strong> Elem&#x65B9;&#x6CD5;&#x8FD4;&#x8FF4;&#x6307;&#x91DD;&#x6307;&#x5411;&#x7684;&#x8B8A;&#x91CF;&#xFF0C;&#x9084;&#x662F;reflect.Value&#x985E;&#x578B;&#x3002;&#x6280;&#x8853;&#x6307;&#x91DD;&#x662F;nil&#xFF0C;&#x9019;&#x500B;&#x64CD;&#x4F5C;&#x4E5F;&#x662F;&#x5B89;&#x5168;&#x7684;&#xFF0C;&#x5728;&#x9019;&#x7A2E;&#x60C5;&#x6CC1;&#x4E0B;&#x6307;&#x91DD;&#x662F;Invalid&#x7121;&#x6548;&#x985E;&#x578B;&#xFF0C;&#x4F46;&#x662F;&#x6211;&#x5011;&#x53EF;&#x4EE5;&#x7528;IsNil&#x65B9;&#x6CD5;&#x4F86;&#x986F;&#x5F0F;&#x5730;&#x6E2C;&#x8A66;&#x4E00;&#x500B;&#x7A7A;&#x6307;&#x91DD;&#xFF0C;&#x9019;&#x6A23;&#x6211;&#x5011;&#x53EF;&#x4EE5;&#x6253;&#x5370;&#x66F4;&#x5408;&#x9069;&#x7684;&#x4FE1;&#x606F;&#x3002;&#x6211;&#x5011;&#x5728;path&#x524D;&#x9762;&#x6DFB;&#x52A0;&#x201C;*&#x201D;&#xFF0C;&#x4F75;&#x7528;&#x62EC;&#x5F27;&#x5305;&#x542B;&#x4EE5;&#x907F;&#x514D;&#x6B67;&#x7FA9;&#x3002;</p>
<p><strong>&#x63A5;&#x53E3;&#xFF1A;</strong> &#x518D;&#x4E00;&#x6B21;&#xFF0C;&#x6211;&#x5011;&#x4F7F;&#x7528;IsNil&#x65B9;&#x6CD5;&#x4F86;&#x6E2C;&#x8A66;&#x63A5;&#x53E3;&#x662F;&#x5426;&#x662F;nil&#xFF0C;&#x5982;&#x679C;&#x4E0D;&#x662F;&#xFF0C;&#x6211;&#x5011;&#x53EF;&#x4EE5;&#x8ABF;&#x7528;v.Elem()&#x4F86;&#x7372;&#x53D6;&#x63A5;&#x53E3;&#x5C0D;&#x61C9;&#x7684;&#x52D5;&#x614B;&#x503C;&#xFF0C;&#x4F75;&#x4E14;&#x6253;&#x5370;&#x5C0D;&#x61C9;&#x7684;&#x985E;&#x578B;&#x548C;&#x503C;&#x3002;</p>
<p>&#x73FE;&#x5728;&#x6211;&#x5011;&#x7684;Display&#x51FD;&#x6578;&#x7E3D;&#x7B97;&#x5B8C;&#x5DE5;&#x4E86;&#xFF0C;&#x8B93;&#x6211;&#x5011;&#x770B;&#x770B;&#x5B83;&#x7684;&#x8868;&#x73FE;&#x5427;&#x3002;&#x4E0B;&#x9762;&#x7684;Movie&#x985E;&#x578B;&#x662F;&#x5728;4.5&#x7BC0;&#x7684;&#x96FB;&#x5F71;&#x985E;&#x578B;&#x4E0A;&#x6F14;&#x8B8A;&#x4F86;&#x7684;&#xFF1A;</p>
<pre><code class="lang-Go"><span class="hljs-keyword">type</span> Movie <span class="hljs-keyword">struct</span> {
Title, Subtitle <span class="hljs-typename">string</span>
Year <span class="hljs-typename">int</span>
Color <span class="hljs-typename">bool</span>
Actor <span class="hljs-keyword">map</span>[<span class="hljs-typename">string</span>]<span class="hljs-typename">string</span>
Oscars []<span class="hljs-typename">string</span>
Sequel *<span class="hljs-typename">string</span>
}
</code></pre>
<p>&#x8B93;&#x6211;&#x5011;&#x8072;&#x660E;&#x4E00;&#x500B;&#x8A72;&#x985E;&#x578B;&#x7684;&#x8B8A;&#x91CF;&#xFF0C;&#x7136;&#x5F8C;&#x770B;&#x770B;Display&#x51FD;&#x6578;&#x5982;&#x4F55;&#x986F;&#x793A;&#x5B83;&#xFF1A;</p>
<pre><code class="lang-Go">strangelove := Movie{
Title: <span class="hljs-string">&quot;Dr. Strangelove&quot;</span>,
Subtitle: <span class="hljs-string">&quot;How I Learned to Stop Worrying and Love the Bomb&quot;</span>,
Year: <span class="hljs-number">1964</span>,
Color: <span class="hljs-constant">false</span>,
Actor: <span class="hljs-keyword">map</span>[<span class="hljs-typename">string</span>]<span class="hljs-typename">string</span>{
<span class="hljs-string">&quot;Dr. Strangelove&quot;</span>: <span class="hljs-string">&quot;Peter Sellers&quot;</span>,
<span class="hljs-string">&quot;Grp. Capt. Lionel Mandrake&quot;</span>: <span class="hljs-string">&quot;Peter Sellers&quot;</span>,
<span class="hljs-string">&quot;Pres. Merkin Muffley&quot;</span>: <span class="hljs-string">&quot;Peter Sellers&quot;</span>,
<span class="hljs-string">&quot;Gen. Buck Turgidson&quot;</span>: <span class="hljs-string">&quot;George C. Scott&quot;</span>,
<span class="hljs-string">&quot;Brig. Gen. Jack D. Ripper&quot;</span>: <span class="hljs-string">&quot;Sterling Hayden&quot;</span>,
<span class="hljs-string">`Maj. T.J. &quot;King&quot; Kong`</span>: <span class="hljs-string">&quot;Slim Pickens&quot;</span>,
},
Oscars: []<span class="hljs-typename">string</span>{
<span class="hljs-string">&quot;Best Actor (Nomin.)&quot;</span>,
<span class="hljs-string">&quot;Best Adapted Screenplay (Nomin.)&quot;</span>,
<span class="hljs-string">&quot;Best Director (Nomin.)&quot;</span>,
<span class="hljs-string">&quot;Best Picture (Nomin.)&quot;</span>,
},
}
</code></pre>
<p>Display(&quot;strangelove&quot;, strangelove)&#x8ABF;&#x7528;&#x5C07;&#x986F;&#x793A;&#xFF08;strangelove&#x96FB;&#x5F71;&#x5C0D;&#x61C9;&#x7684;&#x4E2D;&#x6587;&#x540D;&#x662F;&#x300A;&#x5947;&#x611B;&#x535A;&#x58EB;&#x300B;&#xFF09;&#xFF1A;</p>
<pre><code class="lang-Go">Display strangelove (display.Movie):
strangelove.Title = <span class="hljs-string">&quot;Dr. Strangelove&quot;</span>
strangelove.Subtitle = <span class="hljs-string">&quot;How I Learned to Stop Worrying and Love the Bomb&quot;</span>
strangelove.Year = <span class="hljs-number">1964</span>
strangelove.Color = <span class="hljs-constant">false</span>
strangelove.Actor[<span class="hljs-string">&quot;Gen. Buck Turgidson&quot;</span>] = <span class="hljs-string">&quot;George C. Scott&quot;</span>
strangelove.Actor[<span class="hljs-string">&quot;Brig. Gen. Jack D. Ripper&quot;</span>] = <span class="hljs-string">&quot;Sterling Hayden&quot;</span>
strangelove.Actor[<span class="hljs-string">&quot;Maj. T.J. \&quot;King\&quot; Kong&quot;</span>] = <span class="hljs-string">&quot;Slim Pickens&quot;</span>
strangelove.Actor[<span class="hljs-string">&quot;Dr. Strangelove&quot;</span>] = <span class="hljs-string">&quot;Peter Sellers&quot;</span>
strangelove.Actor[<span class="hljs-string">&quot;Grp. Capt. Lionel Mandrake&quot;</span>] = <span class="hljs-string">&quot;Peter Sellers&quot;</span>
strangelove.Actor[<span class="hljs-string">&quot;Pres. Merkin Muffley&quot;</span>] = <span class="hljs-string">&quot;Peter Sellers&quot;</span>
strangelove.Oscars[<span class="hljs-number">0</span>] = <span class="hljs-string">&quot;Best Actor (Nomin.)&quot;</span>
strangelove.Oscars[<span class="hljs-number">1</span>] = <span class="hljs-string">&quot;Best Adapted Screenplay (Nomin.)&quot;</span>
strangelove.Oscars[<span class="hljs-number">2</span>] = <span class="hljs-string">&quot;Best Director (Nomin.)&quot;</span>
strangelove.Oscars[<span class="hljs-number">3</span>] = <span class="hljs-string">&quot;Best Picture (Nomin.)&quot;</span>
strangelove.Sequel = <span class="hljs-constant">nil</span>
</code></pre>
<p>&#x6211;&#x5011;&#x4E5F;&#x53EF;&#x4EE5;&#x4F7F;&#x7528;Display&#x51FD;&#x6578;&#x4F86;&#x986F;&#x793A;&#x6A19;&#x6E96;&#x5EAB;&#x4E2D;&#x985E;&#x578B;&#x7684;&#x5167;&#x90E8;&#x7D50;&#x69CB;&#xFF0C;&#x4F8B;&#x5982;<code>*os.File</code>&#x985E;&#x578B;&#xFF1A;</p>
<pre><code class="lang-Go">Display(<span class="hljs-string">&quot;os.Stderr&quot;</span>, os.Stderr)
<span class="hljs-comment">// Output:</span>
<span class="hljs-comment">// Display os.Stderr (*os.File):</span>
<span class="hljs-comment">// (*(*os.Stderr).file).fd = 2</span>
<span class="hljs-comment">// (*(*os.Stderr).file).name = &quot;/dev/stderr&quot;</span>
<span class="hljs-comment">// (*(*os.Stderr).file).nepipe = 0</span>
</code></pre>
<p>&#x8981;&#x8A3B;&#x610F;&#x7684;&#x662F;&#xFF0C;&#x7D50;&#x69CB;&#x9AD4;&#x4E2D;&#x672A;&#x5C0E;&#x51FA;&#x7684;&#x6210;&#x54E1;&#x5C0D;&#x53CD;&#x5C04;&#x4E5F;&#x662F;&#x53EF;&#x898B;&#x7684;&#x3002;&#x9700;&#x8981;&#x7576;&#x5FC3;&#x7684;&#x662F;&#x9019;&#x500B;&#x4F8B;&#x5B50;&#x7684;&#x8F38;&#x51FA;&#x5728;&#x4E0D;&#x540C;&#x64CD;&#x4F5C;&#x7E6B;&#x7D71;&#x4E0A;&#x53EF;&#x80FD;&#x662F;&#x4E0D;&#x540C;&#x7684;&#xFF0C;&#x4F75;&#x4E14;&#x96A8;&#x7740;&#x6A19;&#x6E96;&#x5EAB;&#x7684;&#x767C;&#x5C55;&#x4E5F;&#x53EF;&#x80FD;&#x5C0E;&#x81F4;&#x7D50;&#x679C;&#x4E0D;&#x540C;&#x3002;&#xFF08;&#x9019;&#x4E5F;&#x662F;&#x5C07;&#x9019;&#x4E9B;&#x6210;&#x54E1;&#x5B9A;&#x7FA9;&#x7232;&#x79C1;&#x6709;&#x6210;&#x54E1;&#x7684;&#x539F;&#x56E0;&#x4E4B;&#x4E00;&#xFF01;&#xFF09;&#x6211;&#x5011;&#x6DF1;&#x5733;&#x53EF;&#x4EE5;&#x7528;Display&#x51FD;&#x6578;&#x4F86;&#x986F;&#x793A;reflect.Value&#xFF0C;&#x4F86;&#x67FB;&#x770B;<code>*os.File</code>&#x985E;&#x578B;&#x7684;&#x5167;&#x90E8;&#x8868;&#x793A;&#x65B9;&#x5F0F;&#x3002;<code>Display(&quot;rV&quot;, reflect.ValueOf(os.Stderr))</code>&#x8ABF;&#x7528;&#x7684;&#x8F38;&#x51FA;&#x5982;&#x4E0B;&#xFF0C;&#x7576;&#x7136;&#x4E0D;&#x540C;&#x74B0;&#x5883;&#x5F97;&#x5230;&#x7684;&#x7D50;&#x679C;&#x53EF;&#x80FD;&#x6709;&#x5DEE;&#x7570;&#xFF1A;</p>
<pre><code class="lang-Go">Display rV (reflect.Value):
(*rV.typ).size = <span class="hljs-number">8</span>
(*rV.typ).hash = <span class="hljs-number">871609668</span>
(*rV.typ).align = <span class="hljs-number">8</span>
(*rV.typ).fieldAlign = <span class="hljs-number">8</span>
(*rV.typ).kind = <span class="hljs-number">22</span>
(*(*rV.typ).<span class="hljs-typename">string</span>) = <span class="hljs-string">&quot;*os.File&quot;</span>
(*(*(*rV.typ).uncommonType).methods[<span class="hljs-number">0</span>].name) = <span class="hljs-string">&quot;Chdir&quot;</span>
(*(*(*(*rV.typ).uncommonType).methods[<span class="hljs-number">0</span>].mtyp).<span class="hljs-typename">string</span>) = <span class="hljs-string">&quot;func() error&quot;</span>
(*(*(*(*rV.typ).uncommonType).methods[<span class="hljs-number">0</span>].typ).<span class="hljs-typename">string</span>) = <span class="hljs-string">&quot;func(*os.File) error&quot;</span>
...
</code></pre>
<p>&#x89C0;&#x5BDF;&#x4E0B;&#x9762;&#x5169;&#x500B;&#x4F8B;&#x5B50;&#x7684;&#x5340;&#x522B;&#xFF1A;</p>
<pre><code class="lang-Go"><span class="hljs-keyword">var</span> i <span class="hljs-keyword">interface</span>{} = <span class="hljs-number">3</span>
Display(<span class="hljs-string">&quot;i&quot;</span>, i)
<span class="hljs-comment">// Output:</span>
<span class="hljs-comment">// Display i (int):</span>
<span class="hljs-comment">// i = 3</span>
Display(<span class="hljs-string">&quot;&amp;i&quot;</span>, &amp;i)
<span class="hljs-comment">// Output:</span>
<span class="hljs-comment">// Display &amp;i (*interface {}):</span>
<span class="hljs-comment">// (*&amp;i).type = int</span>
<span class="hljs-comment">// (*&amp;i).value = 3</span>
</code></pre>
<p>&#x5728;&#x7B2C;&#x4E00;&#x500B;&#x4F8B;&#x5B50;&#x4E2D;&#xFF0C;Display&#x51FD;&#x6578;&#x5C07;&#x8ABF;&#x7528;reflect.ValueOf(i)&#xFF0C;&#x5B83;&#x8FD4;&#x8FF4;&#x4E00;&#x500B;Int&#x985E;&#x578B;&#x7684;&#x503C;&#x3002;&#x6B63;&#x5982;&#x6211;&#x5011;&#x5728;12.2&#x7BC0;&#x4E2D;&#x63D0;&#x5230;&#x7684;&#xFF0C;reflect.ValueOf&#x7E3D;&#x662F;&#x8FD4;&#x8FF4;&#x4E00;&#x500B;&#x503C;&#x7684;&#x5177;&#x9AD4;&#x985E;&#x578B;&#xFF0C;&#x56E0;&#x7232;&#x5B83;&#x662F;&#x5F9E;&#x4E00;&#x500B;&#x63A5;&#x53E3;&#x503C;&#x63D0;&#x53D6;&#x7684;&#x5167;&#x5BB9;&#x3002;</p>
<p>&#x5728;&#x7B2C;&#x4E8C;&#x500B;&#x4F8B;&#x5B50;&#x4E2D;&#xFF0C;Display&#x51FD;&#x6578;&#x8ABF;&#x7528;&#x7684;&#x662F;reflect.ValueOf(&amp;i)&#xFF0C;&#x5B83;&#x8FD4;&#x8FF4;&#x4E00;&#x500B;&#x6307;&#x5411;i&#x7684;&#x6307;&#x91DD;&#xFF0C;&#x5C0D;&#x61C9;Ptr&#x985E;&#x578B;&#x3002;&#x5728;switch&#x7684;Ptr&#x5206;&#x652F;&#x4E2D;&#xFF0C;&#x901A;&#x904E;&#x8ABF;&#x7528;Elem&#x4F86;&#x8FD4;&#x8FF4;&#x9019;&#x500B;&#x503C;&#xFF0C;&#x8FD4;&#x8FF4;&#x4E00;&#x500B;Value&#x4F86;&#x8868;&#x793A;i&#xFF0C;&#x5C0D;&#x61C9;Interface&#x985E;&#x578B;&#x3002;&#x4E00;&#x500B;&#x9593;&#x63A5;&#x7372;&#x5F97;&#x7684;Value&#xFF0C;&#x5C31;&#x50CF;&#x9019;&#x4E00;&#x500B;&#xFF0C;&#x53EF;&#x80FD;&#x4EE3;&#x8868;&#x4EFB;&#x610F;&#x985E;&#x578B;&#x7684;&#x503C;&#xFF0C;&#x5305;&#x62EC;&#x63A5;&#x53E3;&#x985E;&#x578B;&#x3002;&#x5167;&#x90E8;&#x7684;display&#x51FD;&#x6578;&#x905E;&#x6B78;&#x8ABF;&#x7528;&#x81EA;&#x8EAB;&#xFF0C;&#x9019;&#x6B21;&#x5B83;&#x5C07;&#x6253;&#x5370;&#x63A5;&#x53E3;&#x7684;&#x52D5;&#x614B;&#x985E;&#x578B;&#x548C;&#x503C;&#x3002;</p>
<p>&#x76EE;&#x524D;&#x7684;&#x5BE6;&#x73FE;&#xFF0C;Display&#x5982;&#x679C;&#x986F;&#x793A;&#x4E00;&#x500B;&#x5E36;&#x74B0;&#x7684;&#x6578;&#x64DA;&#x7D50;&#x69CB;&#x5C07;&#x6703;&#x9677;&#x5165;&#x6B7B;&#x5FAA;&#x74B0;&#xFF0C;&#x4F8B;&#x5982;&#x9996;&#x4F4D;&#x9805;&#x93C8;&#x7684;&#x93C8;&#x8868;&#xFF1A;</p>
<pre><code class="lang-Go"><span class="hljs-comment">// a struct that points to itself</span>
<span class="hljs-keyword">type</span> Cycle <span class="hljs-keyword">struct</span>{ Value <span class="hljs-typename">int</span>; Tail *Cycle }
<span class="hljs-keyword">var</span> c Cycle
c = Cycle{<span class="hljs-number">42</span>, &amp;c}
Display(<span class="hljs-string">&quot;c&quot;</span>, c)
</code></pre>
<p>Display&#x6703;&#x6C38;&#x9060;&#x4E0D;&#x505C;&#x5730;&#x9032;&#x884C;&#x6DF1;&#x5EA6;&#x905E;&#x6B78;&#x6253;&#x5370;&#xFF1A;</p>
<pre><code class="lang-Go">Display c (display.Cycle):
c.Value = <span class="hljs-number">42</span>
(*c.Tail).Value = <span class="hljs-number">42</span>
(*(*c.Tail).Tail).Value = <span class="hljs-number">42</span>
(*(*(*c.Tail).Tail).Tail).Value = <span class="hljs-number">42</span>
...ad infinitum...
</code></pre>
<p>&#x8A31;&#x591A;Go&#x8A9E;&#x8A00;&#x7A0B;&#x5E8F;&#x90FD;&#x5305;&#x542B;&#x4E86;&#x4E00;&#x4E9B;&#x5FAA;&#x74B0;&#x7684;&#x6578;&#x64DA;&#x7D50;&#x679C;&#x3002;Display&#x652F;&#x6301;&#x9019;&#x985E;&#x5E36;&#x74B0;&#x7684;&#x6578;&#x64DA;&#x7D50;&#x69CB;&#x662F;&#x6BD4;&#x8F03;&#x68D8;&#x624B;&#x7684;&#xFF0C;&#x9700;&#x8981;&#x589E;&#x52A0;&#x4E00;&#x500B;&#x984D;&#x5916;&#x7684;&#x8A18;&#x9332;&#x8A2A;&#x554F;&#x7684;&#x8DEF;&#x5F91;&#xFF1B;&#x4EE3;&#x50F9;&#x662F;&#x6602;&#x8CB4;&#x7684;&#x3002;&#x4E00;&#x822C;&#x7684;&#x89E3;&#x6C7A;&#x65B9;&#x6848;&#x662F;&#x91C7;&#x7528;&#x4E0D;&#x5B89;&#x5168;&#x7684;&#x8A9E;&#x8A00;&#x7279;&#x6027;&#xFF0C;&#x6211;&#x5011;&#x5C07;&#x5728;13.3&#x7BC0;&#x770B;&#x5230;&#x5177;&#x9AD4;&#x7684;&#x89E3;&#x6C7A;&#x65B9;&#x6848;&#x3002;</p>
<p>&#x5E36;&#x74B0;&#x7684;&#x6578;&#x64DA;&#x7D50;&#x69CB;&#x5F88;&#x5C11;&#x6703;&#x5C0D;fmt.Sprint&#x51FD;&#x6578;&#x9020;&#x6210;&#x554F;&#x984C;&#xFF0C;&#x56E0;&#x7232;&#x5B83;&#x5F88;&#x5C11;&#x5690;&#x8A66;&#x6253;&#x5370;&#x5B8C;&#x6574;&#x7684;&#x6578;&#x64DA;&#x7D50;&#x69CB;&#x3002;&#x4F8B;&#x5982;&#xFF0C;&#x7576;&#x5B83;&#x9047;&#x5230;&#x4E00;&#x500B;&#x6307;&#x91DD;&#x7684;&#x6642;&#x5019;&#xFF0C;&#x5B83;&#x96BB;&#x662F;&#x7C21;&#x55AE;&#x7B2C;&#x6253;&#x5370;&#x6307;&#x91DD;&#x7684;&#x6578;&#x503C;&#x3002;&#x96D6;&#x7136;&#xFF0C;&#x5728;&#x6253;&#x5370;&#x5305;&#x542B;&#x81EA;&#x8EAB;&#x7684;slice&#x6216;map&#x6642;&#x53EF;&#x80FD;&#x9047;&#x5230;&#x774F;&#x96E3;&#xFF0C;&#x4F46;&#x662F;&#x4E0D;&#x4FDD;&#x8B49;&#x8655;&#x7406;&#x9019;&#x7A2E;&#x662F;&#x7F55;&#x898B;&#x60C5;&#x6CC1;&#x537B;&#x53EF;&#x4EE5;&#x907F;&#x514D;&#x984D;&#x5916;&#x7684;&#x9EBB;&#x7169;&#x3002;</p>
<p><strong>&#x7DF4;&#x7FD2; 12.1&#xFF1A;</strong> &#x64F4;&#x5C55;Displayhans&#xFF0C;&#x4EE5;&#x4FBF;&#x5B83;&#x53EF;&#x4EE5;&#x986F;&#x793A;&#x5305;&#x542B;&#x4EE5;&#x7D50;&#x69CB;&#x9AD4;&#x6216;&#x6578;&#x7D44;&#x4F5C;&#x7232;map&#x7684;key&#x985E;&#x578B;&#x7684;&#x503C;&#x3002;</p>
<p><strong>&#x7DF4;&#x7FD2; 12.2&#xFF1A;</strong> &#x589E;&#x5F37;display&#x51FD;&#x6578;&#x7684;&#x7A69;&#x5065;&#x6027;&#xFF0C;&#x901A;&#x904E;&#x8A18;&#x9332;&#x908A;&#x754C;&#x7684;&#x6B65;&#x6578;&#x4F86;&#x78BA;&#x4FDD;&#x5728;&#x8D85;&#x51FA;&#x4E00;&#x5B9A;&#x9650;&#x88FD;&#x524D;&#x653E;&#x68C4;&#x905E;&#x6B78;&#x3002;&#xFF08;&#x5728;13.3&#x7BC0;&#xFF0C;&#x6211;&#x5011;&#x6703;&#x770B;&#x5230;&#x53E6;&#x4E00;&#x7A2E;&#x63A2;&#x6E2C;&#x6578;&#x64DA;&#x7D50;&#x69CB;&#x662F;&#x5426;&#x5B58;&#x5728;&#x74B0;&#x7684;&#x6280;&#x8853;&#x3002;&#xFF09;</p>
</section>
</div>
</div>
</div>
<a href="../ch12/ch12-02.html" class="navigation navigation-prev " aria-label="Previous page: reflect.Type和reflect.Value"><i class="fa fa-angle-left"></i></a>
<a href="../ch12/ch12-04.html" class="navigation navigation-next " aria-label="Next page: 示例: 編碼S表達式"><i class="fa fa-angle-right"></i></a>
</div>
</div>
<script src="../gitbook/app.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 = {"katex":{},"highlight":{},"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>