gopl-zh.github.com/ch3/ch3-06.html
2015-12-24 14:49:03 +08:00

2239 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>常量 | 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="../ch4/ch4.html" />
<link rel="prev" href="../ch3/ch3-05.html" />
</head>
<body>
<div class="book" data-level="3.6" data-chapter-title="常量" data-filepath="ch3/ch3-06.md" data-basepath=".." data-revision="Thu Dec 24 2015 14:42:02 GMT+0800 (中国标准时间)">
<div class="book-summary">
<nav role="navigation">
<ul class="summary">
<li class="chapter " data-level="0" data-path="index.html">
<a href="../index.html">
<i class="fa fa-check"></i>
前言
</a>
<ul class="articles">
<li class="chapter " data-level="0.1" data-path="ch0/ch0-01.html">
<a href="../ch0/ch0-01.html">
<i class="fa fa-check"></i>
<b>0.1.</b>
Go語言起源
</a>
</li>
<li class="chapter " data-level="0.2" data-path="ch0/ch0-02.html">
<a href="../ch0/ch0-02.html">
<i class="fa fa-check"></i>
<b>0.2.</b>
Go語言項目
</a>
</li>
<li class="chapter " data-level="0.3" data-path="ch0/ch0-03.html">
<a href="../ch0/ch0-03.html">
<i class="fa fa-check"></i>
<b>0.3.</b>
本書的組織
</a>
</li>
<li class="chapter " data-level="0.4" data-path="ch0/ch0-04.html">
<a href="../ch0/ch0-04.html">
<i class="fa fa-check"></i>
<b>0.4.</b>
更多的信息
</a>
</li>
<li class="chapter " data-level="0.5" data-path="ch0/ch0-05.html">
<a href="../ch0/ch0-05.html">
<i class="fa fa-check"></i>
<b>0.5.</b>
致謝
</a>
</li>
</ul>
</li>
<li class="chapter " data-level="1" data-path="ch1/ch1.html">
<a href="../ch1/ch1.html">
<i class="fa fa-check"></i>
<b>1.</b>
入門
</a>
<ul class="articles">
<li class="chapter " data-level="1.1" data-path="ch1/ch1-01.html">
<a href="../ch1/ch1-01.html">
<i class="fa fa-check"></i>
<b>1.1.</b>
Hello, World
</a>
</li>
<li class="chapter " data-level="1.2" data-path="ch1/ch1-02.html">
<a href="../ch1/ch1-02.html">
<i class="fa fa-check"></i>
<b>1.2.</b>
命令行參數
</a>
</li>
<li class="chapter " data-level="1.3" data-path="ch1/ch1-03.html">
<a href="../ch1/ch1-03.html">
<i class="fa fa-check"></i>
<b>1.3.</b>
査找重複的行
</a>
</li>
<li class="chapter " data-level="1.4" data-path="ch1/ch1-04.html">
<a href="../ch1/ch1-04.html">
<i class="fa fa-check"></i>
<b>1.4.</b>
GIF動畵
</a>
</li>
<li class="chapter " data-level="1.5" data-path="ch1/ch1-05.html">
<a href="../ch1/ch1-05.html">
<i class="fa fa-check"></i>
<b>1.5.</b>
穫取URL
</a>
</li>
<li class="chapter " data-level="1.6" data-path="ch1/ch1-06.html">
<a href="../ch1/ch1-06.html">
<i class="fa fa-check"></i>
<b>1.6.</b>
併發穫取多個URL
</a>
</li>
<li class="chapter " data-level="1.7" data-path="ch1/ch1-07.html">
<a href="../ch1/ch1-07.html">
<i class="fa fa-check"></i>
<b>1.7.</b>
Web服務
</a>
</li>
<li class="chapter " data-level="1.8" data-path="ch1/ch1-08.html">
<a href="../ch1/ch1-08.html">
<i class="fa fa-check"></i>
<b>1.8.</b>
本章要點
</a>
</li>
</ul>
</li>
<li class="chapter " data-level="2" data-path="ch2/ch2.html">
<a href="../ch2/ch2.html">
<i class="fa fa-check"></i>
<b>2.</b>
程序結構
</a>
<ul class="articles">
<li class="chapter " data-level="2.1" data-path="ch2/ch2-01.html">
<a href="../ch2/ch2-01.html">
<i class="fa fa-check"></i>
<b>2.1.</b>
命名
</a>
</li>
<li class="chapter " data-level="2.2" data-path="ch2/ch2-02.html">
<a href="../ch2/ch2-02.html">
<i class="fa fa-check"></i>
<b>2.2.</b>
聲明
</a>
</li>
<li class="chapter " data-level="2.3" data-path="ch2/ch2-03.html">
<a href="../ch2/ch2-03.html">
<i class="fa fa-check"></i>
<b>2.3.</b>
變量
</a>
</li>
<li class="chapter " data-level="2.4" data-path="ch2/ch2-04.html">
<a href="../ch2/ch2-04.html">
<i class="fa fa-check"></i>
<b>2.4.</b>
賦值
</a>
</li>
<li class="chapter " data-level="2.5" data-path="ch2/ch2-05.html">
<a href="../ch2/ch2-05.html">
<i class="fa fa-check"></i>
<b>2.5.</b>
類型
</a>
</li>
<li class="chapter " data-level="2.6" data-path="ch2/ch2-06.html">
<a href="../ch2/ch2-06.html">
<i class="fa fa-check"></i>
<b>2.6.</b>
包和文件
</a>
</li>
<li class="chapter " data-level="2.7" data-path="ch2/ch2-07.html">
<a href="../ch2/ch2-07.html">
<i class="fa fa-check"></i>
<b>2.7.</b>
作用域
</a>
</li>
</ul>
</li>
<li class="chapter " data-level="3" data-path="ch3/ch3.html">
<a href="../ch3/ch3.html">
<i class="fa fa-check"></i>
<b>3.</b>
基礎數據類型
</a>
<ul class="articles">
<li class="chapter " data-level="3.1" data-path="ch3/ch3-01.html">
<a href="../ch3/ch3-01.html">
<i class="fa fa-check"></i>
<b>3.1.</b>
整型
</a>
</li>
<li class="chapter " data-level="3.2" data-path="ch3/ch3-02.html">
<a href="../ch3/ch3-02.html">
<i class="fa fa-check"></i>
<b>3.2.</b>
浮點數
</a>
</li>
<li class="chapter " data-level="3.3" data-path="ch3/ch3-03.html">
<a href="../ch3/ch3-03.html">
<i class="fa fa-check"></i>
<b>3.3.</b>
複數
</a>
</li>
<li class="chapter " data-level="3.4" data-path="ch3/ch3-04.html">
<a href="../ch3/ch3-04.html">
<i class="fa fa-check"></i>
<b>3.4.</b>
布爾型
</a>
</li>
<li class="chapter " data-level="3.5" data-path="ch3/ch3-05.html">
<a href="../ch3/ch3-05.html">
<i class="fa fa-check"></i>
<b>3.5.</b>
字符串
</a>
</li>
<li class="chapter active" data-level="3.6" data-path="ch3/ch3-06.html">
<a href="../ch3/ch3-06.html">
<i class="fa fa-check"></i>
<b>3.6.</b>
常量
</a>
</li>
</ul>
</li>
<li class="chapter " data-level="4" data-path="ch4/ch4.html">
<a href="../ch4/ch4.html">
<i class="fa fa-check"></i>
<b>4.</b>
複合數據類型
</a>
<ul class="articles">
<li class="chapter " data-level="4.1" data-path="ch4/ch4-01.html">
<a href="../ch4/ch4-01.html">
<i class="fa fa-check"></i>
<b>4.1.</b>
數組
</a>
</li>
<li class="chapter " data-level="4.2" data-path="ch4/ch4-02.html">
<a href="../ch4/ch4-02.html">
<i class="fa fa-check"></i>
<b>4.2.</b>
切片
</a>
</li>
<li class="chapter " data-level="4.3" data-path="ch4/ch4-03.html">
<a href="../ch4/ch4-03.html">
<i class="fa fa-check"></i>
<b>4.3.</b>
字典
</a>
</li>
<li class="chapter " data-level="4.4" data-path="ch4/ch4-04.html">
<a href="../ch4/ch4-04.html">
<i class="fa fa-check"></i>
<b>4.4.</b>
結構體
</a>
</li>
<li class="chapter " data-level="4.5" data-path="ch4/ch4-05.html">
<a href="../ch4/ch4-05.html">
<i class="fa fa-check"></i>
<b>4.5.</b>
JSON
</a>
</li>
<li class="chapter " data-level="4.6" data-path="ch4/ch4-06.html">
<a href="../ch4/ch4-06.html">
<i class="fa fa-check"></i>
<b>4.6.</b>
文本和HTML模闆
</a>
</li>
</ul>
</li>
<li class="chapter " data-level="5" data-path="ch5/ch5.html">
<a href="../ch5/ch5.html">
<i class="fa fa-check"></i>
<b>5.</b>
函數
</a>
<ul class="articles">
<li class="chapter " data-level="5.1" data-path="ch5/ch5-01.html">
<a href="../ch5/ch5-01.html">
<i class="fa fa-check"></i>
<b>5.1.</b>
函數聲明
</a>
</li>
<li class="chapter " data-level="5.2" data-path="ch5/ch5-02.html">
<a href="../ch5/ch5-02.html">
<i class="fa fa-check"></i>
<b>5.2.</b>
遞歸
</a>
</li>
<li class="chapter " data-level="5.3" data-path="ch5/ch5-03.html">
<a href="../ch5/ch5-03.html">
<i class="fa fa-check"></i>
<b>5.3.</b>
多返迴值
</a>
</li>
<li class="chapter " data-level="5.4" data-path="ch5/ch5-04.html">
<a href="../ch5/ch5-04.html">
<i class="fa fa-check"></i>
<b>5.4.</b>
錯誤
</a>
</li>
<li class="chapter " data-level="5.5" data-path="ch5/ch5-05.html">
<a href="../ch5/ch5-05.html">
<i class="fa fa-check"></i>
<b>5.5.</b>
函數值
</a>
</li>
<li class="chapter " data-level="5.6" data-path="ch5/ch5-06.html">
<a href="../ch5/ch5-06.html">
<i class="fa fa-check"></i>
<b>5.6.</b>
匿名函數
</a>
</li>
<li class="chapter " data-level="5.7" data-path="ch5/ch5-07.html">
<a href="../ch5/ch5-07.html">
<i class="fa fa-check"></i>
<b>5.7.</b>
可變參數
</a>
</li>
<li class="chapter " data-level="5.8" data-path="ch5/ch5-08.html">
<a href="../ch5/ch5-08.html">
<i class="fa fa-check"></i>
<b>5.8.</b>
Deferred函數
</a>
</li>
<li class="chapter " data-level="5.9" data-path="ch5/ch5-09.html">
<a href="../ch5/ch5-09.html">
<i class="fa fa-check"></i>
<b>5.9.</b>
Panic異常
</a>
</li>
<li class="chapter " data-level="5.10" data-path="ch5/ch5-10.html">
<a href="../ch5/ch5-10.html">
<i class="fa fa-check"></i>
<b>5.10.</b>
Recover捕穫異常
</a>
</li>
</ul>
</li>
<li class="chapter " data-level="6" data-path="ch6/ch6.html">
<a href="../ch6/ch6.html">
<i class="fa fa-check"></i>
<b>6.</b>
方法
</a>
<ul class="articles">
<li class="chapter " data-level="6.1" data-path="ch6/ch6-01.html">
<a href="../ch6/ch6-01.html">
<i class="fa fa-check"></i>
<b>6.1.</b>
方法聲明
</a>
</li>
<li class="chapter " data-level="6.2" data-path="ch6/ch6-02.html">
<a href="../ch6/ch6-02.html">
<i class="fa fa-check"></i>
<b>6.2.</b>
基於指針對象的方法
</a>
</li>
<li class="chapter " data-level="6.3" data-path="ch6/ch6-03.html">
<a href="../ch6/ch6-03.html">
<i class="fa fa-check"></i>
<b>6.3.</b>
通過嵌入結構體來擴展類型
</a>
</li>
<li class="chapter " data-level="6.4" data-path="ch6/ch6-04.html">
<a href="../ch6/ch6-04.html">
<i class="fa fa-check"></i>
<b>6.4.</b>
方法值和方法表達式
</a>
</li>
<li class="chapter " data-level="6.5" data-path="ch6/ch6-05.html">
<a href="../ch6/ch6-05.html">
<i class="fa fa-check"></i>
<b>6.5.</b>
示例: Bit數組
</a>
</li>
<li class="chapter " data-level="6.6" data-path="ch6/ch6-06.html">
<a href="../ch6/ch6-06.html">
<i class="fa fa-check"></i>
<b>6.6.</b>
封裝
</a>
</li>
</ul>
</li>
<li class="chapter " data-level="7" data-path="ch7/ch7.html">
<a href="../ch7/ch7.html">
<i class="fa fa-check"></i>
<b>7.</b>
接口
</a>
<ul class="articles">
<li class="chapter " data-level="7.1" data-path="ch7/ch7-01.html">
<a href="../ch7/ch7-01.html">
<i class="fa fa-check"></i>
<b>7.1.</b>
接口是合約
</a>
</li>
<li class="chapter " data-level="7.2" data-path="ch7/ch7-02.html">
<a href="../ch7/ch7-02.html">
<i class="fa fa-check"></i>
<b>7.2.</b>
接口類型
</a>
</li>
<li class="chapter " data-level="7.3" data-path="ch7/ch7-03.html">
<a href="../ch7/ch7-03.html">
<i class="fa fa-check"></i>
<b>7.3.</b>
實現接口的條件
</a>
</li>
<li class="chapter " data-level="7.4" data-path="ch7/ch7-04.html">
<a href="../ch7/ch7-04.html">
<i class="fa fa-check"></i>
<b>7.4.</b>
flag.Value接口
</a>
</li>
<li class="chapter " data-level="7.5" data-path="ch7/ch7-05.html">
<a href="../ch7/ch7-05.html">
<i class="fa fa-check"></i>
<b>7.5.</b>
接口值
</a>
</li>
<li class="chapter " data-level="7.6" data-path="ch7/ch7-06.html">
<a href="../ch7/ch7-06.html">
<i class="fa fa-check"></i>
<b>7.6.</b>
sort.Interface接口
</a>
</li>
<li class="chapter " data-level="7.7" data-path="ch7/ch7-07.html">
<a href="../ch7/ch7-07.html">
<i class="fa fa-check"></i>
<b>7.7.</b>
http.Handler接口
</a>
</li>
<li class="chapter " data-level="7.8" data-path="ch7/ch7-08.html">
<a href="../ch7/ch7-08.html">
<i class="fa fa-check"></i>
<b>7.8.</b>
error接口
</a>
</li>
<li class="chapter " data-level="7.9" data-path="ch7/ch7-09.html">
<a href="../ch7/ch7-09.html">
<i class="fa fa-check"></i>
<b>7.9.</b>
示例: 表達式求值
</a>
</li>
<li class="chapter " data-level="7.10" data-path="ch7/ch7-10.html">
<a href="../ch7/ch7-10.html">
<i class="fa fa-check"></i>
<b>7.10.</b>
類型斷言
</a>
</li>
<li class="chapter " data-level="7.11" data-path="ch7/ch7-11.html">
<a href="../ch7/ch7-11.html">
<i class="fa fa-check"></i>
<b>7.11.</b>
基於類型斷言識别錯誤類型
</a>
</li>
<li class="chapter " data-level="7.12" data-path="ch7/ch7-12.html">
<a href="../ch7/ch7-12.html">
<i class="fa fa-check"></i>
<b>7.12.</b>
通過類型斷言査詢接口
</a>
</li>
<li class="chapter " data-level="7.13" data-path="ch7/ch7-13.html">
<a href="../ch7/ch7-13.html">
<i class="fa fa-check"></i>
<b>7.13.</b>
類型分支
</a>
</li>
<li class="chapter " data-level="7.14" data-path="ch7/ch7-14.html">
<a href="../ch7/ch7-14.html">
<i class="fa fa-check"></i>
<b>7.14.</b>
示例: 基於標記的XML解碼
</a>
</li>
<li class="chapter " data-level="7.15" data-path="ch7/ch7-15.html">
<a href="../ch7/ch7-15.html">
<i class="fa fa-check"></i>
<b>7.15.</b>
補充幾點
</a>
</li>
</ul>
</li>
<li class="chapter " data-level="8" data-path="ch8/ch8.html">
<a href="../ch8/ch8.html">
<i class="fa fa-check"></i>
<b>8.</b>
Goroutines和Channels
</a>
<ul class="articles">
<li class="chapter " data-level="8.1" data-path="ch8/ch8-01.html">
<a href="../ch8/ch8-01.html">
<i class="fa fa-check"></i>
<b>8.1.</b>
Goroutines
</a>
</li>
<li class="chapter " data-level="8.2" data-path="ch8/ch8-02.html">
<a href="../ch8/ch8-02.html">
<i class="fa fa-check"></i>
<b>8.2.</b>
示例: 併發的Clock服務
</a>
</li>
<li class="chapter " data-level="8.3" data-path="ch8/ch8-03.html">
<a href="../ch8/ch8-03.html">
<i class="fa fa-check"></i>
<b>8.3.</b>
示例: 併發的Echo服務
</a>
</li>
<li class="chapter " data-level="8.4" data-path="ch8/ch8-04.html">
<a href="../ch8/ch8-04.html">
<i class="fa fa-check"></i>
<b>8.4.</b>
Channels
</a>
</li>
<li class="chapter " data-level="8.5" data-path="ch8/ch8-05.html">
<a href="../ch8/ch8-05.html">
<i class="fa fa-check"></i>
<b>8.5.</b>
併行的循環
</a>
</li>
<li class="chapter " data-level="8.6" data-path="ch8/ch8-06.html">
<a href="../ch8/ch8-06.html">
<i class="fa fa-check"></i>
<b>8.6.</b>
示例: 併發的Web爬蟲
</a>
</li>
<li class="chapter " data-level="8.7" data-path="ch8/ch8-07.html">
<a href="../ch8/ch8-07.html">
<i class="fa fa-check"></i>
<b>8.7.</b>
基於select的多路複用
</a>
</li>
<li class="chapter " data-level="8.8" data-path="ch8/ch8-08.html">
<a href="../ch8/ch8-08.html">
<i class="fa fa-check"></i>
<b>8.8.</b>
示例: 併發的字典遍歷
</a>
</li>
<li class="chapter " data-level="8.9" data-path="ch8/ch8-09.html">
<a href="../ch8/ch8-09.html">
<i class="fa fa-check"></i>
<b>8.9.</b>
併發的退齣
</a>
</li>
<li class="chapter " data-level="8.10" data-path="ch8/ch8-10.html">
<a href="../ch8/ch8-10.html">
<i class="fa fa-check"></i>
<b>8.10.</b>
示例: 聊天服務
</a>
</li>
</ul>
</li>
<li class="chapter " data-level="9" data-path="ch9/ch9.html">
<a href="../ch9/ch9.html">
<i class="fa fa-check"></i>
<b>9.</b>
基於共享變量的併發
</a>
<ul class="articles">
<li class="chapter " data-level="9.1" data-path="ch9/ch9-01.html">
<a href="../ch9/ch9-01.html">
<i class="fa fa-check"></i>
<b>9.1.</b>
競爭條件
</a>
</li>
<li class="chapter " data-level="9.2" data-path="ch9/ch9-02.html">
<a href="../ch9/ch9-02.html">
<i class="fa fa-check"></i>
<b>9.2.</b>
sync.Mutex互斥鎖
</a>
</li>
<li class="chapter " data-level="9.3" data-path="ch9/ch9-03.html">
<a href="../ch9/ch9-03.html">
<i class="fa fa-check"></i>
<b>9.3.</b>
sync.RWMutex讀寫鎖
</a>
</li>
<li class="chapter " data-level="9.4" data-path="ch9/ch9-04.html">
<a href="../ch9/ch9-04.html">
<i class="fa fa-check"></i>
<b>9.4.</b>
內存同步
</a>
</li>
<li class="chapter " data-level="9.5" data-path="ch9/ch9-05.html">
<a href="../ch9/ch9-05.html">
<i class="fa fa-check"></i>
<b>9.5.</b>
sync.Once初始化
</a>
</li>
<li class="chapter " data-level="9.6" data-path="ch9/ch9-06.html">
<a href="../ch9/ch9-06.html">
<i class="fa fa-check"></i>
<b>9.6.</b>
競爭條件檢測
</a>
</li>
<li class="chapter " data-level="9.7" data-path="ch9/ch9-07.html">
<a href="../ch9/ch9-07.html">
<i class="fa fa-check"></i>
<b>9.7.</b>
示例: 併發的非阻塞緩存
</a>
</li>
<li class="chapter " data-level="9.8" data-path="ch9/ch9-08.html">
<a href="../ch9/ch9-08.html">
<i class="fa fa-check"></i>
<b>9.8.</b>
Goroutines和線程
</a>
</li>
</ul>
</li>
<li class="chapter " data-level="10" data-path="ch10/ch10.html">
<a href="../ch10/ch10.html">
<i class="fa fa-check"></i>
<b>10.</b>
包和工具
</a>
<ul class="articles">
<li class="chapter " data-level="10.1" data-path="ch10/ch10-01.html">
<a href="../ch10/ch10-01.html">
<i class="fa fa-check"></i>
<b>10.1.</b>
簡介
</a>
</li>
<li class="chapter " data-level="10.2" data-path="ch10/ch10-02.html">
<a href="../ch10/ch10-02.html">
<i class="fa fa-check"></i>
<b>10.2.</b>
導入路徑
</a>
</li>
<li class="chapter " data-level="10.3" data-path="ch10/ch10-03.html">
<a href="../ch10/ch10-03.html">
<i class="fa fa-check"></i>
<b>10.3.</b>
包聲明
</a>
</li>
<li class="chapter " data-level="10.4" data-path="ch10/ch10-04.html">
<a href="../ch10/ch10-04.html">
<i class="fa fa-check"></i>
<b>10.4.</b>
導入聲明
</a>
</li>
<li class="chapter " data-level="10.5" data-path="ch10/ch10-05.html">
<a href="../ch10/ch10-05.html">
<i class="fa fa-check"></i>
<b>10.5.</b>
匿名導入
</a>
</li>
<li class="chapter " data-level="10.6" data-path="ch10/ch10-06.html">
<a href="../ch10/ch10-06.html">
<i class="fa fa-check"></i>
<b>10.6.</b>
包和命名
</a>
</li>
<li class="chapter " data-level="10.7" data-path="ch10/ch10-07.html">
<a href="../ch10/ch10-07.html">
<i class="fa fa-check"></i>
<b>10.7.</b>
工具
</a>
</li>
</ul>
</li>
<li class="chapter " data-level="11" data-path="ch11/ch11.html">
<a href="../ch11/ch11.html">
<i class="fa fa-check"></i>
<b>11.</b>
測試
</a>
<ul class="articles">
<li class="chapter " data-level="11.1" data-path="ch11/ch11-01.html">
<a href="../ch11/ch11-01.html">
<i class="fa fa-check"></i>
<b>11.1.</b>
go test
</a>
</li>
<li class="chapter " 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="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="36-&#x5E38;&#x91CF;">3.6. &#x5E38;&#x91CF;</h2>
<p>&#x5E38;&#x91CF;&#x8868;&#x9054;&#x5F0F;&#x7684;&#x503C;&#x5728;&#x7DE8;&#x8B6F;&#x671F;&#x8A08;&#x7B97;, &#x800C;&#x4E0D;&#x662F;&#x5728;&#x904B;&#x884C;&#x671F;. &#x6BCF;&#x7A2E;&#x5E38;&#x91CF;&#x7684;&#x6F5B;&#x5728;&#x985E;&#x578B;&#x90FD;&#x662F;&#x57FA;&#x790E;&#x985E;&#x578B;: boolean, string, &#x6216;&#x6578;&#x5B57;.</p>
<p>&#x4E00;&#x500B;&#x5E38;&#x91CF;&#x7684;&#x8072;&#x660E;&#x8A9E;&#x53E5;&#x5B9A;&#x7FA9;&#x4E86;&#x5E38;&#x91CF;&#x7684;&#x540D;&#x5B57;, &#x548C;&#x8B8A;&#x91CF;&#x7684;&#x8072;&#x660E;&#x8A9E;&#x6CD5;&#x985E;&#x4F3C;, &#x5E38;&#x91CF;&#x7684;&#x503C;&#x4E0D;&#x53EF;&#x8129;&#x6539;, &#x9019;&#x6A23;&#x53EF;&#x4EE5;&#x9632;&#x6B62;&#x5728;&#x904B;&#x884C;&#x671F;&#x88AB;&#x610F;&#x5916;&#x6216;&#x60E1;&#x610F;&#x7684;&#x8129;&#x6539;. &#x4F8B;&#x5982;, &#x5E38;&#x91CF;&#x6BD4;&#x8B8A;&#x91CF;&#x66F4;&#x9069;&#x5408;&#x7528;&#x65BC;&#x8868;&#x9054;&#x50CF; &#x3C0; &#x4E4B;&#x985E;&#x7684;&#x6578;&#x5B78;&#x5E38;&#x6578;, &#x56E0;&#x7232;&#x5B83;&#x5011;&#x7684;&#x503C;&#x4E0D;&#x6703;&#x8B8A;&#x5316;:</p>
<pre><code class="lang-Go"><span class="hljs-keyword">const</span> pi = <span class="hljs-number">3.14159</span> <span class="hljs-comment">// approximately; math.Pi is a better approximation</span>
</code></pre>
<p>&#x548C;&#x8B8A;&#x91CF;&#x8072;&#x660E;&#x4E00;&#x6A23;, &#x53EF;&#x4EE5;&#x6279;&#x91CF;&#x8072;&#x660E;&#x591A;&#x500B;&#x5E38;&#x91CF;; &#x9019;&#x6BD4;&#x8F03;&#x9069;&#x5408;&#x8072;&#x660E;&#x4E00;&#x7D44;&#x76F8;&#x95DC;&#x7684;&#x5E38;&#x91CF;:</p>
<pre><code class="lang-Go"><span class="hljs-keyword">const</span> (
e = <span class="hljs-number">2.71828182845904523536028747135266249775724709369995957496696763</span>
pi = <span class="hljs-number">3.14159265358979323846264338327950288419716939937510582097494459</span>
)
</code></pre>
<p>&#x8A31;&#x591A;&#x5E38;&#x91CF;&#x7684;&#x904B;&#x7B97;&#x53EF;&#x4EE5;&#x5728;&#x7DE8;&#x8B6F;&#x671F;&#x5B8C;&#x6210;, &#x9019;&#x6A23;&#x53EF;&#x4EE5;&#x6E1B;&#x5C11;&#x904B;&#x884C;&#x6642;&#x7684;&#x5DE5;&#x4F5C;, &#x4E5F;&#x65B9;&#x4FBF;&#x5176;&#x4ED6;&#x7DE8;&#x8B6F;&#x512A;&#x5316;. &#x7576;&#x64CD;&#x4F5C;&#x6578;&#x662F;&#x5E38;&#x91CF;&#x6642;, &#x4E00;&#x4E9B;&#x904B;&#x884C;&#x6642;&#x7684;&#x932F;&#x8AA4;&#x53EF;&#x4EE5;&#x5728;&#x7DE8;&#x8B6F;&#x6642;&#x767C;&#x73FE;, &#x4F8B;&#x5982;&#x6574;&#x6578;&#x9664;&#x96F6;, &#x5B57;&#x7B26;&#x4E32;&#x7D22;&#x5F15;&#x8D8A;&#x754C;, &#x4EFB;&#x4F55;&#x5C0E;&#x81F4;&#x7121;&#x6548;&#x6D6E;&#x9EDE;&#x6578;&#x7684;&#x64CD;&#x4F5C;&#x7B49;.</p>
<p>&#x5E38;&#x91CF;&#x9593;&#x7684;&#x6240;&#x6709;&#x7B97;&#x8853;&#x904B;&#x7B97;, &#x908F;&#x8F2F;&#x904B;&#x7B97;&#x548C;&#x6BD4;&#x8F03;&#x904B;&#x7B97;&#x7684;&#x7D50;&#x679C;&#x4E5F;&#x662F;&#x5E38;&#x91CF;, &#x5C0D;&#x5E38;&#x91CF;&#x7684;&#x985E;&#x578B;&#x8F49;&#x63DB;&#x64CD;&#x4F5C;&#x6216;&#x4EE5;&#x4E0B;&#x51FD;&#x6578;&#x8ABF;&#x7528;&#x90FD;&#x662F;&#x8FD4;&#x8FF4;&#x5E38;&#x91CF;&#x7D50;&#x679C;: len, cap, real, imag, complex, &#x548C; unsafe.Sizeof(&#xA7;13.1).</p>
<p>&#x56E0;&#x7232;&#x5B83;&#x5011;&#x7684;&#x503C;&#x662F;&#x5728;&#x7DE8;&#x8B6F;&#x671F;&#x5C31;&#x78BA;&#x5B9A;&#x7684;, &#x56E0;&#x6B64;&#x5E38;&#x91CF;&#x53EF;&#x4EE5;&#x662F;&#x69CB;&#x6210;&#x985E;&#x578B;&#x7684;&#x4E00;&#x90E8;&#x5206;, &#x4F8B;&#x5982;&#x7528;&#x65BC;&#x6307;&#x5B9A;&#x6578;&#x7D44;&#x985E;&#x578B;&#x7684;&#x9577;&#x5EA6;:</p>
<pre><code class="lang-Go"><span class="hljs-keyword">const</span> IPv4Len = <span class="hljs-number">4</span>
<span class="hljs-comment">// parseIPv4 parses an IPv4 address (d.d.d.d).</span>
<span class="hljs-keyword">func</span> parseIPv4(s <span class="hljs-typename">string</span>) IP {
<span class="hljs-keyword">var</span> p [IPv4Len]<span class="hljs-typename">byte</span>
<span class="hljs-comment">// ...</span>
}
</code></pre>
<p>&#x4E00;&#x500B;&#x5E38;&#x91CF;&#x7684;&#x8072;&#x660E;&#x4E5F;&#x53EF;&#x4EE5;&#x5305;&#x542B;&#x4E00;&#x500B;&#x985E;&#x578B;&#x548C;&#x4E00;&#x500B;&#x503C;, &#x4F46;&#x662F;&#x5982;&#x679C;&#x6C92;&#x6709;&#x986F;&#x5F0F;&#x6307;&#x660E;&#x985E;&#x578B;, &#x90A3;&#x9EBD;&#x5C07;&#x5F9E;&#x53F3;&#x908A;&#x7684;&#x8868;&#x9054;&#x5F0F;&#x63A8;&#x65B7;&#x985E;&#x578B;. &#x5728;&#x4E0B;&#x9762;&#x7684;&#x4EE3;&#x78BC;&#x4E2D;, time.Duration &#x662F;&#x4E00;&#x500B;&#x547D;&#x540D;&#x985E;&#x578B;, &#x5E95;&#x5C64;&#x985E;&#x578B;&#x662F; int64, time.Minute &#x662F;&#x5C0D;&#x61C9;&#x985E;&#x578B;&#x7684;&#x5E38;&#x91CF;. &#x4E0B;&#x9762;&#x8072;&#x660E;&#x7684;&#x5169;&#x500B;&#x5E38;&#x91CF;&#x90FD;&#x662F; time.Duration &#x985E;&#x578B;, &#x53EF;&#x4EE5;&#x901A;&#x904E; %T &#x53C3;&#x6578;&#x6253;&#x5370;&#x985E;&#x578B;&#x4FE1;&#x606F;:</p>
<pre><code class="lang-Go"><span class="hljs-keyword">const</span> noDelay time.Duration = <span class="hljs-number">0</span>
<span class="hljs-keyword">const</span> timeout = <span class="hljs-number">5</span> * time.Minute
fmt.Printf(<span class="hljs-string">&quot;%T %[1]v\n&quot;</span>, noDelay) <span class="hljs-comment">// &quot;time.Duration 0&quot;</span>
fmt.Printf(<span class="hljs-string">&quot;%T %[1]v\n&quot;</span>, timeout) <span class="hljs-comment">// &quot;time.Duration 5m0s</span>
fmt.Printf(<span class="hljs-string">&quot;%T %[1]v\n&quot;</span>, time.Minute) <span class="hljs-comment">// &quot;time.Duration 1m0s&quot;</span>
</code></pre>
<p>&#x5982;&#x679C;&#x662F;&#x6279;&#x91CF;&#x8072;&#x660E;&#x7684;&#x5E38;&#x91CF;, &#x9664;&#x4E86;&#x7B2C;&#x4E00;&#x500B;&#x5916;&#x5176;&#x4ED6;&#x5E38;&#x91CF;&#x7684;&#x53F3;&#x908A;&#x7684;&#x8868;&#x767C;&#x751F;&#x53EF;&#x4EE5;&#x7701;&#x7565;, &#x5982;&#x679C;&#x7701;&#x7565;&#x5247;&#x8868;&#x793A;&#x4F7F;&#x7528;&#x524D;&#x9762;&#x7684;&#x8868;&#x9054;&#x5F0F;, &#x5C0D;&#x61C9;&#x7684;&#x5E38;&#x91CF;&#x985E;&#x578B;&#x4E5F;&#x4E00;&#x6A23;. &#x4F8B;&#x5982;:</p>
<pre><code class="lang-Go"><span class="hljs-keyword">const</span> (
a = <span class="hljs-number">1</span>
b
c = <span class="hljs-number">2</span>
d
)
fmt.Println(a, b, c, d) <span class="hljs-comment">// &quot;1 1 2 2&quot;</span>
</code></pre>
<p>&#x5982;&#x679C;&#x96BB;&#x662F;&#x7C21;&#x55AE;&#x5730;&#x8907;&#x88FD;&#x53F3;&#x908A;&#x7684;&#x5E38;&#x91CF;&#x8868;&#x9054;&#x5F0F;, &#x4F75;&#x6C92;&#x6709;&#x592A;&#x5BE6;&#x7528;&#x7684;&#x50F9;&#x503C;. &#x4F46;&#x662F;&#x5B83;&#x53EF;&#x4EE5;&#x5E36;&#x4F86;&#x5176;&#x4ED6;&#x7684;&#x7279;&#x6027;, &#x90A3;&#x5C31;&#x662F; iota &#x5E38;&#x91CF;&#x751F;&#x6210;&#x5668;.</p>
<h3 id="361-iota-&#x5E38;&#x91CF;&#x751F;&#x6210;&#x5668;">3.6.1. iota &#x5E38;&#x91CF;&#x751F;&#x6210;&#x5668;</h3>
<p>&#x5E38;&#x91CF;&#x8072;&#x660E;&#x53EF;&#x4EE5;&#x4F7F;&#x7528; iota &#x5E38;&#x91CF;&#x751F;&#x6210;&#x5668;, &#x7528;&#x65BC;&#x751F;&#x6210;&#x4E00;&#x7D44;&#x76F8;&#x4F3C;&#x7684;&#x5E38;&#x91CF;&#x503C;, &#x4F46;&#x662F;&#x4E0D;&#x7528;&#x6BCF;&#x884C;&#x90FD;&#x5BEB;&#x4E00;&#x904D;. &#x5728;&#x4E00;&#x500B; const &#x8072;&#x660E;&#x8A9E;&#x53E5;&#x4E2D;, &#x5728;&#x958B;&#x59CB;&#x4E00;&#x884C; iota &#x5C07;&#x6703;&#x88AB;&#x7F6E;&#x7232;0, &#x7136;&#x5F8C;&#x5728;&#x6BCF;&#x4E00;&#x500B;&#x6709;&#x5E38;&#x91CF;&#x8072;&#x660E;&#x7684;&#x884C;&#x52A0;&#x4E00;.</p>
<p>&#x4E0B;&#x9762;&#x662F;&#x4F86;&#x81EA; time &#x5305;&#x7684;&#x4F8B;&#x5B50;, &#x5B83;&#x9996;&#x5148;&#x5B9A;&#x7FA9;&#x4E86;Weekday&#x547D;&#x540D;&#x985E;&#x578B;, &#x7136;&#x5F8C;&#x7232;&#x4E00;&#x9031;&#x7684;&#x6BCF;&#x5929;&#x5B9A;&#x7FA9;&#x4E00;&#x500B;&#x5E38;&#x91CF;, &#x5F9E;&#x9031;&#x65E5;0&#x958B;&#x59CB;. &#x9019;&#x7A2E;&#x985E;&#x578B;&#x4E00;&#x822C;&#x88AB;&#x7A31;&#x7232;&#x679A;&#x8209;&#x985E;&#x578B;.</p>
<pre><code class="lang-Go"><span class="hljs-keyword">type</span> Weekday <span class="hljs-typename">int</span>
<span class="hljs-keyword">const</span> (
Sunday Weekday = <span class="hljs-constant">iota</span>
Monday
Tuesday
Wednesday
Thursday
Friday
Saturday
)
</code></pre>
<p>&#x9031;&#x4E00;&#x5C07;&#x5C0D;&#x61C9;0, &#x9031;&#x4E00;&#x7232;1, &#x5982;&#x6B64;&#x7B49;&#x7B49;.</p>
<p>&#x6211;&#x5011;&#x4E5F;&#x53EF;&#x4EE5;&#x5728;&#x8907;&#x96DC;&#x7684;&#x5E38;&#x91CF;&#x8868;&#x9054;&#x5F0F;&#x4E2D;&#x4F7F;&#x7528; iota, &#x4E0B;&#x9762;&#x662F;&#x4F86;&#x81EA; net &#x5305;&#x7684;&#x4F8B;&#x5B50;, &#x7528;&#x65BC;&#x7D66;&#x4E00;&#x500B;&#x7121;&#x7B26;&#x865F;&#x6574;&#x6578;&#x7684;&#x6700;&#x4F4E;5bit&#x7684;&#x6BCF;&#x500B;bit&#x7D66;&#x5B9A;&#x4E00;&#x500B;&#x540D;&#x5B57;:</p>
<pre><code class="lang-Go"><span class="hljs-keyword">type</span> Flags <span class="hljs-typename">uint</span>
<span class="hljs-keyword">const</span> (
FlagUp Flags = <span class="hljs-number">1</span> &lt;&lt; <span class="hljs-constant">iota</span> <span class="hljs-comment">// is up</span>
FlagBroadcast <span class="hljs-comment">// supports broadcast access capability</span>
FlagLoopback <span class="hljs-comment">// is a loopback interface</span>
FlagPointToPoint <span class="hljs-comment">// belongs to a point-to-point link</span>
FlagMulticast <span class="hljs-comment">// supports multicast access capability</span>
)
</code></pre>
<p>&#x96A8;&#x7740; iota &#x7684;&#x905E;&#x589E;, &#x6BCF;&#x500B;&#x5E38;&#x91CF;&#x5C0D;&#x61C9;&#x8868;&#x9054;&#x5F0F; 1 &lt;&lt; iota, &#x662F;&#x9023;&#x7E8C;&#x7684;2&#x7684;&#x51AA;, &#x5206;&#x522B;&#x5C0D;&#x61C9;&#x4E00;&#x500B;bit&#x4F4D;&#x7F6E;. &#x4F7F;&#x7528;&#x9019;&#x4E9B;&#x5E38;&#x91CF;&#x53EF;&#x4EE5;&#x6E2C;&#x8A66;, &#x8A2D;&#x7F6E;, &#x6216;&#x6E05;&#x9664;&#x5C0D;&#x61C9;&#x7684;bit&#x4F4D;&#x7684;&#x503C;:</p>
<pre><code class="lang-Go">gopl.io/ch3/netflag
<span class="hljs-keyword">func</span> IsUp(v Flags) <span class="hljs-typename">bool</span> { <span class="hljs-keyword">return</span> v&amp;FlagUp == FlagUp }
<span class="hljs-keyword">func</span> TurnDown(v *Flags) { *v &amp;^= FlagUp }
<span class="hljs-keyword">func</span> SetBroadcast(v *Flags) { *v |= FlagBroadcast }
<span class="hljs-keyword">func</span> IsCast(v Flags) <span class="hljs-typename">bool</span> { <span class="hljs-keyword">return</span> v&amp;(FlagBroadcast|FlagMulticast) != <span class="hljs-number">0</span> }
unc main() {
<span class="hljs-keyword">var</span> v Flags = FlagMulticast | FlagUp
fmt.Printf(<span class="hljs-string">&quot;%b %t\n&quot;</span>, v, IsUp(v)) <span class="hljs-comment">// &quot;10001 true&quot;</span>
TurnDown(&amp;v)
fmt.Printf(<span class="hljs-string">&quot;%b %t\n&quot;</span>, v, IsUp(v)) <span class="hljs-comment">// &quot;10000 false&quot;</span>
SetBroadcast(&amp;v)
fmt.Printf(<span class="hljs-string">&quot;%b %t\n&quot;</span>, v, IsUp(v)) <span class="hljs-comment">// &quot;10010 false&quot;</span>
fmt.Printf(<span class="hljs-string">&quot;%b %t\n&quot;</span>, v, IsCast(v)) <span class="hljs-comment">// &quot;10010 true&quot;</span>
}
</code></pre>
<p>&#x4E0B;&#x9762;&#x662F;&#x4E00;&#x500B;&#x66F4;&#x8907;&#x96DC;&#x7684;&#x4F8B;&#x5B50;, &#x6BCF;&#x500B;&#x5E38;&#x91CF;&#x90FD;&#x662F;1024&#x7684;&#x51AA;:</p>
<pre><code class="lang-Go"><span class="hljs-keyword">const</span> (
_ = <span class="hljs-number">1</span> &lt;&lt; (<span class="hljs-number">10</span> * <span class="hljs-constant">iota</span>)
KiB <span class="hljs-comment">// 1024</span>
MiB <span class="hljs-comment">// 1048576</span>
GiB <span class="hljs-comment">// 1073741824</span>
TiB <span class="hljs-comment">// 1099511627776 (exceeds 1 &lt;&lt; 32)</span>
PiB <span class="hljs-comment">// 1125899906842624</span>
EiB <span class="hljs-comment">// 1152921504606846976</span>
ZiB <span class="hljs-comment">// 1180591620717411303424 (exceeds 1 &lt;&lt; 64)</span>
YiB <span class="hljs-comment">// 1208925819614629174706176</span>
)
</code></pre>
<p>iota &#x6A5F;&#x88FD;&#x4E5F;&#x6709;&#x5176;&#x5C40;&#x9650;&#x6027;. &#x4F8B;&#x5982;, &#x5B83;&#x4F75;&#x4E0D;&#x80FD;&#x7528;&#x65BC;&#x7523;&#x751F;1000&#x7684;&#x51AA;(KB,MB,&#x7B49;&#x7B49;), &#x56E0;&#x7232;&#x4F75;&#x6C92;&#x6709;&#x8A08;&#x7B97;&#x51AA;&#x7684;&#x904B;&#x7B97;&#x7B26;.</p>
<p><strong>&#x7DF4;&#x7FD2;3.13:</strong> &#x7DE8;&#x5BEB;KB,MB&#x7684;&#x5E38;&#x91CF;&#x8072;&#x660E;, &#x7136;&#x5F8C;&#x64F4;&#x5C55;&#x5230;YB.</p>
<h3 id="362-&#x7121;&#x985E;&#x578B;&#x5E38;&#x91CF;">3.6.2. &#x7121;&#x985E;&#x578B;&#x5E38;&#x91CF;</h3>
<p>Go&#x8A9E;&#x8A00;&#x7684;&#x5E38;&#x91CF;&#x6709;&#x9EDE;&#x4E0D;&#x5C0B;&#x5E38;. &#x96D6;&#x7136;&#x4E00;&#x500B;&#x5E38;&#x91CF;&#x53EF;&#x4EE5;&#x6709;&#x4EFB;&#x610F;&#x6709;&#x4E00;&#x500B;&#x78BA;&#x5B9A;&#x7684;&#x57FA;&#x790E;&#x985E;&#x578B;, &#x4F8B;&#x5982; int &#x6216; float64, &#x6216;&#x8005;&#x662F;&#x985E;&#x4F3C; time.Duration &#x9019;&#x6A23;&#x547D;&#x540D;&#x7684;&#x57FA;&#x790E;&#x985E;&#x578B;, &#x4F46;&#x662F;&#x8A31;&#x591A;&#x5E38;&#x91CF;&#x4F75;&#x6C92;&#x6709;&#x4E00;&#x500B;&#x660E;&#x78BA;&#x7684;&#x57FA;&#x790E;&#x985E;&#x578B;. &#x7DE8;&#x8B6F;&#x671F;&#x7232;&#x9019;&#x4E9B;&#x6C92;&#x6709;&#x660E;&#x78BA;&#x7684;&#x57FA;&#x790E;&#x985E;&#x578B;&#x7684;&#x6578;&#x5B57;&#x5E38;&#x91CF;&#x63D0;&#x4F9B;&#x6BD4;&#x57FA;&#x790E;&#x985E;&#x578B;&#x6216;&#x6A5F;&#x5668;&#x66F4;&#x9AD8;&#x7CBE;&#x5EA6;&#x7684;&#x7B97;&#x8853;&#x904B;&#x7B97;; &#x4F60;&#x53EF;&#x4EE5;&#x8A8D;&#x7232;&#x81F3;&#x5C11;&#x6709;256bit&#x7684;&#x904B;&#x7B97;&#x7CBE;&#x5EA6;. &#x9019;&#x91CC;&#x6709;&#x516D;&#x7A2E;&#x672A;&#x660E;&#x78BA;&#x985E;&#x578B;&#x7684;&#x5E38;&#x91CF;&#x985E;&#x578B;, &#x5206;&#x522B;&#x662F; &#x7121;&#x985E;&#x578B;&#x7684;&#x5E03;&#x723E;&#x578B;, &#x7121;&#x985E;&#x578B;&#x7684;&#x6574;&#x6578;, &#x7121;&#x985E;&#x578B;&#x7684;&#x5B57;&#x7B26;, &#x7121;&#x985E;&#x578B;&#x7684;&#x6D6E;&#x9EDE;&#x6578;, &#x7121;&#x985E;&#x578B;&#x7684;&#x8907;&#x6578;, &#x7121;&#x985E;&#x578B;&#x7684;&#x5B57;&#x7B26;&#x4E32;.</p>
<p>&#x901A;&#x904E;&#x5EF6;&#x9072;&#x660E;&#x78BA;&#x5177;&#x9AD4;&#x985E;&#x578B;, &#x7121;&#x985E;&#x578B;&#x7684;&#x5E38;&#x91CF;&#x4E0D;&#x50C5;&#x53EF;&#x4EE5;&#x63D0;&#x4F9B;&#x66F4;&#x9AD8;&#x7684;&#x7CBE;&#x5EA6;, &#x800C;&#x4E14;&#x53EF;&#x4EE5;&#x76F4;&#x63A5;&#x7528;&#x65BC;&#x66F4;&#x591A;&#x7684;&#x8868;&#x9054;&#x5F0F;&#x800C;&#x4E0D;&#x9700;&#x8981;&#x985E;&#x578B;&#x8F49;&#x63DB;. &#x4F8B;&#x5982; &#x4F8B;&#x5B50;&#x4E2D;&#x7684; ZiB &#x548C; YiB &#x7684;&#x503C;&#x5DF2;&#x7D93;&#x8D85;&#x9F63;&#x4EFB;&#x4F55;Go&#x4E2D;&#x6574;&#x6578;&#x985E;&#x578B;&#x80FD;&#x8868;&#x9054;&#x7684;&#x8303;&#x570D;, &#x4F46;&#x662F;&#x5B83;&#x5011;&#x4F9D;&#x7136;&#x662F;&#x5408;&#x6CD5;&#x7684;&#x5E38;&#x91CF;, &#x800C;&#x4E14;&#x53EF;&#x4EE5;&#x50CF;&#x4E0B;&#x9762;&#x8868;&#x9054;&#x5F0F;&#x9019;&#x6A23;&#x4F7F;&#x7528;:</p>
<pre><code class="lang-Go">fmt.Println(YiB/ZiB) <span class="hljs-comment">// &quot;1024&quot;</span>
</code></pre>
<p>&#x53E6;&#x4E00;&#x500B;&#x4F8B;&#x5B50;, math.Pi &#x7121;&#x985E;&#x578B;&#x7684;&#x6D6E;&#x9EDE;&#x6578;&#x5E38;&#x91CF;, &#x53EF;&#x4EE5;&#x76F4;&#x63A5;&#x7528;&#x65BC;&#x4EFB;&#x610F;&#x9700;&#x8981;&#x6D6E;&#x9EDE;&#x6578;&#x6216;&#x8907;&#x6578;&#x7684;&#x5730;&#x65B9;:</p>
<pre><code class="lang-Go"><span class="hljs-keyword">var</span> x <span class="hljs-typename">float32</span> = math.Pi
<span class="hljs-keyword">var</span> y <span class="hljs-typename">float64</span> = math.Pi
<span class="hljs-keyword">var</span> z <span class="hljs-typename">complex128</span> = math.Pi
</code></pre>
<p>&#x5982;&#x679C; math.Pi &#x88AB;&#x78BA;&#x5B9A;&#x7232;&#x7279;&#x5B9A;&#x985E;&#x578B;, &#x6BD4;&#x5982; float64, &#x90A3;&#x9EBD;&#x7D50;&#x679C;&#x7CBE;&#x5EA6;&#x53EF;&#x80FD;&#x6703;&#x4E0D;&#x4E00;&#x6A23;, &#x540C;&#x6642;&#x5C0D;&#x65BC;&#x9700;&#x8981;float32&#x6216;complex128&#x985E;&#x578B;&#x503C;&#x7684;&#x5730;&#x65B9;&#x6703;&#x9700;&#x8981;&#x4E00;&#x500B;&#x660E;&#x78BA;&#x7684;&#x985E;&#x578B;&#x8F49;&#x63DB;:</p>
<pre><code class="lang-Go"><span class="hljs-keyword">const</span> Pi64 <span class="hljs-typename">float64</span> = math.Pi
<span class="hljs-keyword">var</span> x <span class="hljs-typename">float32</span> = <span class="hljs-typename">float32</span>(Pi64)
<span class="hljs-keyword">var</span> y <span class="hljs-typename">float64</span> = Pi64
<span class="hljs-keyword">var</span> z <span class="hljs-typename">complex128</span> = <span class="hljs-typename">complex128</span>(Pi64)
</code></pre>
<p>&#x5C0D;&#x65BC;&#x5E38;&#x91CF;&#x9762;&#x503C;, &#x4E0D;&#x540C;&#x7684;&#x5BEB;&#x6CD5;&#x5C0D;&#x61C9;&#x4E0D;&#x540C;&#x7684;&#x985E;&#x578B;. &#x4F8B;&#x5982; 0, 0.0, 0i, &#x548C; &apos;\u0000&apos; &#x96D6;&#x7136;&#x6709;&#x7740;&#x76F8;&#x540C;&#x7684;&#x5E38;&#x91CF;&#x503C;, &#x4F46;&#x662F;&#x5B83;&#x5011;&#x5206;&#x522B;&#x5C0D;&#x61C9;&#x7121;&#x985E;&#x578B;&#x7684;&#x6574;&#x6578;,&#x7121;&#x985E;&#x578B;&#x7684;&#x6D6E;&#x9EDE;&#x6578;,&#x7121;&#x985E;&#x578B;&#x7684;&#x8907;&#x6578;,&#x548C;&#x7121;&#x985E;&#x578B;&#x7684;&#x5B57;&#x7B26;&#x7B49;&#x4E0D;&#x540C;&#x7684;&#x5E38;&#x91CF;&#x985E;&#x578B;. &#x540C;&#x6A23;, true &#x548C; false &#x4E5F;&#x662F;&#x7121;&#x985E;&#x578B;&#x7684;&#x5E03;&#x723E;&#x985E;&#x578B;, &#x5B57;&#x7B26;&#x4E32;&#x9762;&#x503C;&#x5E38;&#x91CF;&#x662F;&#x7121;&#x985E;&#x578B;&#x7684;&#x5B57;&#x7B26;&#x4E32;.</p>
<p>&#x524D;&#x9762;&#x8AAC;&#x904E;&#x9664;&#x6CD5;&#x904B;&#x7B97;&#x7B26; / &#x6839;&#x64DA;&#x64CD;&#x4F5C;&#x6578;&#x7684;&#x985E;&#x578B;&#x751F;&#x6210;&#x5C0D;&#x61C9;&#x985E;&#x578B;&#x7684;&#x7D50;&#x679C;. &#x56E0;&#x6B64;, &#x4E0D;&#x540C;&#x5BEB;&#x6CD5;&#x7684;&#x5E38;&#x91CF;&#x9664;&#x6CD5;&#x8868;&#x9054;&#x5F0F;&#x53EF;&#x80FD;&#x5C0D;&#x61C9;&#x4E0D;&#x540C;&#x7684;&#x7D50;&#x679C;:</p>
<pre><code class="lang-Go"><span class="hljs-keyword">var</span> f <span class="hljs-typename">float64</span> = <span class="hljs-number">212</span>
fmt.Println((f - <span class="hljs-number">32</span>) * <span class="hljs-number">5</span> / <span class="hljs-number">9</span>) <span class="hljs-comment">// &quot;100&quot;; (f - 32) * 5 is a float64</span>
fmt.Println(<span class="hljs-number">5</span> / <span class="hljs-number">9</span> * (f - <span class="hljs-number">32</span>)) <span class="hljs-comment">// &quot;0&quot;; 5/9 is an untyped integer, 0</span>
fmt.Println(<span class="hljs-number">5.0</span> / <span class="hljs-number">9.0</span> * (f - <span class="hljs-number">32</span>)) <span class="hljs-comment">// &quot;100&quot;; 5.0/9.0 is an untyped float</span>
</code></pre>
<p>&#x96BB;&#x6709;&#x5E38;&#x91CF;&#x53EF;&#x4EE5;&#x662F;&#x7121;&#x985E;&#x578B;&#x7684;. &#x7576;&#x4E00;&#x500B;&#x7121;&#x985E;&#x578B;&#x7684;&#x5E38;&#x91CF;&#x88AB;&#x8CE6;&#x503C;&#x7D66;&#x4E00;&#x500B;&#x8B8A;&#x91CF;, &#x5C31;&#x50CF;&#x4E0A;&#x9762;&#x7684;&#x7B2C;&#x4E00;&#x884C;&#x8A9E;&#x53E5;, &#x6216;&#x8005;&#x662F;&#x50CF;&#x5176;&#x9918;&#x4E09;&#x500B;&#x8A9E;&#x53E5;&#x4E2D;&#x53F3;&#x908A;&#x8868;&#x9054;&#x5F0F;&#x4E2D;&#x542B;&#x6709;&#x660E;&#x78BA;&#x985E;&#x578B;&#x7684;&#x503C;, &#x7121;&#x985E;&#x578B;&#x7684;&#x5E38;&#x91CF;&#x5C07;&#x6703;&#x88AB;&#x96B1;&#x5F0F;&#x8F49;&#x63DB;&#x7232;&#x5C0D;&#x61C9;&#x7684;&#x985E;&#x578B;, &#x5982;&#x679C;&#x53EF;&#x80FD;&#x7684;&#x8A71;.</p>
<pre><code class="lang-Go"><span class="hljs-keyword">var</span> f <span class="hljs-typename">float64</span> = <span class="hljs-number">3</span> + <span class="hljs-number">0i</span> <span class="hljs-comment">// untyped complex -&gt; float64</span>
f = <span class="hljs-number">2</span> <span class="hljs-comment">// untyped integer -&gt; float64</span>
f = <span class="hljs-number">1e123</span> <span class="hljs-comment">// untyped floating-point -&gt; float64</span>
f = <span class="hljs-string">&apos;a&apos;</span> <span class="hljs-comment">// untyped rune -&gt; float64</span>
</code></pre>
<p>&#x4E0A;&#x9762;&#x7684;&#x8A9E;&#x53E5;&#x76F8;&#x7576;&#x65BC;:</p>
<pre><code class="lang-Go"><span class="hljs-keyword">var</span> f <span class="hljs-typename">float64</span> = <span class="hljs-typename">float64</span>(<span class="hljs-number">3</span> + <span class="hljs-number">0i</span>)
f = <span class="hljs-typename">float64</span>(<span class="hljs-number">2</span>)
f = <span class="hljs-typename">float64</span>(<span class="hljs-number">1e123</span>)
f = <span class="hljs-typename">float64</span>(<span class="hljs-string">&apos;a&apos;</span>)
</code></pre>
<p>&#x7121;&#x8AD6;&#x662F;&#x96B1;&#x5F0F;&#x6216;&#x986F;&#x5F0F;, &#x5C07;&#x4E00;&#x7A2E;&#x985E;&#x578B;&#x8F49;&#x63DB;&#x7232;&#x53E6;&#x4E00;&#x7A2E;&#x985E;&#x578B;&#x8981;&#x6C42;&#x76EE;&#x6A19;&#x53EF;&#x4EE5;&#x8868;&#x793A;&#x539F;&#x59CB;&#x503C;. &#x5C0D;&#x65BC;&#x6D6E;&#x9EDE;&#x6578;&#x548C;&#x8907;&#x6578;, &#x53EF;&#x80FD;&#x6703;&#x6709;&#x820D;&#x5165;&#x8655;&#x7406;:</p>
<pre><code class="lang-Go"><span class="hljs-keyword">const</span> (
deadbeef = <span class="hljs-number">0xdeadbeef</span> <span class="hljs-comment">// untyped int with value 3735928559</span>
a = <span class="hljs-typename">uint32</span>(deadbeef) <span class="hljs-comment">// uint32 with value 3735928559</span>
b = <span class="hljs-typename">float32</span>(deadbeef) <span class="hljs-comment">// float32 with value 3735928576 (rounded up)</span>
c = <span class="hljs-typename">float64</span>(deadbeef) <span class="hljs-comment">// float64 with value 3735928559 (exact)</span>
d = <span class="hljs-typename">int32</span>(deadbeef) <span class="hljs-comment">// compile error: constant overflows int32</span>
e = <span class="hljs-typename">float64</span>(<span class="hljs-number">1e309</span>) <span class="hljs-comment">// compile error: constant overflows float64</span>
f = <span class="hljs-typename">uint</span>(-<span class="hljs-number">1</span>) <span class="hljs-comment">// compile error: constant underflows uint</span>
)
</code></pre>
<p>&#x5C0D;&#x65BC;&#x4E00;&#x500B;&#x6C92;&#x6709;&#x986F;&#x5F0F;&#x985E;&#x578B;&#x7684;&#x8B8A;&#x91CF;&#x8072;&#x660E;(&#x5305;&#x62EC;&#x77ED;&#x8B8A;&#x91CF;&#x8072;&#x660E;&#x8A9E;&#x6CD5;), &#x7121;&#x985E;&#x578B;&#x7684;&#x5E38;&#x91CF;&#x6703;&#x88AB;&#x96B1;&#x5F0F;&#x8F49;&#x7232;&#x9ED8;&#x8A8D;&#x7684;&#x8B8A;&#x91CF;&#x985E;&#x578B;, &#x5C31;&#x50CF;&#x4E0B;&#x9762;&#x7684;&#x4F8B;&#x5B50;:</p>
<pre><code class="lang-Go">i := <span class="hljs-number">0</span> <span class="hljs-comment">// untyped integer; implicit int(0)</span>
r := <span class="hljs-string">&apos;\000&apos;</span> <span class="hljs-comment">// untyped rune; implicit rune(&apos;\000&apos;)</span>
f := <span class="hljs-number">0.0</span> <span class="hljs-comment">// untyped floating-point; implicit float64(0.0)</span>
c := <span class="hljs-number">0i</span> <span class="hljs-comment">// untyped complex; implicit complex128(0i)</span>
</code></pre>
<p>&#x8A3B;&#x610F;&#x9ED8;&#x8A8D;&#x985E;&#x578B;&#x662F;&#x898F;&#x5247;&#x7684;: &#x7121;&#x985E;&#x578B;&#x7684;&#x6574;&#x6578;&#x5E38;&#x91CF;&#x9ED8;&#x8A8D;&#x8F49;&#x63DB;&#x7232;int, &#x5C0D;&#x61C9;&#x4E0D;&#x78BA;&#x5B9A;&#x7684;&#x5C3A;&#x5BF8;, &#x4F46;&#x662F;&#x6D6E;&#x9EDE;&#x6578;&#x597D;&#x8907;&#x6578;&#x5E38;&#x91CF;&#x5247;&#x9ED8;&#x8A8D;&#x8F49;&#x63DB;&#x7232;float64&#x548C;complex128. Go&#x8A9E;&#x8A00;&#x672C;&#x8EAB;&#x4F75;&#x6C92;&#x6709;&#x4E0D;&#x78BA;&#x5B9A;&#x7684;&#x5C3A;&#x5BF8;&#x7684;&#x6D6E;&#x9EDE;&#x6578;&#x548C;&#x8907;&#x6578;&#x985E;&#x578B;, &#x56E0;&#x7232;&#x5982;&#x4F55;&#x4E0D;&#x77E5;&#x9053;&#x6D6E;&#x9EDE;&#x6578;&#x985E;&#x578B;&#x7684;&#x8A71;&#x5F88;&#x96E3;&#x5BEB;&#x9F63;&#x6B63;&#x78BA;&#x7684;&#x6578;&#x503C;&#x7B97;&#x6CD5;.</p>
<p>&#x5982;&#x679C;&#x8981;&#x7D66;&#x8B8A;&#x91CF;&#x4E00;&#x500B;&#x4E0D;&#x540C;&#x7684;&#x985E;&#x578B;, &#x6211;&#x5011;&#x5FC5;&#x9B1A;&#x986F;&#x5F0F;&#x5730;&#x5C07;&#x7121;&#x985E;&#x578B;&#x7684;&#x5E38;&#x91CF;&#x8F49;&#x5316;&#x7232;&#x6240;&#x9700;&#x7684;&#x985E;&#x578B;, &#x6216;&#x7D66;&#x8072;&#x660E;&#x7684;&#x8B8A;&#x91CF;&#x6307;&#x5B9A;&#x985E;&#x578B;, &#x50CF;&#x4E0B;&#x9762;&#x4F8B;&#x5B50;&#x9019;&#x6A23;:</p>
<pre><code class="lang-Go"><span class="hljs-keyword">var</span> i = <span class="hljs-typename">int8</span>(<span class="hljs-number">0</span>)
<span class="hljs-keyword">var</span> i <span class="hljs-typename">int8</span> = <span class="hljs-number">0</span>
</code></pre>
<p>&#x7576;&#x5690;&#x8A66;&#x5C07;&#x9019;&#x4E9B;&#x7121;&#x985E;&#x578B;&#x7684;&#x5E38;&#x91CF;&#x8F49;&#x7232;&#x4E00;&#x500B;&#x63A5;&#x53E3;&#x503C;&#x6642;(&#x898B;&#x7B2C;7&#x7AE0;), &#x9019;&#x4E9B;&#x9ED8;&#x8A8D;&#x985E;&#x578B;&#x5C07;&#x986F;&#x5F97;&#x5C24;&#x7232;&#x91CD;&#x8981;, &#x56E0;&#x7232;&#x8981;&#x9760;&#x5B83;&#x5011;&#x660E;&#x78BA;&#x63A5;&#x53E3;&#x5C0D;&#x61C9;&#x7684;&#x52D5;&#x614B;&#x985E;&#x578B;.</p>
<pre><code class="lang-Go">fmt.Printf(<span class="hljs-string">&quot;%T\n&quot;</span>, <span class="hljs-number">0</span>) <span class="hljs-comment">// &quot;int&quot;</span>
fmt.Printf(<span class="hljs-string">&quot;%T\n&quot;</span>, <span class="hljs-number">0.0</span>) <span class="hljs-comment">// &quot;float64&quot;</span>
fmt.Printf(<span class="hljs-string">&quot;%T\n&quot;</span>, <span class="hljs-number">0i</span>) <span class="hljs-comment">// &quot;complex128&quot;</span>
fmt.Printf(<span class="hljs-string">&quot;%T\n&quot;</span>, <span class="hljs-string">&apos;\000&apos;</span>) <span class="hljs-comment">// &quot;int32&quot; (rune)</span>
</code></pre>
<p>&#x73FE;&#x5728;&#x6211;&#x5011;&#x5DF2;&#x7D93;&#x8B1B;&#x8FF0;&#x4E86;Go&#x8A9E;&#x8A00;&#x4E2D;&#x5168;&#x90E8;&#x7684;&#x57FA;&#x790E;&#x6578;&#x64DA;&#x985E;&#x578B;. &#x4E0B;&#x4E00;&#x6B65;&#x5C07;&#x6F14;&#x793A;&#x5982;&#x4F55;&#x7528;&#x57FA;&#x790E;&#x6578;&#x64DA;&#x985E;&#x578B;&#x7D44;&#x5408;&#x6210;&#x6578;&#x7D44;&#x6216;&#x7D50;&#x69CB;&#x9AD4;&#x7B49;&#x8907;&#x96DC;&#x6578;&#x64DA;&#x985E;&#x578B;, &#x7136;&#x5F8C;&#x69CB;&#x5EFA;&#x7528;&#x65BC;&#x89E3;&#x6C7A;&#x5BE6;&#x969B;&#x7DE8;&#x7A0B;&#x554F;&#x984C;&#x7684;&#x6578;&#x64DA;&#x7D50;&#x69CB;, &#x9019;&#x5C07;&#x662F;&#x7B2C;&#x56DB;&#x7AE0;&#x7684;&#x8A0E;&#x8AD6;&#x4E3B;&#x984C;.</p>
</section>
</div>
</div>
</div>
<a href="../ch3/ch3-05.html" class="navigation navigation-prev " aria-label="Previous page: 字符串"><i class="fa fa-angle-left"></i></a>
<a href="../ch4/ch4.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>