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" / >
2015-12-31 08:20:27 +00:00
< title > JSON | Go语言圣经< / title >
2015-12-09 07:57:17 +00:00
< 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" >
2015-12-28 08:08:26 +00:00
< link rel = "stylesheet" href = "../gitbook/plugins/gitbook-plugin-katex/katex.min.css" >
2015-12-09 07:57:17 +00:00
< 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 = "../ch4/ch4-06.html" / >
< link rel = "prev" href = "../ch4/ch4-04.html" / >
< / head >
< body >
2015-12-31 08:20:27 +00:00
< div class = "book" data-level = "4.5" data-chapter-title = "JSON" data-filepath = "ch4/ch4-05.md" data-basepath = ".." data-revision = "Thu Dec 31 2015 16:18:40 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 >
2015-12-21 04:55:18 +00:00
致謝
2015-12-09 07:57:17 +00:00
< / 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 >
2015-12-21 04:55:18 +00:00
査找重複的行
2015-12-09 07:57:17 +00:00
< / 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 >
2015-12-21 04:55:18 +00:00
GIF動畵
2015-12-09 07:57:17 +00:00
< / 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 >
2015-12-28 08:08:26 +00:00
獲取URL
2015-12-09 07:57:17 +00:00
< / 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 >
2015-12-28 08:08:26 +00:00
併發獲取多個URL
2015-12-09 07:57:17 +00:00
< / 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 >
2015-12-21 04:55:18 +00:00
複數
2015-12-09 07:57:17 +00:00
< / 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 >
2015-12-21 04:55:18 +00:00
布爾型
2015-12-09 07:57:17 +00:00
< / 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 >
2015-12-21 04:55:18 +00:00
複合數據類型
2015-12-09 07:57:17 +00:00
< / 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 >
2015-12-31 08:20:27 +00:00
Slice
2015-12-09 07:57:17 +00:00
< / 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 >
2015-12-31 08:20:27 +00:00
Map
2015-12-09 07:57:17 +00:00
< / 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 active" 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 >
2015-12-28 08:08:26 +00:00
Recover捕獲異常
2015-12-09 07:57:17 +00:00
< / 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 >
2015-12-21 04:55:18 +00:00
基於指針對象的方法
2015-12-09 07:57:17 +00:00
< / 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 >
2015-12-21 04:55:18 +00:00
方法值和方法表達式
2015-12-09 07:57:17 +00:00
< / 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 >
2015-12-21 04:55:18 +00:00
接口是合約
2015-12-09 07:57:17 +00:00
< / 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 >
2015-12-21 04:55:18 +00:00
示例: 表達式求值
2015-12-09 07:57:17 +00:00
< / 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 >
2015-12-21 04:55:18 +00:00
基於類型斷言識别錯誤類型
2015-12-09 07:57:17 +00:00
< / 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 >
2015-12-21 04:55:18 +00:00
示例: 併發的Clock服務
2015-12-09 07:57:17 +00:00
< / 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 >
2015-12-21 04:55:18 +00:00
示例: 併發的Echo服務
2015-12-09 07:57:17 +00:00
< / 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 >
2015-12-21 04:55:18 +00:00
示例: 併發的Web爬蟲
2015-12-09 07:57:17 +00:00
< / 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 >
2015-12-21 04:55:18 +00:00
基於select的多路複用
2015-12-09 07:57:17 +00:00
< / 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 >
2015-12-21 04:55:18 +00:00
示例: 併發的字典遍歷
2015-12-09 07:57:17 +00:00
< / 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 >
2015-12-28 08:08:26 +00:00
併發的退出
2015-12-09 07:57:17 +00:00
< / 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 >
2015-12-21 04:55:18 +00:00
基於共享變量的併發
2015-12-09 07:57:17 +00:00
< / 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 >
2015-12-21 04:55:18 +00:00
內存同步
2015-12-09 07:57:17 +00:00
< / 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 >
2015-12-21 04:55:18 +00:00
示例: 併發的非阻塞緩存
2015-12-09 07:57:17 +00:00
< / 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 >
2015-12-21 04:55:18 +00:00
Goroutines和線程
2015-12-09 07:57:17 +00:00
< / 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 >
2015-12-21 04:55:18 +00:00
爲何需要反射?
2015-12-09 07:57:17 +00:00
< / 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 >
2015-12-21 04:55:18 +00:00
示例: 編碼S表達式
2015-12-09 07:57:17 +00:00
< / 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 >
2015-12-21 04:55:18 +00:00
示例: 解碼S表達式
2015-12-09 07:57:17 +00:00
< / 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 >
2015-12-28 08:08:26 +00:00
獲取結構體字段標識
2015-12-09 07:57:17 +00:00
< / 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 >
2015-12-24 06:47:06 +00:00
< li class = "chapter " data-level = "14" data-path = "CONTRIBUTORS.html" >
2015-12-09 07:57:17 +00:00
2015-12-24 06:47:06 +00:00
< a href = "../CONTRIBUTORS.html" >
2015-12-09 07:57:17 +00:00
< i class = "fa fa-check" > < / i >
2015-12-21 04:55:18 +00:00
< b > 14.< / b >
2015-12-09 07:57:17 +00:00
2015-12-24 06:47:06 +00:00
附録
2015-12-09 07:57:17 +00:00
< / 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 >
2015-12-31 08:20:27 +00:00
< a href = "../" > Go语言圣经< / a >
2015-12-09 07:57:17 +00:00
< / h1 >
< / div >
< div class = "page-wrapper" tabindex = "-1" role = "main" >
< div class = "page-inner" >
< section class = "normal" id = "section-" >
< h2 id = "45-json" > 4.5. JSON< / h2 >
2015-12-31 08:20:27 +00:00
< p > JavaScript對 象 表 示 法 ( JSON) 是 一 種 用 於 發 送 和 接 收 結 構 化 信 息 的 標 準 協 議 。 JSON併 不 是 唯 一 標 準 協 議 。 XML( § 7.14) 、 ASN.1和 Google的 Protocol Buffers都 是 類 似 的 協 議 , 併 且 有 各 自 的 特 色 , 但 是 由 於 簡 潔 性 、 可 讀 性 和 流 行 程 度 等 原 因 , JSON是 應 用 最 廣 泛 的 一 個 。 < / p >
< p > Go語 言 對 於 這 些 標 準 格 式 的 編 碼 和 解 碼 都 有 良 好 的 支 持 , 由 標 準 庫 中 的 encoding/json、 encoding/xml、 encoding/asn1等 包 提 供 ( 譯 註 : Protocol Buffers的 支 持 由 github.com/golang/protobuf 包 提 供 ) , 併 且 這 類 包 都 有 着 相 似 的 API接 口 。 本 節 , 我 們 將 對 重 要 的 encoding/json包 的 用 法 做 個 概 述 。 < / p >
< p > JSON是 對 JavaScript中 各 種 值 — — 字 符 串 、 數 字 、 布 爾 值 和 對 象 — — Unicode本 文 編 碼 。 它 可 以 用 有 效 可 讀 的 方 式 表 示 第 三 章 的 基 礎 數 據 類 型 和 本 章 的 數 組 、 slice、 結 構 體 和 map等 聚 合 數 據 類 型 。 < / p >
< p > 基 本 的 JSON類 型 有 數 字 ( 十 進 製 或 科 學 記 數 法 ) 、 布 爾 值 ( true或 false) 、 字 符 串 , 其 中 字 符 串 是 以 雙 引 號 包 含 的 Unicode字 符 序 列 , 支 持 和 Go語 言 類 似 的 反 斜 槓 轉 義 特 性 , 不 過 JSON使 用 的 是 \Uhhhh轉 義 數 字 來 表 示 一 個 UTF-16編 碼 , 而 不 是 Go語 言 的 rune類 型 。 < / p >
< p > 這 些 基 礎 類 型 可 以 通 過 JSON的 數 組 和 對 象 類 型 進 行 遞 歸 組 合 。 一 個 JSON數 組 是 一 個 有 序 的 值 序 列 , 寫 在 一 個 方 括 號 中 併 以 逗 號 分 隔 ; 一 個 JSON數 組 可 以 用 於 編 碼 Go語 言 的 數 組 和 slice。 一 個 JSON對 象 是 一 個 字 符 串 到 值 的 映 射 , 寫 成 以 繫 列 的 name:value對 形 式 , 用 花 括 號 包 含 併 以 逗 號 分 隔 ; JSON的 對 象 類 型 可 以 用 於 編 碼 Go語 言 的 map類 型 ( key類 型 是 字 符 串 ) 和 結 構 體 。 例 如 : < / p >
< pre > < code > boolean true
number -273.15
string " She said \" Hello, BF\" "
array [" gold" , " silver" , " bronze" ]
object {" year" : 1980,
" event" : " archery" ,
" medals" : [" gold" , " silver" , " bronze" ]}
< / code > < / pre > < p > 考 慮 一 個 應 用 程 序 , 該 程 序 負 責 收 集 各 種 電 影 評 論 併 提 供 反 饋 功 能 。 它 的 Movie數 據 類 型 和 一 個 典 型 的 表 示 電 影 的 值 列 表 如 下 所 示 。 ( 其 中 結 構 體 聲 明 中 , Year和 Color成 員 後 面 的 字 符 串 面 值 是 結 構 體 成 員 Tag; 我 們 稍 後 會 解 釋 它 的 作 用 。 ) < / p >
< pre > < code class = "lang-Go" > gopl.io/ch4/movie
< span class = "hljs-keyword" > type< / span > Movie < span class = "hljs-keyword" > struct< / span > {
Title < span class = "hljs-typename" > string< / span >
Year < span class = "hljs-typename" > int< / span > < span class = "hljs-string" > `json:" released" `< / span >
Color < span class = "hljs-typename" > bool< / span > < span class = "hljs-string" > `json:" color,omitempty" `< / span >
Actors []< span class = "hljs-typename" > string< / span >
}
< span class = "hljs-keyword" > var< / span > movies = []Movie{
{Title: < span class = "hljs-string" > " Casablanca" < / span > , Year: < span class = "hljs-number" > 1942< / span > , Color: < span class = "hljs-constant" > false< / span > ,
Actors: []< span class = "hljs-typename" > string< / span > {< span class = "hljs-string" > " Humphrey Bogart" < / span > , < span class = "hljs-string" > " Ingrid Bergman" < / span > }},
{Title: < span class = "hljs-string" > " Cool Hand Luke" < / span > , Year: < span class = "hljs-number" > 1967< / span > , Color: < span class = "hljs-constant" > true< / span > ,
Actors: []< span class = "hljs-typename" > string< / span > {< span class = "hljs-string" > " Paul Newman" < / span > }},
{Title: < span class = "hljs-string" > " Bullitt" < / span > , Year: < span class = "hljs-number" > 1968< / span > , Color: < span class = "hljs-constant" > true< / span > ,
Actors: []< span class = "hljs-typename" > string< / span > {< span class = "hljs-string" > " Steve McQueen" < / span > , < span class = "hljs-string" > " Jacqueline Bisset" < / span > }},
< span class = "hljs-comment" > // ...< / span >
}
< / code > < / pre >
< p > 這 樣 的 數 據 結 構 特 别 適 合 JSON格 式 , 併 且 在 兩 種 之 間 相 互 轉 換 也 很 容 易 。 將 一 個 Go語 言 中 類 似 movies的 結 構 體 slice轉 爲 JSON的 過 程 叫 編 組 ( marshaling) 。 編 組 通 過 調 用 json.Marshal函 數 完 成 : < / p >
< pre > < code class = "lang-Go" > data, err := json.Marshal(movies)
< span class = "hljs-keyword" > if< / span > err != < span class = "hljs-constant" > nil< / span > {
log.Fatalf(< span class = "hljs-string" > " JSON marshaling failed: %s" < / span > , err)
}
fmt.Printf(< span class = "hljs-string" > " %s\n" < / span > , data)
< / code > < / pre >
< p > Marshal函 數 生 成 一 個 字 節 slice, 包 含 很 長 的 字 符 串 , 併 且 沒 有 空 白 縮 進 ; 我 們 將 它 摺 行 以 便 於 顯 示 : < / p >
< pre > < code > [{" Title" :" Casablanca" ," released" :1942," Actors" :[" Humphrey Bogart" ," Ingr
id Bergman" ]},{" Title" :" Cool Hand Luke" ," released" :1967," color" :true," Ac
tors" :[" Paul Newman" ]},{" Title" :" Bullitt" ," released" :1968," color" :true,"
Actors" :[" Steve McQueen" ," Jacqueline Bisset" ]}]
< / code > < / pre > < p > 這 種 緊 湊 的 表 示 形 式 雖 然 包 含 了 全 部 的 信 息 , 但 是 很 難 閲 讀 。 爲 了 生 成 便 於 閲 讀 的 格 式 , 另 一 個 json.MarshalIndent函 數 將 産 生 整 齊 縮 進 的 輸 出 。 有 兩 個 額 外 的 字 符 串 參 數 用 於 表 示 每 一 行 輸 出 的 前 綴 和 每 一 個 層 級 的 縮 進 : < / p >
< pre > < code class = "lang-Go" > data, err := json.MarshalIndent(movies, < span class = "hljs-string" > " " < / span > , < span class = "hljs-string" > " " < / span > )
< span class = "hljs-keyword" > if< / span > err != < span class = "hljs-constant" > nil< / span > {
log.Fatalf(< span class = "hljs-string" > " JSON marshaling failed: %s" < / span > , err)
}
fmt.Printf(< span class = "hljs-string" > " %s\n" < / span > , data)
< / code > < / pre >
< p > 上 面 的 代 碼 將 産 生 這 樣 的 輸 出 : < / p >
< pre > < code class = "lang-Json" > [
{
< span class = "hljs-string" > " Title" < / span > : < span class = "hljs-string" > " Casablanca" < / span > ,
< span class = "hljs-string" > " released" < / span > : < span class = "hljs-number" > 1942< / span > ,
< span class = "hljs-string" > " Actors" < / span > : [
< span class = "hljs-string" > " Humphrey Bogart" < / span > ,
< span class = "hljs-string" > " Ingrid Bergman" < / span >
]
},
{
< span class = "hljs-string" > " Title" < / span > : < span class = "hljs-string" > " Cool Hand Luke" < / span > ,
< span class = "hljs-string" > " released" < / span > : < span class = "hljs-number" > 1967< / span > ,
< span class = "hljs-string" > " color" < / span > : < span class = "hljs-literal" > true< / span > ,
< span class = "hljs-string" > " Actors" < / span > : [
< span class = "hljs-string" > " Paul Newman" < / span >
]
},
{
< span class = "hljs-string" > " Title" < / span > : < span class = "hljs-string" > " Bullitt" < / span > ,
< span class = "hljs-string" > " released" < / span > : < span class = "hljs-number" > 1968< / span > ,
< span class = "hljs-string" > " color" < / span > : < span class = "hljs-literal" > true< / span > ,
< span class = "hljs-string" > " Actors" < / span > : [
< span class = "hljs-string" > " Steve McQueen" < / span > ,
< span class = "hljs-string" > " Jacqueline Bisset" < / span >
]
}
]
< / code > < / pre >
< p > 在 編 碼 時 , 默 認 使 用 Go語 言 結 構 體 的 成 員 名 字 作 爲 JSON的 對 象 ( 通 過 reflect反 射 技 術 , 我 們 將 在 12.6節 討 論 ) 。 隻 有 導 出 的 結 構 體 成 員 才 會 被 編 碼 , 這 也 就 是 我 們 爲 什 麽 選 擇 用 大 寫 字 母 開 頭 的 成 員 名 稱 。 < / p >
< p > 細 心 的 讀 者 可 能 已 經 註 意 到 , 其 中 Year名 字 的 成 員 在 編 碼 後 變 成 了 released, 還 有 Color長 遠 編 碼 後 變 成 了 小 寫 字 母 開 頭 的 color。 這 是 因 爲 構 體 成 員 Tag所 導 致 的 。 一 個 構 體 成 員 Tag是 和 在 編 譯 階 段 關 聯 到 該 成 員 的 元 信 息 字 符 串 : < / p >
< pre > < code > Year int `json:" released" `
Color bool `json:" color,omitempty" `
< / code > < / pre > < p > 結 構 體 的 成 員 Tag可 以 是 任 意 的 字 符 串 面 值 , 但 是 通 常 是 一 繫 列 用 空 格 分 隔 的 key:" value" 鍵 值 對 序 列 ; 因 爲 值 中 含 義 雙 引 號 字 符 , 因 此 成 員 Tag一 般 用 原 生 字 符 串 面 值 的 形 式 書 寫 。 json開 頭 鍵 對 應 的 值 用 於 控 製 encoding/json包 的 編 碼 和 解 碼 的 行 爲 , 併 且 encoding/...下 面 其 它 的 包 也 遵 循 這 個 約 定 。 成 員 Tag中 json對 應 值 的 第 一 部 分 用 於 指 定 JSON對 象 的 名 字 , 比 如 將 Go語 言 中 的 TotalCount成 員 對 應 到 JSON中 的 total_count對 象 。 Color成 員 的 Tag還 帶 了 一 個 額 外 的 omitempty選 項 , 表 示 當 Go語 言 結 構 體 成 員 爲 空 或 零 值 時 不 生 成 JSON對 象 ( 這 里 false爲 零 值 ) 。 果 然 , Casablanca是 一 個 黑 白 電 影 , 併 沒 有 輸 出 Color成 員 。 < / p >
< p > 編 碼 的 逆 操 作 是 解 碼 , 對 應 將 JSON數 據 解 碼 爲 GO語 言 的 數 據 結 構 , Go語 言 中 一 般 叫 unmarshaling, 通 過 json.Unmarshal函 數 完 成 。 下 面 的 代 碼 將 JSON格 式 的 電 影 數 據 解 碼 爲 一 個 結 構 體 slice, 結 構 體 中 隻 有 Title成 員 。 通 過 定 義 合 適 的 Go語 言 數 據 結 構 , 我 們 可 以 選 擇 性 地 解 碼 JSON中 感 興 趣 的 成 員 。 當 Unmarshal返 迴 , slice將 被 隻 含 有 Title信 息 值 填 充 , 其 它 JSON成 員 將 被 忽 略 。 < / p >
< pre > < code class = "lang-Go" > < span class = "hljs-keyword" > var< / span > titles []< span class = "hljs-keyword" > struct< / span > { Title < span class = "hljs-typename" > string< / span > }
< span class = "hljs-keyword" > if< / span > err := json.Unmarshal(data, & titles); err != < span class = "hljs-constant" > nil< / span > {
log.Fatalf(< span class = "hljs-string" > " JSON unmarshaling failed: %s" < / span > , err)
}
fmt.Println(titles) < span class = "hljs-comment" > // " [{Casablanca} {Cool Hand Luke} {Bullitt}]" < / span >
< / code > < / pre >
< p > 許 多 web服 務 都 提 供 JSON接 口 , 通 過 HTTP接 口 發 送 JSON格 式 請 求 併 返 迴 JSON格 式 的 信 息 。 爲 了 説 明 這 一 點 , 我 們 通 過 Github的 issue査 詢 服 務 。 首 先 , 我 們 要 定 義 合 適 的 類 型 和 常 量 : < / p >
< pre > < code class = "lang-Go" > gopl.io/ch4/github
< span class = "hljs-comment" > // Package github provides a Go API for the GitHub issue tracker.< / span >
< span class = "hljs-comment" > // See https://developer.github.com/v3/search/#search-issues.< / span >
< span class = "hljs-keyword" > package< / span > github
< span class = "hljs-keyword" > import< / span > < span class = "hljs-string" > " time" < / span >
< span class = "hljs-keyword" > const< / span > IssuesURL = < span class = "hljs-string" > " https://api.github.com/search/issues" < / span >
< span class = "hljs-keyword" > type< / span > IssuesSearchResult < span class = "hljs-keyword" > struct< / span > {
TotalCount < span class = "hljs-typename" > int< / span > < span class = "hljs-string" > `json:" total_count" `< / span >
Items []*Issue
}
< span class = "hljs-keyword" > type< / span > Issue < span class = "hljs-keyword" > struct< / span > {
Number < span class = "hljs-typename" > int< / span >
HTMLURL < span class = "hljs-typename" > string< / span > < span class = "hljs-string" > `json:" html_url" `< / span >
Title < span class = "hljs-typename" > string< / span >
State < span class = "hljs-typename" > string< / span >
User *User
CreatedAt time.Time < span class = "hljs-string" > `json:" created_at" `< / span >
Body < span class = "hljs-typename" > string< / span > < span class = "hljs-comment" > // in Markdown format< / span >
}
< span class = "hljs-keyword" > type< / span > User < span class = "hljs-keyword" > struct< / span > {
Login < span class = "hljs-typename" > string< / span >
HTMLURL < span class = "hljs-typename" > string< / span > < span class = "hljs-string" > `json:" html_url" `< / span >
}
< / code > < / pre >
< p > 和 前 面 一 樣 , 卽 使 對 應 的 JSON對 象 名 是 小 寫 字 母 , 每 個 結 構 體 的 成 員 名 也 是 聲 明 爲 大 小 字 母 開 頭 的 。 因 爲 有 些 JSON成 員 名 字 和 Go結 構 體 成 員 名 字 併 不 相 同 , 因 此 需 要 Go語 言 結 構 體 成 員 Tag來 指 定 對 應 的 JSON名 字 。 同 樣 , 在 解 碼 的 時 候 也 需 要 做 同 樣 的 處 理 , GitHub服 務 返 迴 的 信 息 比 我 們 定 義 的 要 多 很 多 。 < / p >
< p > SearchIssues函 數 發 出 一 個 HTTP請 求 , 然 後 解 碼 返 迴 的 JSON格 式 的 結 果 。 因 爲 用 戶 提 供 的 査 詢 條 件 可 能 包 含 類 似 < code > ?< / code > 和 < code > & < / code > 之 類 的 特 殊 字 符 , 爲 了 避 免 對 URL造 成 衝 突 , 我 們 用 url.QueryEscape來 對 査 詢 中 的 特 殊 字 符 進 行 轉 義 操 作 。 < / p >
< pre > < code class = "lang-Go" > gopl.io/ch4/github
< span class = "hljs-keyword" > package< / span > github
< span class = "hljs-keyword" > import< / span > (
< span class = "hljs-string" > " encoding/json" < / span >
< span class = "hljs-string" > " fmt" < / span >
< span class = "hljs-string" > " net/http" < / span >
< span class = "hljs-string" > " net/url" < / span >
< span class = "hljs-string" > " strings" < / span >
)
< span class = "hljs-comment" > // SearchIssues queries the GitHub issue tracker.< / span >
< span class = "hljs-keyword" > func< / span > SearchIssues(terms []< span class = "hljs-typename" > string< / span > ) (*IssuesSearchResult, error) {
q := url.QueryEscape(strings.Join(terms, < span class = "hljs-string" > " " < / span > ))
resp, err := http.Get(IssuesURL + < span class = "hljs-string" > " ?q=" < / span > + q)
< span class = "hljs-keyword" > if< / span > err != < span class = "hljs-constant" > nil< / span > {
< span class = "hljs-keyword" > return< / span > < span class = "hljs-constant" > nil< / span > , err
}
< span class = "hljs-comment" > // We must close resp.Body on all execution paths.< / span >
< span class = "hljs-comment" > // (Chapter 5 presents ' defer' , which makes this simpler.)< / span >
< span class = "hljs-keyword" > if< / span > resp.StatusCode != http.StatusOK {
resp.Body.Close()
< span class = "hljs-keyword" > return< / span > < span class = "hljs-constant" > nil< / span > , fmt.Errorf(< span class = "hljs-string" > " search query failed: %s" < / span > , resp.Status)
}
< span class = "hljs-keyword" > var< / span > result IssuesSearchResult
< span class = "hljs-keyword" > if< / span > err := json.NewDecoder(resp.Body).Decode(& result); err != < span class = "hljs-constant" > nil< / span > {
resp.Body.Close()
< span class = "hljs-keyword" > return< / span > < span class = "hljs-constant" > nil< / span > , err
}
resp.Body.Close()
< span class = "hljs-keyword" > return< / span > & result, < span class = "hljs-constant" > nil< / span >
}
< / code > < / pre >
< p > 在 早 些 的 例 子 中 , 我 們 使 用 了 json.Unmarshal函 數 來 將 JSON格 式 的 字 符 串 解 碼 爲 字 節 slice。 但 是 這 個 例 子 中 , 我 們 使 用 了 基 於 流 式 的 解 碼 器 json.Decoder, 它 可 以 從 一 個 流 解 碼 JSON數 據 , 盡 管 這 不 是 必 鬚 的 。 如 您 所 料 , 還 有 一 個 針 對 輸 出 流 的 json.Encoder編 碼 對 象 。 < / p >
< p > 我 們 調 用 Decode方 法 來 填 充 變 量 。 這 里 有 多 種 方 法 可 以 格 式 化 結 構 。 下 面 是 最 簡 單 的 一 種 , 以 一 個 固 定 寬 度 打 印 每 個 issue, 但 是 在 下 一 節 我 們 將 看 到 如 果 利 用 模 闆 來 輸 出 複 雜 的 格 式 。 < / p >
< pre > < code class = "lang-Go" > gopl.io/ch4/issues
< span class = "hljs-comment" > // Issues prints a table of GitHub issues matching the search terms.< / span >
< span class = "hljs-keyword" > package< / span > main
< span class = "hljs-keyword" > import< / span > (
< span class = "hljs-string" > " fmt" < / span >
< span class = "hljs-string" > " log" < / span >
< span class = "hljs-string" > " os" < / span >
< span class = "hljs-string" > " gopl.io/ch4/github" < / span >
)
< span class = "hljs-keyword" > func< / span > main() {
result, err := github.SearchIssues(os.Args[< span class = "hljs-number" > 1< / span > :])
< span class = "hljs-keyword" > if< / span > err != < span class = "hljs-constant" > nil< / span > {
log.Fatal(err)
}
fmt.Printf(< span class = "hljs-string" > " %d issues:\n" < / span > , result.TotalCount)
< span class = "hljs-keyword" > for< / span > _, item := < span class = "hljs-keyword" > range< / span > result.Items {
fmt.Printf(< span class = "hljs-string" > " #%-5d %9.9s %.55s\n" < / span > ,
item.Number, item.User.Login, item.Title)
}
}
< / code > < / pre >
< p > 通 過 命 令 行 參 數 指 定 檢 索 條 件 。 下 面 的 命 令 是 査 詢 Go語 言 項 目 中 和 JSON解 碼 相 關 的 問 題 , 還 有 査 詢 返 迴 的 結 果 : < / p >
< pre > < code > $ go build gopl.io/ch4/issues
$ ./issues repo:golang/go is:open json decoder
13 issues:
#5680 eaigner encoding/json: set key converter on en/decoder
#6050 gopherbot encoding/json: provide tokenizer
#8658 gopherbot encoding/json: use bufio
#8462 kortschak encoding/json: UnmarshalText confuses json.Unmarshal
#5901 rsc encoding/json: allow override type marshaling
#9812 klauspost encoding/json: string tag not symmetric
#7872 extempora encoding/json: Encoder internally buffers full output
#9650 cespare encoding/json: Decoding gives errPhase when unmarshalin
#6716 gopherbot encoding/json: include field name in unmarshal error me
#6901 lukescott encoding/json, encoding/xml: option to treat unknown fi
#6384 joeshaw encoding/json: encode precise floating point integers u
#6647 btracey x/tools/cmd/godoc: display type kind of each named type
#4237 gjemiller encoding/base64: URLEncoding padding is optional
< / code > < / pre > < p > GitHub的 Web服 務 接 口 < a href = "https://developer.github.com/v3/" target = "_blank" > https://developer.github.com/v3/< / a > 包 含 了 更 多 的 特 性 。 < / p >
< p > < strong > 練 習 4.10: < / strong > 脩 改 issues程 序 , 根 據 問 題 的 時 間 進 行 分 類 , 比 如 不 到 一 個 月 的 、 不 到 一 年 的 、 超 過 一 年 。 < / p >
< p > < strong > 練 習 4.11: < / strong > 編 寫 一 個 工 具 , 允 許 用 戶 在 命 令 行 創 建 、 讀 取 、 更 新 和 刪 除 GitHub上 的 issue, 當 必 要 的 時 候 自 動 打 開 用 戶 默 認 的 編 輯 器 用 於 輸 入 文 本 信 息 。 < / p >
< p > < strong > 練 習 4.12: < / strong > 流 行 的 web漫 畵 服 務 xkcd也 提 供 了 JSON接 口 。 例 如 , 一 個 < a href = "https://xkcd.com/571/info.0.json" target = "_blank" > https://xkcd.com/571/info.0.json< / a > 請 求 將 返 迴 一 個 很 多 人 喜 愛 的 571編 號 的 詳 細 描 述 。 下 載 每 個 鏈 接 ( 隻 下 載 一 次 ) 然 後 創 建 一 個 離 線 索 引 。 編 寫 一 個 xkcd工 具 , 使 用 這 些 離 線 索 引 , 打 印 和 命 令 行 輸 入 的 檢 索 詞 相 匹 配 的 漫 畵 的 URL。 < / p >
< p > < strong > 練 習 4.13: < / strong > 使 用 開 放 電 影 數 據 庫 的 JSON服 務 接 口 , 允 許 你 檢 索 和 下 載 < a href = "https://omdbapi.com/" target = "_blank" > https://omdbapi.com/< / a > 上 電 影 的 名 字 和 對 應 的 海 報 圖 像 。 編 寫 一 個 poster工 具 , 通 過 命 令 行 輸 入 的 電 影 名 字 , 下 載 對 應 的 海 報 。 < / p >
2015-12-09 07:57:17 +00:00
< / section >
< / div >
< / div >
< / div >
< a href = "../ch4/ch4-04.html" class = "navigation navigation-prev " aria-label = "Previous page: 結構體" > < i class = "fa fa-angle-left" > < / i > < / a >
< a href = "../ch4/ch4-06.html" class = "navigation navigation-next " aria-label = "Next page: 文本和HTML模闆" > < 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) {
2015-12-28 08:08:26 +00:00
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}};
2015-12-09 07:57:17 +00:00
gitbook.start(config);
});
< / script >
< / body >
< / html >