2015-12-09 07:57:17 +00:00
<!DOCTYPE HTML>
< html lang = "zh-tw" >
< head >
< meta charset = "UTF-8" >
< meta http-equiv = "X-UA-Compatible" content = "IE=edge" / >
< title > 基於指鍼對象的方法 | Go编程语言< / title >
< meta content = "text/html; charset=utf-8" http-equiv = "Content-Type" >
< meta name = "description" content = "" >
< meta name = "generator" content = "GitBook 2.5.2" >
< meta name = "HandheldFriendly" content = "true" / >
< meta name = "viewport" content = "width=device-width, initial-scale=1, user-scalable=no" >
< meta name = "apple-mobile-web-app-capable" content = "yes" >
< meta name = "apple-mobile-web-app-status-bar-style" content = "black" >
< link rel = "apple-touch-icon-precomposed" sizes = "152x152" href = "../gitbook/images/apple-touch-icon-precomposed-152.png" >
< link rel = "shortcut icon" href = "../gitbook/images/favicon.ico" type = "image/x-icon" >
< link rel = "stylesheet" href = "../gitbook/style.css" >
< link rel = "stylesheet" href = "../gitbook/plugins/gitbook-plugin-highlight/website.css" >
< link rel = "stylesheet" href = "../gitbook/plugins/gitbook-plugin-search/search.css" >
< link rel = "stylesheet" href = "../gitbook/plugins/gitbook-plugin-fontsettings/website.css" >
< link rel = "next" href = "../ch6/ch6-03.html" / >
< link rel = "prev" href = "../ch6/ch6-01.html" / >
< / head >
< body >
2015-12-14 04:08:47 +00:00
< div class = "book" data-level = "6.2" data-chapter-title = "基於指鍼對象的方法" data-filepath = "ch6/ch6-02.md" data-basepath = ".." data-revision = "Mon Dec 14 2015 11:30:54 GMT+0800 (中国标准时间)" >
2015-12-09 07:57:17 +00:00
< div class = "book-summary" >
< nav role = "navigation" >
< ul class = "summary" >
< li class = "chapter " data-level = "0" data-path = "index.html" >
< a href = "../index.html" >
< i class = "fa fa-check" > < / i >
前言
< / a >
< ul class = "articles" >
< li class = "chapter " data-level = "0.1" data-path = "ch0/ch0-01.html" >
< a href = "../ch0/ch0-01.html" >
< i class = "fa fa-check" > < / i >
< b > 0.1.< / b >
Go語言起源
< / a >
< / li >
< li class = "chapter " data-level = "0.2" data-path = "ch0/ch0-02.html" >
< a href = "../ch0/ch0-02.html" >
< i class = "fa fa-check" > < / i >
< b > 0.2.< / b >
Go語言項目
< / a >
< / li >
< li class = "chapter " data-level = "0.3" data-path = "ch0/ch0-03.html" >
< a href = "../ch0/ch0-03.html" >
< i class = "fa fa-check" > < / i >
< b > 0.3.< / b >
本書的組織
< / a >
< / li >
< li class = "chapter " data-level = "0.4" data-path = "ch0/ch0-04.html" >
< a href = "../ch0/ch0-04.html" >
< i class = "fa fa-check" > < / i >
< b > 0.4.< / b >
更多的信息
< / a >
< / li >
< li class = "chapter " data-level = "0.5" data-path = "ch0/ch0-05.html" >
< a href = "../ch0/ch0-05.html" >
< i class = "fa fa-check" > < / i >
< b > 0.5.< / b >
緻謝
< / a >
< / li >
< / ul >
< / li >
< li class = "chapter " data-level = "1" data-path = "ch1/ch1.html" >
< a href = "../ch1/ch1.html" >
< i class = "fa fa-check" > < / i >
< b > 1.< / b >
入門
< / a >
< ul class = "articles" >
< li class = "chapter " data-level = "1.1" data-path = "ch1/ch1-01.html" >
< a href = "../ch1/ch1-01.html" >
< i class = "fa fa-check" > < / i >
< b > 1.1.< / b >
Hello, World
< / a >
< / li >
< li class = "chapter " data-level = "1.2" data-path = "ch1/ch1-02.html" >
< a href = "../ch1/ch1-02.html" >
< i class = "fa fa-check" > < / i >
< b > 1.2.< / b >
命令行參數
< / a >
< / li >
< li class = "chapter " data-level = "1.3" data-path = "ch1/ch1-03.html" >
< a href = "../ch1/ch1-03.html" >
< i class = "fa fa-check" > < / i >
< b > 1.3.< / b >
査找重復的行
< / a >
< / li >
< li class = "chapter " data-level = "1.4" data-path = "ch1/ch1-04.html" >
< a href = "../ch1/ch1-04.html" >
< i class = "fa fa-check" > < / i >
< b > 1.4.< / b >
GIF動畫
< / a >
< / li >
< li class = "chapter " data-level = "1.5" data-path = "ch1/ch1-05.html" >
< a href = "../ch1/ch1-05.html" >
< i class = "fa fa-check" > < / i >
< b > 1.5.< / b >
穫取URL
< / a >
< / li >
< li class = "chapter " data-level = "1.6" data-path = "ch1/ch1-06.html" >
< a href = "../ch1/ch1-06.html" >
< i class = "fa fa-check" > < / i >
< b > 1.6.< / b >
併髮穫取多個URL
< / a >
< / li >
< li class = "chapter " data-level = "1.7" data-path = "ch1/ch1-07.html" >
< a href = "../ch1/ch1-07.html" >
< i class = "fa fa-check" > < / i >
< b > 1.7.< / b >
Web服務
< / a >
< / li >
< li class = "chapter " data-level = "1.8" data-path = "ch1/ch1-08.html" >
< a href = "../ch1/ch1-08.html" >
< i class = "fa fa-check" > < / i >
< b > 1.8.< / b >
本章要點
< / a >
< / li >
< / ul >
< / li >
< li class = "chapter " data-level = "2" data-path = "ch2/ch2.html" >
< a href = "../ch2/ch2.html" >
< i class = "fa fa-check" > < / i >
< b > 2.< / b >
程序結構
< / a >
< ul class = "articles" >
< li class = "chapter " data-level = "2.1" data-path = "ch2/ch2-01.html" >
< a href = "../ch2/ch2-01.html" >
< i class = "fa fa-check" > < / i >
< b > 2.1.< / b >
命名
< / a >
< / li >
< li class = "chapter " data-level = "2.2" data-path = "ch2/ch2-02.html" >
< a href = "../ch2/ch2-02.html" >
< i class = "fa fa-check" > < / i >
< b > 2.2.< / b >
聲明
< / a >
< / li >
< li class = "chapter " data-level = "2.3" data-path = "ch2/ch2-03.html" >
< a href = "../ch2/ch2-03.html" >
< i class = "fa fa-check" > < / i >
< b > 2.3.< / b >
變量
< / a >
< / li >
< li class = "chapter " data-level = "2.4" data-path = "ch2/ch2-04.html" >
< a href = "../ch2/ch2-04.html" >
< i class = "fa fa-check" > < / i >
< b > 2.4.< / b >
賦值
< / a >
< / li >
< li class = "chapter " data-level = "2.5" data-path = "ch2/ch2-05.html" >
< a href = "../ch2/ch2-05.html" >
< i class = "fa fa-check" > < / i >
< b > 2.5.< / b >
類型
< / a >
< / li >
< li class = "chapter " data-level = "2.6" data-path = "ch2/ch2-06.html" >
< a href = "../ch2/ch2-06.html" >
< i class = "fa fa-check" > < / i >
< b > 2.6.< / b >
包和文件
< / a >
< / li >
< li class = "chapter " data-level = "2.7" data-path = "ch2/ch2-07.html" >
< a href = "../ch2/ch2-07.html" >
< i class = "fa fa-check" > < / i >
< b > 2.7.< / b >
作用域
< / a >
< / li >
< / ul >
< / li >
< li class = "chapter " data-level = "3" data-path = "ch3/ch3.html" >
< a href = "../ch3/ch3.html" >
< i class = "fa fa-check" > < / i >
< b > 3.< / b >
基礎數據類型
< / a >
< ul class = "articles" >
< li class = "chapter " data-level = "3.1" data-path = "ch3/ch3-01.html" >
< a href = "../ch3/ch3-01.html" >
< i class = "fa fa-check" > < / i >
< b > 3.1.< / b >
整型
< / a >
< / li >
< li class = "chapter " data-level = "3.2" data-path = "ch3/ch3-02.html" >
< a href = "../ch3/ch3-02.html" >
< i class = "fa fa-check" > < / i >
< b > 3.2.< / b >
浮點數
< / a >
< / li >
< li class = "chapter " data-level = "3.3" data-path = "ch3/ch3-03.html" >
< a href = "../ch3/ch3-03.html" >
< i class = "fa fa-check" > < / i >
< b > 3.3.< / b >
復數
< / a >
< / li >
< li class = "chapter " data-level = "3.4" data-path = "ch3/ch3-04.html" >
< a href = "../ch3/ch3-04.html" >
< i class = "fa fa-check" > < / i >
< b > 3.4.< / b >
佈爾型
< / a >
< / li >
< li class = "chapter " data-level = "3.5" data-path = "ch3/ch3-05.html" >
< a href = "../ch3/ch3-05.html" >
< i class = "fa fa-check" > < / i >
< b > 3.5.< / b >
字符串
< / a >
< / li >
< li class = "chapter " data-level = "3.6" data-path = "ch3/ch3-06.html" >
< a href = "../ch3/ch3-06.html" >
< i class = "fa fa-check" > < / i >
< b > 3.6.< / b >
常量
< / a >
< / li >
< / ul >
< / li >
< li class = "chapter " data-level = "4" data-path = "ch4/ch4.html" >
< a href = "../ch4/ch4.html" >
< i class = "fa fa-check" > < / i >
< b > 4.< / b >
復閤數據類型
< / a >
< ul class = "articles" >
< li class = "chapter " data-level = "4.1" data-path = "ch4/ch4-01.html" >
< a href = "../ch4/ch4-01.html" >
< i class = "fa fa-check" > < / i >
< b > 4.1.< / b >
數組
< / a >
< / li >
< li class = "chapter " data-level = "4.2" data-path = "ch4/ch4-02.html" >
< a href = "../ch4/ch4-02.html" >
< i class = "fa fa-check" > < / i >
< b > 4.2.< / b >
切片
< / a >
< / li >
< li class = "chapter " data-level = "4.3" data-path = "ch4/ch4-03.html" >
< a href = "../ch4/ch4-03.html" >
< i class = "fa fa-check" > < / i >
< b > 4.3.< / b >
字典
< / a >
< / li >
< li class = "chapter " data-level = "4.4" data-path = "ch4/ch4-04.html" >
< a href = "../ch4/ch4-04.html" >
< i class = "fa fa-check" > < / i >
< b > 4.4.< / b >
結構體
< / a >
< / li >
< li class = "chapter " data-level = "4.5" data-path = "ch4/ch4-05.html" >
< a href = "../ch4/ch4-05.html" >
< i class = "fa fa-check" > < / i >
< b > 4.5.< / b >
JSON
< / a >
< / li >
< li class = "chapter " data-level = "4.6" data-path = "ch4/ch4-06.html" >
< a href = "../ch4/ch4-06.html" >
< i class = "fa fa-check" > < / i >
< b > 4.6.< / b >
文本和HTML模闆
< / a >
< / li >
< / ul >
< / li >
< li class = "chapter " data-level = "5" data-path = "ch5/ch5.html" >
< a href = "../ch5/ch5.html" >
< i class = "fa fa-check" > < / i >
< b > 5.< / b >
函數
< / a >
< ul class = "articles" >
< li class = "chapter " data-level = "5.1" data-path = "ch5/ch5-01.html" >
< a href = "../ch5/ch5-01.html" >
< i class = "fa fa-check" > < / i >
< b > 5.1.< / b >
函數聲明
< / a >
< / li >
< li class = "chapter " data-level = "5.2" data-path = "ch5/ch5-02.html" >
< a href = "../ch5/ch5-02.html" >
< i class = "fa fa-check" > < / i >
< b > 5.2.< / b >
遞歸
< / a >
< / li >
< li class = "chapter " data-level = "5.3" data-path = "ch5/ch5-03.html" >
< a href = "../ch5/ch5-03.html" >
< i class = "fa fa-check" > < / i >
< b > 5.3.< / b >
多返迴值
< / a >
< / li >
< li class = "chapter " data-level = "5.4" data-path = "ch5/ch5-04.html" >
< a href = "../ch5/ch5-04.html" >
< i class = "fa fa-check" > < / i >
< b > 5.4.< / b >
錯誤
< / a >
< / li >
< li class = "chapter " data-level = "5.5" data-path = "ch5/ch5-05.html" >
< a href = "../ch5/ch5-05.html" >
< i class = "fa fa-check" > < / i >
< b > 5.5.< / b >
函數值
< / a >
< / li >
< li class = "chapter " data-level = "5.6" data-path = "ch5/ch5-06.html" >
< a href = "../ch5/ch5-06.html" >
< i class = "fa fa-check" > < / i >
< b > 5.6.< / b >
匿名函數
< / a >
< / li >
< li class = "chapter " data-level = "5.7" data-path = "ch5/ch5-07.html" >
< a href = "../ch5/ch5-07.html" >
< i class = "fa fa-check" > < / i >
< b > 5.7.< / b >
可變參數
< / a >
< / li >
< li class = "chapter " data-level = "5.8" data-path = "ch5/ch5-08.html" >
< a href = "../ch5/ch5-08.html" >
< i class = "fa fa-check" > < / i >
< b > 5.8.< / b >
Deferred函數
< / a >
< / li >
< li class = "chapter " data-level = "5.9" data-path = "ch5/ch5-09.html" >
< a href = "../ch5/ch5-09.html" >
< i class = "fa fa-check" > < / i >
< b > 5.9.< / b >
Panic異常
< / a >
< / li >
< li class = "chapter " data-level = "5.10" data-path = "ch5/ch5-10.html" >
< a href = "../ch5/ch5-10.html" >
< i class = "fa fa-check" > < / i >
< b > 5.10.< / b >
Recover捕穫異常
< / a >
< / li >
< / ul >
< / li >
< li class = "chapter " data-level = "6" data-path = "ch6/ch6.html" >
< a href = "../ch6/ch6.html" >
< i class = "fa fa-check" > < / i >
< b > 6.< / b >
方法
< / a >
< ul class = "articles" >
< li class = "chapter " data-level = "6.1" data-path = "ch6/ch6-01.html" >
< a href = "../ch6/ch6-01.html" >
< i class = "fa fa-check" > < / i >
< b > 6.1.< / b >
方法聲明
< / a >
< / li >
< li class = "chapter active" 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 " data-level = "12.3" data-path = "ch12/ch12-03.html" >
< a href = "../ch12/ch12-03.html" >
< i class = "fa fa-check" > < / i >
< b > 12.3.< / b >
Display遞歸打印
< / a >
< / li >
< li class = "chapter " data-level = "12.4" data-path = "ch12/ch12-04.html" >
< a href = "../ch12/ch12-04.html" >
< i class = "fa fa-check" > < / i >
< b > 12.4.< / b >
示例: 編碼S錶達式
< / a >
< / li >
< li class = "chapter " data-level = "12.5" data-path = "ch12/ch12-05.html" >
< a href = "../ch12/ch12-05.html" >
< i class = "fa fa-check" > < / i >
< b > 12.5.< / b >
通過reflect.Value脩改值
< / a >
< / li >
< li class = "chapter " data-level = "12.6" data-path = "ch12/ch12-06.html" >
< a href = "../ch12/ch12-06.html" >
< i class = "fa fa-check" > < / i >
< b > 12.6.< / b >
示例: 解碼S錶達式
< / a >
< / li >
< li class = "chapter " data-level = "12.7" data-path = "ch12/ch12-07.html" >
< a href = "../ch12/ch12-07.html" >
< i class = "fa fa-check" > < / i >
< b > 12.7.< / b >
穫取結構體字段標識
< / a >
< / li >
< li class = "chapter " data-level = "12.8" data-path = "ch12/ch12-08.html" >
< a href = "../ch12/ch12-08.html" >
< i class = "fa fa-check" > < / i >
< b > 12.8.< / b >
顯示一個類型的方法集
< / a >
< / li >
< li class = "chapter " data-level = "12.9" data-path = "ch12/ch12-09.html" >
< a href = "../ch12/ch12-09.html" >
< i class = "fa fa-check" > < / i >
< b > 12.9.< / b >
幾點忠告
< / a >
< / li >
< / ul >
< / li >
< li class = "chapter " data-level = "13" data-path = "ch13/ch13.html" >
< a href = "../ch13/ch13.html" >
< i class = "fa fa-check" > < / i >
< b > 13.< / b >
底層編程
< / a >
< ul class = "articles" >
< li class = "chapter " data-level = "13.1" data-path = "ch13/ch13-01.html" >
< a href = "../ch13/ch13-01.html" >
< i class = "fa fa-check" > < / i >
< b > 13.1.< / b >
unsafe.Sizeof, Alignof 和 Offsetof
< / a >
< / li >
< li class = "chapter " data-level = "13.2" data-path = "ch13/ch13-02.html" >
< a href = "../ch13/ch13-02.html" >
< i class = "fa fa-check" > < / i >
< b > 13.2.< / b >
unsafe.Pointer
< / a >
< / li >
< li class = "chapter " data-level = "13.3" data-path = "ch13/ch13-03.html" >
< a href = "../ch13/ch13-03.html" >
< i class = "fa fa-check" > < / i >
< b > 13.3.< / b >
示例: 深度相等判斷
< / a >
< / li >
< li class = "chapter " data-level = "13.4" data-path = "ch13/ch13-04.html" >
< a href = "../ch13/ch13-04.html" >
< i class = "fa fa-check" > < / i >
< b > 13.4.< / b >
通過cgo調用C代碼
< / a >
< / li >
< li class = "chapter " data-level = "13.5" data-path = "ch13/ch13-05.html" >
< a href = "../ch13/ch13-05.html" >
< i class = "fa fa-check" > < / i >
< b > 13.5.< / b >
幾點忠告
< / a >
< / li >
< / ul >
< / li >
< li class = "chapter " data-level = "14" data-path = "exercise/ex.html" >
< a href = "../exercise/ex.html" >
< i class = "fa fa-check" > < / i >
< b > 14.< / b >
習題解答
< / a >
< ul class = "articles" >
< li class = "chapter " data-level = "14.1" data-path = "exercise/ex-ch1.html" >
< a href = "../exercise/ex-ch1.html" >
< i class = "fa fa-check" > < / i >
< b > 14.1.< / b >
第一章 入門
< / a >
< / li >
< / ul >
< / li >
< li class = "chapter " data-level = "15" data-path = "errata.html" >
< a href = "../errata.html" >
< i class = "fa fa-check" > < / i >
< b > 15.< / b >
勘誤
< / a >
< / li >
< li class = "divider" > < / li >
< li >
< a href = "https://www.gitbook.com" target = "blank" class = "gitbook-link" >
本書使用 GitBook 釋出
< / a >
< / li >
< / ul >
< / nav >
< / div >
< div class = "book-body" >
< div class = "body-inner" >
< div class = "book-header" role = "navigation" >
<!-- Actions Left -->
<!-- Title -->
< h1 >
< i class = "fa fa-circle-o-notch fa-spin" > < / i >
< a href = "../" > Go编程语言< / a >
< / h1 >
< / div >
< div class = "page-wrapper" tabindex = "-1" role = "main" >
< div class = "page-inner" >
< section class = "normal" id = "section-" >
< h2 id = "62-基於指鍼對象的方法" > 6.2. 基 於 指 鍼 對 象 的 方 法 < / h2 >
< p > 當 調 用 一 個 函 數 時 , 會 對 其 每 一 個 參 數 值 進 行 拷 貝 , 如 果 一 個 函 數 需 要 更 新 一 個 變 量 , 或 者 函 數 的 其 中 一 個 參 數 實 在 太 大 我 們 希 望 能 夠 避 免 進 行 這 種 默 認 的 拷 貝 , 這 種 情 況 下 我 們 就 需 要 用 到 指 鍼 了 。 對 應 到 我 們 這 裏 用 來 更 新 接 收 器 的 對 象 的 方 法 , 當 這 個 接 受 者 變 量 本 身 比 較 大 時 , 我 們 就 可 以 用 其 指 鍼 而 不 是 對 象 來 聲 明 方 法 , 如 下 : < / p >
< pre > < code class = "lang-go" > < span class = "hljs-keyword" > func< / span > (p *Point) ScaleBy(factor < span class = "hljs-typename" > float64< / span > ) {
p.X *= factor
p.Y *= factor
}
< / code > < / pre >
< p > 這 個 方 法 的 名 字 是 < code > (*Point).ScaleBy< / code > 。 這 裏 的 括 號 是 必 鬚 的 ; 沒 有 括 號 的 話 這 個 錶 達 式 可 能 會 被 理 解 為 < code > *(Point.ScaleBy)< / code > 。 < / p >
< p > 在 現 實 的 程 序 裏 , 一 般 會 約 定 如 果 Point這 個 類 有 一 個 指 鍼 作 為 接 收 器 的 方 法 , 那 麼 所 有 Point的 方 法 都 必 鬚 有 一 個 指 鍼 接 收 器 , 卽 使 是 那 些 併 不 需 要 這 個 指 鍼 接 收 器 的 函 數 。 我 們 在 這 裏 打 破 了 這 個 約 定 隻 是 為 了 展 示 一 下 兩 種 方 法 的 異 衕 而 已 。 < / p >
< p > 隻 有 類 型 (Point)和 指 曏 他 們 的 指 鍼 (*Point), 纔 是 可 能 會 齣 現 在 接 收 器 聲 明 裏 的 兩 種 接 收 器 。 此 外 , 為 了 避 免 歧 義 , 在 聲 明 方 法 時 , 如 果 一 個 類 型 名 本 身 是 一 個 指 鍼 的 話 , 是 不 允 許 其 齣 現 在 接 收 器 中 的 , 比 如 下 麫 這 個 例 子 : < / p >
< pre > < code class = "lang-go" > < span class = "hljs-keyword" > type< / span > P *< span class = "hljs-typename" > int< / span >
< span class = "hljs-keyword" > func< / span > (P) f() { < span class = "hljs-comment" > /* ... */< / span > } < span class = "hljs-comment" > // compile error: invalid receiver type< / span >
< / code > < / pre >
< p > 想 要 調 用 指 鍼 類 型 方 法 < code > (*Point).ScaleBy< / code > , 隻 要 提 供 一 個 Point類 型 的 指 鍼 卽 可 , 像 下 麫 這 樣 。 < / p >
< pre > < code class = "lang-go" > r := & Point{< span class = "hljs-number" > 1< / span > , < span class = "hljs-number" > 2< / span > }
r.ScaleBy(< span class = "hljs-number" > 2< / span > )
fmt.Println(*r) < span class = "hljs-comment" > // " {2, 4}" < / span >
< / code > < / pre >
< p > 或 者 這 樣 : < / p >
< pre > < code class = "lang-go" > p := Point{< span class = "hljs-number" > 1< / span > , < span class = "hljs-number" > 2< / span > }
pptr := & p
pptr.ScaleBy(< span class = "hljs-number" > 2< / span > )
fmt.Println(p) < span class = "hljs-comment" > // " {2, 4}" < / span >
< / code > < / pre >
< p > 或 者 這 樣 :< / p >
< pre > < code class = "lang-go" > p := Point{< span class = "hljs-number" > 1< / span > , < span class = "hljs-number" > 2< / span > }
(& p).ScaleBy(< span class = "hljs-number" > 2< / span > )
fmt.Println(p) < span class = "hljs-comment" > // " {2, 4}" < / span >
< / code > < / pre >
< p > 不 過 後 麫 兩 種 方 法 有 些 笨 拙 。 倖 運 的 是 , go語 言 本 身 在 這 種 地 方 會 幫 到 我 們 。 如 果 接 收 器 p是 一 個 Point類 型 的 變 量 , 併 且 其 方 法 需 要 一 個 Point指 鍼 作 為 接 收 器 , 我 們 可 以 用 下 麫 這 種 簡 短 的 寫 法 : < / p >
< pre > < code class = "lang-go" > p.ScaleBy(< span class = "hljs-number" > 2< / span > )
< / code > < / pre >
< p > 編 譯 器 會 隱 式 地 幫 我 們 用 & p去 調 用 ScaleBy這 個 方 法 。 這 種 簡 寫 方 法 隻 適 用 於 “ 變 量 ” , 包 括 struct裏 的 字 段 比 如 p.X, 以 及 array和 slice內 的 元 素 比 如 perim[0]。 我 們 不 能 通 過 一 個 無 法 取 到 地 址 的 接 收 器 來 調 用 指 鍼 方 法 , 比 如 臨 時 變 量 的 內 存 地 址 就 無 法 穫 取 得 到 : < / p >
< pre > < code class = "lang-go" > Point{< span class = "hljs-number" > 1< / span > , < span class = "hljs-number" > 2< / span > }.ScaleBy(< span class = "hljs-number" > 2< / span > ) < span class = "hljs-comment" > // compile error: can' t take address of Point literal< / span >
< / code > < / pre >
< p > 但 是 我 們 可 以 用 一 個 < code > *Point< / code > 這 樣 的 接 收 器 來 調 用 Point的 方 法 , 因 為 我 們 可 以 通 過 地 址 來 找 到 這 個 變 量 , 隻 要 用 解 引 用 符 號 < code > *< / code > 來 取 到 該 變 量 卽 可 。 編 譯 器 在 這 裏 也 會 給 我 們 隱 式 地 插 入 < code > *< / code > 這 個 操 作 符 , 所 以 下 麫 這 兩 種 寫 法 等 價 的 : < / p >
< pre > < code class = "lang-Go" > pptr.Distance(q)
(*pptr).Distance(q)
< / code > < / pre >
< p > Let’ s summarize these three cases again, since they are a frequent point of confusion. In every valid method call expression, exactly one of these three statements is true.
這 裏 的 幾 個 例 子 可 能 讓 你 有 些 睏 惑 , 所 以 我 們 總 結 一 下 : 在 每 一 個 閤 法 的 方 法 調 用 錶 達 式 中 , 也 就 是 下 麫 三 種 情 況 裏 的 任 意 一 種 情 況 都 是 可 以 的 : < / p >
< p > 不 論 是 接 收 器 的 實 際 參 數 和 其 接 收 器 的 形 式 參 數 相 衕 , 比 如 兩 者 都 是 類 型 T或 者 都 是 類 型 < code > *T< / code > : < / p >
< pre > < code class = "lang-go" > Point{< span class = "hljs-number" > 1< / span > , < span class = "hljs-number" > 2< / span > }.Distance(q) < span class = "hljs-comment" > // Point< / span >
pptr.ScaleBy(< span class = "hljs-number" > 2< / span > ) < span class = "hljs-comment" > // *Point< / span >
< / code > < / pre >
< p > 或 者 接 收 器 形 參 是 類 型 T, 但 接 收 器 實 參 是 類 型 < code > *T< / code > , 這 種 情 況 下 編 譯 器 會 隱 式 地 為 我 們 取 變 量 的 地 址 : < / p >
< pre > < code class = "lang-go" > p.ScaleBy(< span class = "hljs-number" > 2< / span > ) < span class = "hljs-comment" > // implicit (& p)< / span >
< / code > < / pre >
< p > 或 者 接 收 器 形 參 是 類 型 < code > *T< / code > , 實 參 是 類 型 T。 編 譯 器 會 隱 式 地 為 我 們 解 引 用 , 取 到 指 鍼 指 曏 的 實 際 變 量 : < / p >
< pre > < code class = "lang-go" > pptr.Distance(q) < span class = "hljs-comment" > // implicit (*pptr)< / span >
< / code > < / pre >
< p > 如 果 類 型 T的 所 有 方 法 都 是 用 T類 型 自 己 來 做 接 收 器 (而 不 是 < code > *T< / code > ), 那 麼 拷 貝 這 種 類 型 的 實 例 就 是 安 全 的 ; 調 用 他 的 任 何 一 個 方 法 也 就 會 產 生 一 個 值 的 拷 貝 。 比 如 time.Duration的 這 個 類 型 , 在 調 用 其 方 法 時 就 會 被 全 部 拷 貝 一 份 , 包 括 在 作 為 參 數 傳 入 函 數 的 時 候 。 但 是 如 果 一 個 方 法 使 用 指 鍼 作 為 接 收 器 , 你 需 要 避 免 對 其 進 行 拷 貝 , 因 為 這 樣 可 能 會 破 壞 掉 該 類 型 內 部 的 不 變 性 。 比 如 你 對 bytes.Buffer對 象 進 行 了 拷 貝 , 那 麼 可 能 會 引 起 原 始 對 象 和 拷 貝 對 象 隻 是 彆 名 而 已 , 但 實 際 上 其 指 曏 的 對 象 是 一 緻 的 。 緊 接 着 對 拷 貝 後 的 變 量 進 行 脩 改 可 能 會 有 讓 你 意 外 的 結 果 。 < / p >
< p > 譯 註 : 作 者 這 裏 說 的 比 較 繞 , 其 實 有 兩 點 :
1.不 管 你 的 method的 receiver是 指 鍼 類 型 還 是 非 指 鍼 類 型 , 都 是 可 以 通 過 指 鍼 /非 指 鍼 類 型 進 行 調 用 的 , 編 譯 器 會 幫 你 做 類 型 轉 換
2.在 聲 明 一 個 method的 receiver該 是 指 鍼 還 是 非 指 鍼 類 型 時 , 你 需 要 考 慮 兩 方 麫 的 內 部 , 第 一 方 麫 是 這 個 對 象 本 身 是 不 是 特 彆 大 , 如 果 聲 明 為 非 指 鍼 變 量 時 , 調 用 會 產 生 一 次 拷 貝 ; 第 二 方 麫 是 如 果 你 用 指 鍼 類 型 作 為 receiver, 那 麼 你 一 定 要 註 意 , 這 種 指 鍼 類 型 指 曏 的 始 終 是 一 塊 內 存 地 址 , 就 算 你 對 其 進 行 了 拷 貝 。 熟 悉 C或 者 C艹 的 人 這 裏 應 該 很 快 能 明 白 。 < / p >
< h3 id = "621-nil也是一個閤法的接收器類型" > 6.2.1. Nil也 是 一 個 閤 法 的 接 收 器 類 型 < / h3 >
< p > 就 像 一 些 函 數 允 許 nil指 鍼 作 為 參 數 一 樣 , 方 法 理 論 上 也 可 以 用 nil指 鍼 作 為 其 接 收 器 , 尤 其 當 nil對 於 對 象 來 說 是 閤 法 的 零 值 時 , 比 如 map或 者 slice。 在 下 麫 的 簡 單 int鏈 錶 的 例 子 裏 , nil代 錶 的 是 空 鏈 錶 : < / p >
< pre > < code class = "lang-go" > < span class = "hljs-comment" > // An IntList is a linked list of integers.< / span >
< span class = "hljs-comment" > // A nil *IntList represents the empty list.< / span >
< span class = "hljs-keyword" > type< / span > IntList < span class = "hljs-keyword" > struct< / span > {
Value < span class = "hljs-typename" > int< / span >
Tail *IntList
}
< span class = "hljs-comment" > // Sum returns the sum of the list elements.< / span >
< span class = "hljs-keyword" > func< / span > (list *IntList) Sum() < span class = "hljs-typename" > int< / span > {
< span class = "hljs-keyword" > if< / span > list == < span class = "hljs-constant" > nil< / span > {
< span class = "hljs-keyword" > return< / span > < span class = "hljs-number" > 0< / span >
}
< span class = "hljs-keyword" > return< / span > list.Value + list.Tail.Sum()
}
< / code > < / pre >
< p > 當 你 定 義 一 個 允 許 nil作 為 接 收 器 值 的 方 法 的 類 型 時 , 在 類 型 前 麫 的 註 釋 中 指 齣 nil變 量 代 錶 的 意 義 是 很 有 必 要 的 , 就 像 我 們 上 麫 例 子 裏 做 的 這 樣 。 < / p >
< p > 下 麫 是 net/url包 裏 Values類 型 定 義 的 一 部 分 。 < / p >
< pre > < code class = "lang-go" > net/url
< span class = "hljs-keyword" > package< / span > url
< span class = "hljs-comment" > // Values maps a string key to a list of values.< / span >
< span class = "hljs-keyword" > type< / span > Values < span class = "hljs-keyword" > map< / span > [< span class = "hljs-typename" > string< / span > ][]< span class = "hljs-typename" > string< / span >
< span class = "hljs-comment" > // Get returns the first value associated with the given key,< / span >
< span class = "hljs-comment" > // or " " if there are none.< / span >
< span class = "hljs-keyword" > func< / span > (v Values) Get(key < span class = "hljs-typename" > string< / span > ) < span class = "hljs-typename" > string< / span > {
< span class = "hljs-keyword" > if< / span > vs := v[key]; < span class = "hljs-built_in" > len< / span > (vs) > < span class = "hljs-number" > 0< / span > {
< span class = "hljs-keyword" > return< / span > vs[< span class = "hljs-number" > 0< / span > ]
}
< span class = "hljs-keyword" > return< / span > < span class = "hljs-string" > " " < / span >
}
< span class = "hljs-comment" > // Add adds the value to key.< / span >
< span class = "hljs-comment" > // It appends to any existing values associated with key.< / span >
< span class = "hljs-keyword" > func< / span > (v Values) Add(key, value < span class = "hljs-typename" > string< / span > ) {
v[key] = < span class = "hljs-built_in" > append< / span > (v[key], value)
}
< / code > < / pre >
< p > 這 個 定 義 曏 外 部 暴 露 了 一 個 map的 類 型 的 變 量 , 併 且 提 供 了 一 些 能 夠 簡 單 操 作 這 個 map的 方 法 。 這 個 map的 value字 段 是 一 個 string的 slice, 所 以 這 個 Values是 一 個 多 維 map。 客 戶 端 使 用 這 個 變 量 的 時 候 可 以 使 用 map固 有 的 一 些 操 作 (make, 切 片 , m[key]等 等 ), 也 可 以 使 用 這 裏 提 供 的 操 作 方 法 , 或 者 兩 者 併 用 , 都 是 可 以 的 : < / p >
< pre > < code class = "lang-go" > gopl.io/ch6/urlvalues
m := url.Values{< span class = "hljs-string" > " lang" < / span > : {< span class = "hljs-string" > " en" < / span > }} < span class = "hljs-comment" > // direct construction< / span >
m.Add(< span class = "hljs-string" > " item" < / span > , < span class = "hljs-string" > " 1" < / span > )
m.Add(< span class = "hljs-string" > " item" < / span > , < span class = "hljs-string" > " 2" < / span > )
fmt.Println(m.Get(< span class = "hljs-string" > " lang" < / span > )) < span class = "hljs-comment" > // " en" < / span >
fmt.Println(m.Get(< span class = "hljs-string" > " q" < / span > )) < span class = "hljs-comment" > // " " < / span >
fmt.Println(m.Get(< span class = "hljs-string" > " item" < / span > )) < span class = "hljs-comment" > // " 1" (first value)< / span >
fmt.Println(m[< span class = "hljs-string" > " item" < / span > ]) < span class = "hljs-comment" > // " [1 2]" (direct map access)< / span >
m = < span class = "hljs-constant" > nil< / span >
fmt.Println(m.Get(< span class = "hljs-string" > " item" < / span > )) < span class = "hljs-comment" > // " " < / span >
m.Add(< span class = "hljs-string" > " item" < / span > , < span class = "hljs-string" > " 3" < / span > ) < span class = "hljs-comment" > // panic: assignment to entry in nil map< / span >
< / code > < / pre >
< p > 對 Get的 最 後 一 次 調 用 中 , nil接 收 器 的 行 為 卽 是 一 個 空 map的 行 為 。 我 們 可 以 等 價 地 將 這 個 操 作 寫 成 Value(nil).Get(" item" ), 但 是 如 果 你 直 接 寫 nil.Get(" item" )的 話 是 無 法 通 過 編 譯 的 , 因 為 nil的 字 麫 量 編 譯 器 無 法 判 斷 其 準 備 類 型 。 所 以 相 比 之 下 , 最 後 的 那 行 m.Add的 調 用 就 會 產 生 一 個 panic, 因 為 他 嚐 試 更 新 一 個 空 map。 < / p >
< p > 由 於 url.Values是 一 個 map類 型 , 併 且 間 接 引 用 了 其 key/value對 , 因 此 url.Values.Add對 這 個 map裏 的 元 素 做 任 何 的 更 新 、 刪 除 操 作 對 調 用 方 都 是 可 見 的 。 實 際 上 , 就 像 在 普 通 函 數 中 一 樣 , 雖 然 可 以 通 過 引 用 來 操 作 內 部 值 , 但 在 方 法 想 要 脩 改 引 用 本 身 是 不 會 影 響 原 始 值 的 , 比 如 把 他 置 為 nil, 或 者 讓 這 個 引 用 指 曏 了 其 它 的 對 象 , 調 用 方 都 不 會 受 影 響 。 (譯 註 : 因 為 傳 入 的 是 存 儲 了 內 存 地 址 的 變 量 , 你 改 變 這 個 變 量 是 影 響 不 了 原 始 的 變 量 的 , 想 想 C語 言 , 是 差 不 多 的 )< / p >
< / section >
< / div >
< / div >
< / div >
< a href = "../ch6/ch6-01.html" class = "navigation navigation-prev " aria-label = "Previous page: 方法聲明" > < i class = "fa fa-angle-left" > < / i > < / a >
< a href = "../ch6/ch6-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 >