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

2189 lines
91 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.6.6">
<meta name="HandheldFriendly" content="true"/>
<meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-status-bar-style" content="black">
<link rel="apple-touch-icon-precomposed" sizes="152x152" href="../gitbook/images/apple-touch-icon-precomposed-152.png">
<link rel="shortcut icon" href="../gitbook/images/favicon.ico" type="image/x-icon">
<link rel="stylesheet" href="../gitbook/style.css">
<link rel="stylesheet" href="../gitbook/plugins/gitbook-plugin-katex/katex.min.css">
<link rel="stylesheet" href="../gitbook/plugins/gitbook-plugin-highlight/website.css">
<link rel="stylesheet" href="../gitbook/plugins/gitbook-plugin-fontsettings/website.css">
<link rel="next" href="../ch3/ch3-02.html" />
<link rel="prev" href="../ch3/ch3.html" />
</head>
<body>
<div class="book"
data-level="3.1"
data-chapter-title="整型"
data-filepath="ch3/ch3-01.md"
data-basepath=".."
data-revision="Sat Jan 02 2016 16:00:23 GMT+0800 (中国标准时间)"
data-innerlanguage="">
<div class="book-summary">
<nav role="navigation">
<ul class="summary">
<li class="chapter " data-level="0" data-path="index.html">
<a href="../index.html">
<i class="fa fa-check"></i>
前言
</a>
<ul class="articles">
<li class="chapter " data-level="0.1" data-path="ch0/ch0-01.html">
<a href="../ch0/ch0-01.html">
<i class="fa fa-check"></i>
<b>0.1.</b>
Go語言起源
</a>
</li>
<li class="chapter " data-level="0.2" data-path="ch0/ch0-02.html">
<a href="../ch0/ch0-02.html">
<i class="fa fa-check"></i>
<b>0.2.</b>
Go語言項目
</a>
</li>
<li class="chapter " data-level="0.3" data-path="ch0/ch0-03.html">
<a href="../ch0/ch0-03.html">
<i class="fa fa-check"></i>
<b>0.3.</b>
本書的組織
</a>
</li>
<li class="chapter " data-level="0.4" data-path="ch0/ch0-04.html">
<a href="../ch0/ch0-04.html">
<i class="fa fa-check"></i>
<b>0.4.</b>
更多的信息
</a>
</li>
<li class="chapter " data-level="0.5" data-path="ch0/ch0-05.html">
<a href="../ch0/ch0-05.html">
<i class="fa fa-check"></i>
<b>0.5.</b>
致謝
</a>
</li>
</ul>
</li>
<li class="chapter " data-level="1" data-path="ch1/ch1.html">
<a href="../ch1/ch1.html">
<i class="fa fa-check"></i>
<b>1.</b>
入門
</a>
<ul class="articles">
<li class="chapter " data-level="1.1" data-path="ch1/ch1-01.html">
<a href="../ch1/ch1-01.html">
<i class="fa fa-check"></i>
<b>1.1.</b>
Hello, World
</a>
</li>
<li class="chapter " data-level="1.2" data-path="ch1/ch1-02.html">
<a href="../ch1/ch1-02.html">
<i class="fa fa-check"></i>
<b>1.2.</b>
命令行參數
</a>
</li>
<li class="chapter " data-level="1.3" data-path="ch1/ch1-03.html">
<a href="../ch1/ch1-03.html">
<i class="fa fa-check"></i>
<b>1.3.</b>
査找重複的行
</a>
</li>
<li class="chapter " data-level="1.4" data-path="ch1/ch1-04.html">
<a href="../ch1/ch1-04.html">
<i class="fa fa-check"></i>
<b>1.4.</b>
GIF動畵
</a>
</li>
<li class="chapter " data-level="1.5" data-path="ch1/ch1-05.html">
<a href="../ch1/ch1-05.html">
<i class="fa fa-check"></i>
<b>1.5.</b>
獲取URL
</a>
</li>
<li class="chapter " data-level="1.6" data-path="ch1/ch1-06.html">
<a href="../ch1/ch1-06.html">
<i class="fa fa-check"></i>
<b>1.6.</b>
併發獲取多個URL
</a>
</li>
<li class="chapter " data-level="1.7" data-path="ch1/ch1-07.html">
<a href="../ch1/ch1-07.html">
<i class="fa fa-check"></i>
<b>1.7.</b>
Web服務
</a>
</li>
<li class="chapter " data-level="1.8" data-path="ch1/ch1-08.html">
<a href="../ch1/ch1-08.html">
<i class="fa fa-check"></i>
<b>1.8.</b>
本章要點
</a>
</li>
</ul>
</li>
<li class="chapter " data-level="2" data-path="ch2/ch2.html">
<a href="../ch2/ch2.html">
<i class="fa fa-check"></i>
<b>2.</b>
程序結構
</a>
<ul class="articles">
<li class="chapter " data-level="2.1" data-path="ch2/ch2-01.html">
<a href="../ch2/ch2-01.html">
<i class="fa fa-check"></i>
<b>2.1.</b>
命名
</a>
</li>
<li class="chapter " data-level="2.2" data-path="ch2/ch2-02.html">
<a href="../ch2/ch2-02.html">
<i class="fa fa-check"></i>
<b>2.2.</b>
聲明
</a>
</li>
<li class="chapter " data-level="2.3" data-path="ch2/ch2-03.html">
<a href="../ch2/ch2-03.html">
<i class="fa fa-check"></i>
<b>2.3.</b>
變量
</a>
</li>
<li class="chapter " data-level="2.4" data-path="ch2/ch2-04.html">
<a href="../ch2/ch2-04.html">
<i class="fa fa-check"></i>
<b>2.4.</b>
賦值
</a>
</li>
<li class="chapter " data-level="2.5" data-path="ch2/ch2-05.html">
<a href="../ch2/ch2-05.html">
<i class="fa fa-check"></i>
<b>2.5.</b>
類型
</a>
</li>
<li class="chapter " data-level="2.6" data-path="ch2/ch2-06.html">
<a href="../ch2/ch2-06.html">
<i class="fa fa-check"></i>
<b>2.6.</b>
包和文件
</a>
</li>
<li class="chapter " data-level="2.7" data-path="ch2/ch2-07.html">
<a href="../ch2/ch2-07.html">
<i class="fa fa-check"></i>
<b>2.7.</b>
作用域
</a>
</li>
</ul>
</li>
<li class="chapter " data-level="3" data-path="ch3/ch3.html">
<a href="../ch3/ch3.html">
<i class="fa fa-check"></i>
<b>3.</b>
基礎數據類型
</a>
<ul class="articles">
<li class="chapter active" data-level="3.1" data-path="ch3/ch3-01.html">
<a href="../ch3/ch3-01.html">
<i class="fa fa-check"></i>
<b>3.1.</b>
整型
</a>
</li>
<li class="chapter " data-level="3.2" data-path="ch3/ch3-02.html">
<a href="../ch3/ch3-02.html">
<i class="fa fa-check"></i>
<b>3.2.</b>
浮點數
</a>
</li>
<li class="chapter " data-level="3.3" data-path="ch3/ch3-03.html">
<a href="../ch3/ch3-03.html">
<i class="fa fa-check"></i>
<b>3.3.</b>
複數
</a>
</li>
<li class="chapter " data-level="3.4" data-path="ch3/ch3-04.html">
<a href="../ch3/ch3-04.html">
<i class="fa fa-check"></i>
<b>3.4.</b>
布爾型
</a>
</li>
<li class="chapter " data-level="3.5" data-path="ch3/ch3-05.html">
<a href="../ch3/ch3-05.html">
<i class="fa fa-check"></i>
<b>3.5.</b>
字符串
</a>
</li>
<li class="chapter " data-level="3.6" data-path="ch3/ch3-06.html">
<a href="../ch3/ch3-06.html">
<i class="fa fa-check"></i>
<b>3.6.</b>
常量
</a>
</li>
</ul>
</li>
<li class="chapter " data-level="4" data-path="ch4/ch4.html">
<a href="../ch4/ch4.html">
<i class="fa fa-check"></i>
<b>4.</b>
複合數據類型
</a>
<ul class="articles">
<li class="chapter " data-level="4.1" data-path="ch4/ch4-01.html">
<a href="../ch4/ch4-01.html">
<i class="fa fa-check"></i>
<b>4.1.</b>
數組
</a>
</li>
<li class="chapter " data-level="4.2" data-path="ch4/ch4-02.html">
<a href="../ch4/ch4-02.html">
<i class="fa fa-check"></i>
<b>4.2.</b>
Slice
</a>
</li>
<li class="chapter " data-level="4.3" data-path="ch4/ch4-03.html">
<a href="../ch4/ch4-03.html">
<i class="fa fa-check"></i>
<b>4.3.</b>
Map
</a>
</li>
<li class="chapter " data-level="4.4" data-path="ch4/ch4-04.html">
<a href="../ch4/ch4-04.html">
<i class="fa fa-check"></i>
<b>4.4.</b>
結構體
</a>
</li>
<li class="chapter " data-level="4.5" data-path="ch4/ch4-05.html">
<a href="../ch4/ch4-05.html">
<i class="fa fa-check"></i>
<b>4.5.</b>
JSON
</a>
</li>
<li class="chapter " data-level="4.6" data-path="ch4/ch4-06.html">
<a href="../ch4/ch4-06.html">
<i class="fa fa-check"></i>
<b>4.6.</b>
文本和HTML模闆
</a>
</li>
</ul>
</li>
<li class="chapter " data-level="5" data-path="ch5/ch5.html">
<a href="../ch5/ch5.html">
<i class="fa fa-check"></i>
<b>5.</b>
函數
</a>
<ul class="articles">
<li class="chapter " data-level="5.1" data-path="ch5/ch5-01.html">
<a href="../ch5/ch5-01.html">
<i class="fa fa-check"></i>
<b>5.1.</b>
函數聲明
</a>
</li>
<li class="chapter " data-level="5.2" data-path="ch5/ch5-02.html">
<a href="../ch5/ch5-02.html">
<i class="fa fa-check"></i>
<b>5.2.</b>
遞歸
</a>
</li>
<li class="chapter " data-level="5.3" data-path="ch5/ch5-03.html">
<a href="../ch5/ch5-03.html">
<i class="fa fa-check"></i>
<b>5.3.</b>
多返迴值
</a>
</li>
<li class="chapter " data-level="5.4" data-path="ch5/ch5-04.html">
<a href="../ch5/ch5-04.html">
<i class="fa fa-check"></i>
<b>5.4.</b>
錯誤
</a>
</li>
<li class="chapter " data-level="5.5" data-path="ch5/ch5-05.html">
<a href="../ch5/ch5-05.html">
<i class="fa fa-check"></i>
<b>5.5.</b>
函數值
</a>
</li>
<li class="chapter " data-level="5.6" data-path="ch5/ch5-06.html">
<a href="../ch5/ch5-06.html">
<i class="fa fa-check"></i>
<b>5.6.</b>
匿名函數
</a>
</li>
<li class="chapter " data-level="5.7" data-path="ch5/ch5-07.html">
<a href="../ch5/ch5-07.html">
<i class="fa fa-check"></i>
<b>5.7.</b>
可變參數
</a>
</li>
<li class="chapter " data-level="5.8" data-path="ch5/ch5-08.html">
<a href="../ch5/ch5-08.html">
<i class="fa fa-check"></i>
<b>5.8.</b>
Deferred函數
</a>
</li>
<li class="chapter " data-level="5.9" data-path="ch5/ch5-09.html">
<a href="../ch5/ch5-09.html">
<i class="fa fa-check"></i>
<b>5.9.</b>
Panic異常
</a>
</li>
<li class="chapter " data-level="5.10" data-path="ch5/ch5-10.html">
<a href="../ch5/ch5-10.html">
<i class="fa fa-check"></i>
<b>5.10.</b>
Recover捕獲異常
</a>
</li>
</ul>
</li>
<li class="chapter " data-level="6" data-path="ch6/ch6.html">
<a href="../ch6/ch6.html">
<i class="fa fa-check"></i>
<b>6.</b>
方法
</a>
<ul class="articles">
<li class="chapter " data-level="6.1" data-path="ch6/ch6-01.html">
<a href="../ch6/ch6-01.html">
<i class="fa fa-check"></i>
<b>6.1.</b>
方法聲明
</a>
</li>
<li class="chapter " data-level="6.2" data-path="ch6/ch6-02.html">
<a href="../ch6/ch6-02.html">
<i class="fa fa-check"></i>
<b>6.2.</b>
基於指針對象的方法
</a>
</li>
<li class="chapter " data-level="6.3" data-path="ch6/ch6-03.html">
<a href="../ch6/ch6-03.html">
<i class="fa fa-check"></i>
<b>6.3.</b>
通過嵌入結構體來擴展類型
</a>
</li>
<li class="chapter " data-level="6.4" data-path="ch6/ch6-04.html">
<a href="../ch6/ch6-04.html">
<i class="fa fa-check"></i>
<b>6.4.</b>
方法值和方法表達式
</a>
</li>
<li class="chapter " data-level="6.5" data-path="ch6/ch6-05.html">
<a href="../ch6/ch6-05.html">
<i class="fa fa-check"></i>
<b>6.5.</b>
示例: Bit數組
</a>
</li>
<li class="chapter " data-level="6.6" data-path="ch6/ch6-06.html">
<a href="../ch6/ch6-06.html">
<i class="fa fa-check"></i>
<b>6.6.</b>
封裝
</a>
</li>
</ul>
</li>
<li class="chapter " data-level="7" data-path="ch7/ch7.html">
<a href="../ch7/ch7.html">
<i class="fa fa-check"></i>
<b>7.</b>
接口
</a>
<ul class="articles">
<li class="chapter " data-level="7.1" data-path="ch7/ch7-01.html">
<a href="../ch7/ch7-01.html">
<i class="fa fa-check"></i>
<b>7.1.</b>
接口是合約
</a>
</li>
<li class="chapter " data-level="7.2" data-path="ch7/ch7-02.html">
<a href="../ch7/ch7-02.html">
<i class="fa fa-check"></i>
<b>7.2.</b>
接口類型
</a>
</li>
<li class="chapter " data-level="7.3" data-path="ch7/ch7-03.html">
<a href="../ch7/ch7-03.html">
<i class="fa fa-check"></i>
<b>7.3.</b>
實現接口的條件
</a>
</li>
<li class="chapter " data-level="7.4" data-path="ch7/ch7-04.html">
<a href="../ch7/ch7-04.html">
<i class="fa fa-check"></i>
<b>7.4.</b>
flag.Value接口
</a>
</li>
<li class="chapter " data-level="7.5" data-path="ch7/ch7-05.html">
<a href="../ch7/ch7-05.html">
<i class="fa fa-check"></i>
<b>7.5.</b>
接口值
</a>
</li>
<li class="chapter " data-level="7.6" data-path="ch7/ch7-06.html">
<a href="../ch7/ch7-06.html">
<i class="fa fa-check"></i>
<b>7.6.</b>
sort.Interface接口
</a>
</li>
<li class="chapter " data-level="7.7" data-path="ch7/ch7-07.html">
<a href="../ch7/ch7-07.html">
<i class="fa fa-check"></i>
<b>7.7.</b>
http.Handler接口
</a>
</li>
<li class="chapter " data-level="7.8" data-path="ch7/ch7-08.html">
<a href="../ch7/ch7-08.html">
<i class="fa fa-check"></i>
<b>7.8.</b>
error接口
</a>
</li>
<li class="chapter " data-level="7.9" data-path="ch7/ch7-09.html">
<a href="../ch7/ch7-09.html">
<i class="fa fa-check"></i>
<b>7.9.</b>
示例: 表達式求值
</a>
</li>
<li class="chapter " data-level="7.10" data-path="ch7/ch7-10.html">
<a href="../ch7/ch7-10.html">
<i class="fa fa-check"></i>
<b>7.10.</b>
類型斷言
</a>
</li>
<li class="chapter " data-level="7.11" data-path="ch7/ch7-11.html">
<a href="../ch7/ch7-11.html">
<i class="fa fa-check"></i>
<b>7.11.</b>
基於類型斷言識别錯誤類型
</a>
</li>
<li class="chapter " data-level="7.12" data-path="ch7/ch7-12.html">
<a href="../ch7/ch7-12.html">
<i class="fa fa-check"></i>
<b>7.12.</b>
通過類型斷言査詢接口
</a>
</li>
<li class="chapter " data-level="7.13" data-path="ch7/ch7-13.html">
<a href="../ch7/ch7-13.html">
<i class="fa fa-check"></i>
<b>7.13.</b>
類型分支
</a>
</li>
<li class="chapter " data-level="7.14" data-path="ch7/ch7-14.html">
<a href="../ch7/ch7-14.html">
<i class="fa fa-check"></i>
<b>7.14.</b>
示例: 基於標記的XML解碼
</a>
</li>
<li class="chapter " data-level="7.15" data-path="ch7/ch7-15.html">
<a href="../ch7/ch7-15.html">
<i class="fa fa-check"></i>
<b>7.15.</b>
補充幾點
</a>
</li>
</ul>
</li>
<li class="chapter " data-level="8" data-path="ch8/ch8.html">
<a href="../ch8/ch8.html">
<i class="fa fa-check"></i>
<b>8.</b>
Goroutines和Channels
</a>
<ul class="articles">
<li class="chapter " data-level="8.1" data-path="ch8/ch8-01.html">
<a href="../ch8/ch8-01.html">
<i class="fa fa-check"></i>
<b>8.1.</b>
Goroutines
</a>
</li>
<li class="chapter " data-level="8.2" data-path="ch8/ch8-02.html">
<a href="../ch8/ch8-02.html">
<i class="fa fa-check"></i>
<b>8.2.</b>
示例: 併發的Clock服務
</a>
</li>
<li class="chapter " data-level="8.3" data-path="ch8/ch8-03.html">
<a href="../ch8/ch8-03.html">
<i class="fa fa-check"></i>
<b>8.3.</b>
示例: 併發的Echo服務
</a>
</li>
<li class="chapter " data-level="8.4" data-path="ch8/ch8-04.html">
<a href="../ch8/ch8-04.html">
<i class="fa fa-check"></i>
<b>8.4.</b>
Channels
</a>
</li>
<li class="chapter " data-level="8.5" data-path="ch8/ch8-05.html">
<a href="../ch8/ch8-05.html">
<i class="fa fa-check"></i>
<b>8.5.</b>
併行的循環
</a>
</li>
<li class="chapter " data-level="8.6" data-path="ch8/ch8-06.html">
<a href="../ch8/ch8-06.html">
<i class="fa fa-check"></i>
<b>8.6.</b>
示例: 併發的Web爬蟲
</a>
</li>
<li class="chapter " data-level="8.7" data-path="ch8/ch8-07.html">
<a href="../ch8/ch8-07.html">
<i class="fa fa-check"></i>
<b>8.7.</b>
基於select的多路複用
</a>
</li>
<li class="chapter " data-level="8.8" data-path="ch8/ch8-08.html">
<a href="../ch8/ch8-08.html">
<i class="fa fa-check"></i>
<b>8.8.</b>
示例: 併發的字典遍歷
</a>
</li>
<li class="chapter " data-level="8.9" data-path="ch8/ch8-09.html">
<a href="../ch8/ch8-09.html">
<i class="fa fa-check"></i>
<b>8.9.</b>
併發的退出
</a>
</li>
<li class="chapter " data-level="8.10" data-path="ch8/ch8-10.html">
<a href="../ch8/ch8-10.html">
<i class="fa fa-check"></i>
<b>8.10.</b>
示例: 聊天服務
</a>
</li>
</ul>
</li>
<li class="chapter " data-level="9" data-path="ch9/ch9.html">
<a href="../ch9/ch9.html">
<i class="fa fa-check"></i>
<b>9.</b>
基於共享變量的併發
</a>
<ul class="articles">
<li class="chapter " data-level="9.1" data-path="ch9/ch9-01.html">
<a href="../ch9/ch9-01.html">
<i class="fa fa-check"></i>
<b>9.1.</b>
競爭條件
</a>
</li>
<li class="chapter " data-level="9.2" data-path="ch9/ch9-02.html">
<a href="../ch9/ch9-02.html">
<i class="fa fa-check"></i>
<b>9.2.</b>
sync.Mutex互斥鎖
</a>
</li>
<li class="chapter " data-level="9.3" data-path="ch9/ch9-03.html">
<a href="../ch9/ch9-03.html">
<i class="fa fa-check"></i>
<b>9.3.</b>
sync.RWMutex讀寫鎖
</a>
</li>
<li class="chapter " data-level="9.4" data-path="ch9/ch9-04.html">
<a href="../ch9/ch9-04.html">
<i class="fa fa-check"></i>
<b>9.4.</b>
內存同步
</a>
</li>
<li class="chapter " data-level="9.5" data-path="ch9/ch9-05.html">
<a href="../ch9/ch9-05.html">
<i class="fa fa-check"></i>
<b>9.5.</b>
sync.Once初始化
</a>
</li>
<li class="chapter " data-level="9.6" data-path="ch9/ch9-06.html">
<a href="../ch9/ch9-06.html">
<i class="fa fa-check"></i>
<b>9.6.</b>
競爭條件檢測
</a>
</li>
<li class="chapter " data-level="9.7" data-path="ch9/ch9-07.html">
<a href="../ch9/ch9-07.html">
<i class="fa fa-check"></i>
<b>9.7.</b>
示例: 併發的非阻塞緩存
</a>
</li>
<li class="chapter " data-level="9.8" data-path="ch9/ch9-08.html">
<a href="../ch9/ch9-08.html">
<i class="fa fa-check"></i>
<b>9.8.</b>
Goroutines和線程
</a>
</li>
</ul>
</li>
<li class="chapter " data-level="10" data-path="ch10/ch10.html">
<a href="../ch10/ch10.html">
<i class="fa fa-check"></i>
<b>10.</b>
包和工具
</a>
<ul class="articles">
<li class="chapter " data-level="10.1" data-path="ch10/ch10-01.html">
<a href="../ch10/ch10-01.html">
<i class="fa fa-check"></i>
<b>10.1.</b>
簡介
</a>
</li>
<li class="chapter " data-level="10.2" data-path="ch10/ch10-02.html">
<a href="../ch10/ch10-02.html">
<i class="fa fa-check"></i>
<b>10.2.</b>
導入路徑
</a>
</li>
<li class="chapter " data-level="10.3" data-path="ch10/ch10-03.html">
<a href="../ch10/ch10-03.html">
<i class="fa fa-check"></i>
<b>10.3.</b>
包聲明
</a>
</li>
<li class="chapter " data-level="10.4" data-path="ch10/ch10-04.html">
<a href="../ch10/ch10-04.html">
<i class="fa fa-check"></i>
<b>10.4.</b>
導入聲明
</a>
</li>
<li class="chapter " data-level="10.5" data-path="ch10/ch10-05.html">
<a href="../ch10/ch10-05.html">
<i class="fa fa-check"></i>
<b>10.5.</b>
匿名導入
</a>
</li>
<li class="chapter " data-level="10.6" data-path="ch10/ch10-06.html">
<a href="../ch10/ch10-06.html">
<i class="fa fa-check"></i>
<b>10.6.</b>
包和命名
</a>
</li>
<li class="chapter " data-level="10.7" data-path="ch10/ch10-07.html">
<a href="../ch10/ch10-07.html">
<i class="fa fa-check"></i>
<b>10.7.</b>
工具
</a>
</li>
</ul>
</li>
<li class="chapter " data-level="11" data-path="ch11/ch11.html">
<a href="../ch11/ch11.html">
<i class="fa fa-check"></i>
<b>11.</b>
測試
</a>
<ul class="articles">
<li class="chapter " data-level="11.1" data-path="ch11/ch11-01.html">
<a href="../ch11/ch11-01.html">
<i class="fa fa-check"></i>
<b>11.1.</b>
go test
</a>
</li>
<li class="chapter " data-level="11.2" data-path="ch11/ch11-02.html">
<a href="../ch11/ch11-02.html">
<i class="fa fa-check"></i>
<b>11.2.</b>
測試函數
</a>
</li>
<li class="chapter " data-level="11.3" data-path="ch11/ch11-03.html">
<a href="../ch11/ch11-03.html">
<i class="fa fa-check"></i>
<b>11.3.</b>
測試覆蓋率
</a>
</li>
<li class="chapter " data-level="11.4" data-path="ch11/ch11-04.html">
<a href="../ch11/ch11-04.html">
<i class="fa fa-check"></i>
<b>11.4.</b>
基準測試
</a>
</li>
<li class="chapter " data-level="11.5" data-path="ch11/ch11-05.html">
<a href="../ch11/ch11-05.html">
<i class="fa fa-check"></i>
<b>11.5.</b>
剖析
</a>
</li>
<li class="chapter " data-level="11.6" data-path="ch11/ch11-06.html">
<a href="../ch11/ch11-06.html">
<i class="fa fa-check"></i>
<b>11.6.</b>
示例函數
</a>
</li>
</ul>
</li>
<li class="chapter " data-level="12" data-path="ch12/ch12.html">
<a href="../ch12/ch12.html">
<i class="fa fa-check"></i>
<b>12.</b>
反射
</a>
<ul class="articles">
<li class="chapter " data-level="12.1" data-path="ch12/ch12-01.html">
<a href="../ch12/ch12-01.html">
<i class="fa fa-check"></i>
<b>12.1.</b>
爲何需要反射?
</a>
</li>
<li class="chapter " data-level="12.2" data-path="ch12/ch12-02.html">
<a href="../ch12/ch12-02.html">
<i class="fa fa-check"></i>
<b>12.2.</b>
reflect.Type和reflect.Value
</a>
</li>
<li class="chapter " 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="31-&#x6574;&#x578B;">3.1. &#x6574;&#x578B;</h2>
<p>Go&#x8A9E;&#x8A00;&#x7684;&#x6578;&#x503C;&#x985E;&#x578B;&#x5305;&#x62EC;&#x5E7E;&#x7A2E;&#x4E0D;&#x540C;&#x5927;&#x5C0F;&#x7684;&#x6574;&#x5F62;&#x6578;&#x3001;&#x6D6E;&#x9EDE;&#x6578;&#x548C;&#x8907;&#x6578;&#x3002;&#x6BCF;&#x7A2E;&#x6578;&#x503C;&#x985E;&#x578B;&#x90FD;&#x6C7A;&#x5B9A;&#x4E86;&#x5C0D;&#x61C9;&#x7684;&#x5927;&#x5C0F;&#x8303;&#x570D;&#x548C;&#x662F;&#x5426;&#x652F;&#x6301;&#x6B63;&#x8CA0;&#x7B26;&#x865F;&#x3002;&#x8B93;&#x6211;&#x5011;&#x5148;&#x5F9E;&#x6574;&#x5F62;&#x6578;&#x985E;&#x578B;&#x958B;&#x59CB;&#x4ECB;&#x7D39;&#x3002;</p>
<p>Go&#x8A9E;&#x8A00;&#x540C;&#x6642;&#x63D0;&#x4F9B;&#x4E86;&#x6709;&#x7B26;&#x865F;&#x548C;&#x7121;&#x7B26;&#x865F;&#x985E;&#x578B;&#x7684;&#x6574;&#x6578;&#x904B;&#x7B97;&#x3002;&#x9019;&#x91CC;&#x6709;int8&#x3001;int16&#x3001;int32&#x548C;int64&#x56DB;&#x7A2E;&#x622A;&#x7136;&#x4E0D;&#x540C;&#x5927;&#x5C0F;&#x7684;&#x6709;&#x7B26;&#x865F;&#x6574;&#x5F62;&#x6578;&#x985E;&#x578B;&#xFF0C;&#x5206;&#x522B;&#x5C0D;&#x61C9;8&#x3001;16&#x3001;32&#x3001;64bit&#x5927;&#x5C0F;&#x7684;&#x6709;&#x7B26;&#x865F;&#x6574;&#x5F62;&#x6578;&#xFF0C;&#x8207;&#x6B64;&#x5C0D;&#x61C9;&#x7684;&#x662F;uint8&#x3001;uint16&#x3001;uint32&#x548C;uint64&#x56DB;&#x7A2E;&#x7121;&#x7B26;&#x865F;&#x6574;&#x5F62;&#x6578;&#x985E;&#x578B;&#x3002;</p>
<p>&#x9019;&#x91CC;&#x9084;&#x6709;&#x5169;&#x7A2E;&#x4E00;&#x822C;&#x5C0D;&#x61C9;&#x7279;&#x5B9A;CPU&#x5E73;&#x53F0;&#x6A5F;&#x5668;&#x5B57;&#x5927;&#x5C0F;&#x7684;&#x6709;&#x7B26;&#x865F;&#x548C;&#x7121;&#x7B26;&#x865F;&#x6574;&#x6578;int&#x548C;uint&#xFF1B;&#x5176;&#x4E2D;int&#x662F;&#x61C9;&#x7528;&#x6700;&#x5EE3;&#x6CDB;&#x7684;&#x6578;&#x503C;&#x985E;&#x578B;&#x3002;&#x9019;&#x5169;&#x7A2E;&#x985E;&#x578B;&#x90FD;&#x6709;&#x540C;&#x6A23;&#x7684;&#x5927;&#x5C0F;&#xFF0C;32&#x6216;64bit&#xFF0C;&#x4F46;&#x662F;&#x6211;&#x5011;&#x4E0D;&#x80FD;&#x5C0D;&#x6B64;&#x505A;&#x4EFB;&#x4F55;&#x7684;&#x5047;&#x8A2D;&#xFF1B;&#x56E0;&#x7232;&#x4E0D;&#x540C;&#x7684;&#x7DE8;&#x8B6F;&#x5668;&#x537D;&#x4F7F;&#x5728;&#x76F8;&#x540C;&#x7684;&#x786C;&#x4EF6;&#x5E73;&#x53F0;&#x4E0A;&#x53EF;&#x80FD;&#x7523;&#x751F;&#x4E0D;&#x540C;&#x7684;&#x5927;&#x5C0F;&#x3002;</p>
<p>Unicode&#x5B57;&#x7B26;rune&#x985E;&#x578B;&#x662F;&#x548C;int32&#x7B49;&#x50F9;&#x7684;&#x985E;&#x578B;&#xFF0C;&#x901A;&#x5E38;&#x7528;&#x65BC;&#x8868;&#x793A;&#x4E00;&#x500B;Unicode&#x78BC;&#x9EDE;&#x3002;&#x9019;&#x5169;&#x500B;&#x540D;&#x7A31;&#x53EF;&#x4EE5;&#x4E92;&#x63DB;&#x4F7F;&#x7528;&#x3002;&#x540C;&#x6A23;byte&#x4E5F;&#x662F;uint8&#x985E;&#x578B;&#x7684;&#x7B49;&#x50F9;&#x985E;&#x578B;&#xFF0C;byte&#x985E;&#x578B;&#x4E00;&#x822C;&#x7528;&#x65BC;&#x5F37;&#x8ABF;&#x6578;&#x503C;&#x662F;&#x4E00;&#x500B;&#x539F;&#x59CB;&#x7684;&#x6578;&#x64DA;&#x800C;&#x4E0D;&#x662F;&#x4E00;&#x500B;&#x5C0F;&#x7684;&#x6574;&#x6578;&#x3002;</p>
<p>&#x6700;&#x5F8C;&#xFF0C;&#x9084;&#x6709;&#x4E00;&#x7A2E;&#x7121;&#x7B26;&#x865F;&#x7684;&#x6574;&#x6578;&#x985E;&#x578B;uintptr&#xFF0C;&#x6C92;&#x6709;&#x6307;&#x5B9A;&#x5177;&#x9AD4;&#x7684;bit&#x5927;&#x5C0F;&#x4F46;&#x662F;&#x8DB3;&#x4EE5;&#x5BB9;&#x7D0D;&#x6307;&#x91DD;&#x3002;uintptr&#x985E;&#x578B;&#x96BB;&#x6709;&#x5728;&#x5E95;&#x5C64;&#x7DE8;&#x7A0B;&#x662F;&#x624D;&#x9700;&#x8981;&#xFF0C;&#x7279;&#x522B;&#x662F;Go&#x8A9E;&#x8A00;&#x548C;C&#x8A9E;&#x8A00;&#x51FD;&#x6578;&#x5EAB;&#x6216;&#x64CD;&#x4F5C;&#x7E6B;&#x7D71;&#x63A5;&#x53E3;&#x76F8;&#x4EA4;&#x4E92;&#x7684;&#x5730;&#x65B9;&#x3002;&#x6211;&#x5011;&#x5C07;&#x5728;&#x7B2C;&#x5341;&#x4E09;&#x7AE0;&#x7684;unsafe&#x5305;&#x76F8;&#x95DC;&#x90E8;&#x5206;&#x770B;&#x5230;&#x985E;&#x4F3C;&#x7684;&#x4F8B;&#x5B50;&#x3002;</p>
<p>&#x4E0D;&#x7BA1;&#x5B83;&#x5011;&#x7684;&#x5177;&#x9AD4;&#x5927;&#x5C0F;&#xFF0C;int&#x3001;uint&#x548C;uintptr&#x662F;&#x4E0D;&#x540C;&#x985E;&#x578B;&#x7684;&#x5144;&#x5F1F;&#x985E;&#x578B;&#x3002;&#x5176;&#x4E2D;int&#x548C;int32&#x4E5F;&#x662F;&#x4E0D;&#x540C;&#x7684;&#x985E;&#x578B;&#xFF0C;&#x537D;&#x4F7F;int&#x7684;&#x5927;&#x5C0F;&#x4E5F;&#x662F;32bit&#xFF0C;&#x5728;&#x9700;&#x8981;&#x5C07;int&#x7576;&#x4F5C;int32&#x985E;&#x578B;&#x7684;&#x5730;&#x65B9;&#x9700;&#x8981;&#x4E00;&#x500B;&#x986F;&#x5F0F;&#x7684;&#x985E;&#x578B;&#x8F49;&#x63DB;&#x64CD;&#x4F5C;&#xFF0C;&#x53CD;&#x4E4B;&#x4EA6;&#x7136;&#x3002;</p>
<p>&#x5176;&#x4E2D;&#x6709;&#x7B26;&#x865F;&#x6574;&#x6578;&#x91C7;&#x7528;2&#x7684;&#x88DC;&#x78BC;&#x5F62;&#x5F0F;&#x8868;&#x793A;&#xFF0C;&#x4E5F;&#x5C31;&#x662F;&#x6700;&#x9AD8;bit&#x4F4D;&#x7528;&#x4F5C;&#x8868;&#x793A;&#x7B26;&#x865F;&#x4F4D;&#xFF0C;&#x4E00;&#x500B;n-bit&#x7684;&#x6709;&#x7B26;&#x865F;&#x6578;&#x7684;&#x503C;&#x57DF;&#x662F;&#x5F9E;<span class="katex"><span class="katex-mathml"><math><semantics><mrow><mo>&#x2212;</mo><msup><mn>2</mn><mrow><mi>n</mi><mo>&#x2212;</mo><mn>1</mn></mrow></msup></mrow><annotation encoding="application/x-tex">-2^{n-1}</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="strut" style="height:0.8141079999999999em;"></span><span class="strut bottom" style="height:0.897438em;vertical-align:-0.08333em;"></span><span class="base textstyle uncramped"><span class="mord">&#x2212;</span><span class="mord"><span class="mord mathrm">2</span><span class="vlist"><span style="top:-0.363em;margin-right:0.05em;"><span class="fontsize-ensurer reset-size5 size5"><span style="font-size:0em;">&#x200B;</span></span><span class="reset-textstyle scriptstyle uncramped"><span class="mord scriptstyle uncramped"><span class="mord mathit">n</span><span class="mbin">&#x2212;</span><span class="mord mathrm">1</span></span></span></span><span class="baseline-fix"><span class="fontsize-ensurer reset-size5 size5"><span style="font-size:0em;">&#x200B;</span></span>&#x200B;</span></span></span></span></span></span>&#x5230;<span class="katex"><span class="katex-mathml"><math><semantics><mrow><msup><mn>2</mn><mrow><mi>n</mi><mo>&#x2212;</mo><mn>1</mn></mrow></msup><mo>&#x2212;</mo><mn>1</mn></mrow><annotation encoding="application/x-tex">2^{n-1}-1</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="strut" style="height:0.8141079999999999em;"></span><span class="strut bottom" style="height:0.897438em;vertical-align:-0.08333em;"></span><span class="base textstyle uncramped"><span class="mord"><span class="mord mathrm">2</span><span class="vlist"><span style="top:-0.363em;margin-right:0.05em;"><span class="fontsize-ensurer reset-size5 size5"><span style="font-size:0em;">&#x200B;</span></span><span class="reset-textstyle scriptstyle uncramped"><span class="mord scriptstyle uncramped"><span class="mord mathit">n</span><span class="mbin">&#x2212;</span><span class="mord mathrm">1</span></span></span></span><span class="baseline-fix"><span class="fontsize-ensurer reset-size5 size5"><span style="font-size:0em;">&#x200B;</span></span>&#x200B;</span></span></span><span class="mbin">&#x2212;</span><span class="mord mathrm">1</span></span></span></span>&#x3002;&#x7121;&#x7B26;&#x865F;&#x6574;&#x6578;&#x7684;&#x6240;&#x6709;bit&#x4F4D;&#x90FD;&#x7528;&#x65BC;&#x8868;&#x793A;&#x975E;&#x8CA0;&#x6578;&#xFF0C;&#x503C;&#x57DF;&#x662F;0&#x5230;<span class="katex"><span class="katex-mathml"><math><semantics><mrow><msup><mn>2</mn><mi>n</mi></msup><mo>&#x2212;</mo><mn>1</mn></mrow><annotation encoding="application/x-tex">2^n-1</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="strut" style="height:0.664392em;"></span><span class="strut bottom" style="height:0.747722em;vertical-align:-0.08333em;"></span><span class="base textstyle uncramped"><span class="mord"><span class="mord mathrm">2</span><span class="vlist"><span style="top:-0.363em;margin-right:0.05em;"><span class="fontsize-ensurer reset-size5 size5"><span style="font-size:0em;">&#x200B;</span></span><span class="reset-textstyle scriptstyle uncramped"><span class="mord mathit">n</span></span></span><span class="baseline-fix"><span class="fontsize-ensurer reset-size5 size5"><span style="font-size:0em;">&#x200B;</span></span>&#x200B;</span></span></span><span class="mbin">&#x2212;</span><span class="mord mathrm">1</span></span></span></span>&#x3002;&#x4F8B;&#x5982;&#xFF0C;int8&#x985E;&#x578B;&#x6574;&#x6578;&#x7684;&#x503C;&#x57DF;&#x662F;&#x5F9E;-128&#x5230;127&#xFF0C;&#x800C;uint8&#x985E;&#x578B;&#x6574;&#x6578;&#x7684;&#x503C;&#x57DF;&#x662F;&#x5F9E;0&#x5230;255&#x3002;</p>
<p>&#x4E0B;&#x9762;&#x662F;Go&#x8A9E;&#x8A00;&#x4E2D;&#x95DC;&#x65BC;&#x7B97;&#x8853;&#x904B;&#x7B97;&#x3001;&#x908F;&#x8F2F;&#x904B;&#x7B97;&#x548C;&#x6BD4;&#x8F03;&#x904B;&#x7B97;&#x7684;&#x4E8C;&#x5143;&#x904B;&#x7B97;&#x7B26;&#xFF0C;&#x5B83;&#x5011;&#x6309;&#x7167;&#x5148;&#x7D1A;&#x905E;&#x6E1B;&#x7684;&#x9806;&#x5E8F;&#x7684;&#x6392;&#x5217;&#xFF1A;</p>
<pre><code>* / % &lt;&lt; &gt;&gt; &amp; &amp;^
+ - | ^
== != &lt; &lt;= &gt; &gt;=
&amp;&amp;
||
</code></pre><p>&#x4E8C;&#x5143;&#x904B;&#x7B97;&#x7B26;&#x6709;&#x4E94;&#x7A2E;&#x512A;&#x5148;&#x7D1A;&#x3002;&#x5728;&#x540C;&#x4E00;&#x500B;&#x512A;&#x5148;&#x7D1A;&#xFF0C;&#x4F7F;&#x7528;&#x5DE6;&#x512A;&#x5148;&#x7D50;&#x5408;&#x898F;&#x5247;&#xFF0C;&#x4F46;&#x662F;&#x4F7F;&#x7528;&#x62EC;&#x865F;&#x53EF;&#x4EE5;&#x660E;&#x78BA;&#x512A;&#x5148;&#x9806;&#x5E8F;&#xFF0C;&#x4F7F;&#x7528;&#x62EC;&#x865F;&#x4E5F;&#x53EF;&#x4EE5;&#x7528;&#x65BC;&#x63D0;&#x965E;&#x512A;&#x5148;&#x7D1A;&#xFF0C;&#x4F8B;&#x5982;<code>mask &amp; (1 &lt;&lt; 28)</code>&#x3002;</p>
<p>&#x5C0D;&#x65BC;&#x4E0A;&#x8868;&#x4E2D;&#x524D;&#x5169;&#x884C;&#x7684;&#x904B;&#x7B97;&#x7B26;&#xFF0C;&#x4F8B;&#x5982;+&#x904B;&#x7B97;&#x7B26;&#x9084;&#x6709;&#x4E00;&#x500B;&#x8207;&#x8CE6;&#x503C;&#x76F8;&#x7D50;&#x5408;&#x7684;&#x5C0D;&#x61C9;&#x904B;&#x7B97;&#x7B26;+=&#xFF0C;&#x53EF;&#x4EE5;&#x7528;&#x65BC;&#x7C21;&#x5316;&#x8CE6;&#x503C;&#x8A9E;&#x53E5;&#x3002;</p>
<p>&#x6574;&#x6578;&#x7684;&#x7B97;&#x8853;&#x904B;&#x7B97;&#x7B26;+&#x3001;-&#x3001;<code>*</code>&#x548C;<code>/</code>&#x53EF;&#x4EE5;&#x9069;&#x7528;&#x8207;&#x65BC;&#x6574;&#x6578;&#x3001;&#x6D6E;&#x9EDE;&#x6578;&#x548C;&#x8907;&#x6578;&#xFF0C;&#x4F46;&#x662F;&#x53D6;&#x6A21;&#x904B;&#x7B97;&#x7B26;%&#x50C5;&#x7528;&#x65BC;&#x6574;&#x6578;&#x9593;&#x7684;&#x904B;&#x7B97;&#x3002;&#x5C0D;&#x65BC;&#x4E0D;&#x540C;&#x7DE8;&#x7A0B;&#x8A9E;&#x8A00;&#xFF0C;%&#x53D6;&#x6A21;&#x904B;&#x7B97;&#x7684;&#x884C;&#x7232;&#x53EF;&#x80FD;&#x4F75;&#x4E0D;&#x76F8;&#x540C;&#x3002;&#x5728;Go&#x8A9E;&#x8A00;&#x4E2D;&#xFF0C;%&#x53D6;&#x6A21;&#x904B;&#x7B97;&#x7B26;&#x7684;&#x7B26;&#x865F;&#x548C;&#x88AB;&#x53D6;&#x6A21;&#x6578;&#x7684;&#x7B26;&#x865F;&#x7E3D;&#x662F;&#x4E00;&#x81F4;&#x7684;&#xFF0C;&#x56E0;&#x6B64;<code>-5%3</code>&#x548C;<code>-5%-3</code>&#x7D50;&#x679C;&#x90FD;&#x662F;-2&#x3002;&#x9664;&#x6CD5;&#x904B;&#x7B97;&#x7B26;<code>/</code>&#x7684;&#x884C;&#x7232;&#x5247;&#x4F9D;&#x8CF4;&#x65BC;&#x64CD;&#x4F5C;&#x6578;&#x662F;&#x5426;&#x7232;&#x5168;&#x7232;&#x6574;&#x6578;&#xFF0C;&#x6BD4;&#x5982;<code>5.0/4.0</code>&#x7684;&#x7D50;&#x679C;&#x662F;1.25&#xFF0C;&#x4F46;&#x662F;5/4&#x7684;&#x7D50;&#x679C;&#x662F;1&#xFF0C;&#x56E0;&#x7232;&#x6574;&#x6578;&#x9664;&#x6CD5;&#x6703;&#x5411;&#x7740;0&#x65B9;&#x5411;&#x622A;&#x65B7;&#x9918;&#x6578;&#x3002;</p>
<p>&#x5982;&#x679C;&#x4E00;&#x500B;&#x7B97;&#x8853;&#x904B;&#x7B97;&#x7684;&#x7D50;&#x679C;&#xFF0C;&#x4E0D;&#x7BA1;&#x662F;&#x6709;&#x7B26;&#x865F;&#x6216;&#x8005;&#x662F;&#x7121;&#x7B26;&#x865F;&#x7684;&#xFF0C;&#x5982;&#x679C;&#x9700;&#x8981;&#x66F4;&#x591A;&#x7684;bit&#x4F4D;&#x624D;&#x80FD;&#x6B63;&#x78BA;&#x8868;&#x793A;&#x7684;&#x8A71;&#xFF0C;&#x5C31;&#x8AAC;&#x660E;&#x8A08;&#x7B97;&#x7D50;&#x679C;&#x662F;&#x6EA2;&#x51FA;&#x4E86;&#x3002;&#x8D85;&#x51FA;&#x7684;&#x9AD8;&#x4F4D;&#x7684;bit&#x4F4D;&#x90E8;&#x5206;&#x5C07;&#x88AB;&#x4E1F;&#x68C4;&#x3002;&#x5982;&#x679C;&#x539F;&#x59CB;&#x7684;&#x6578;&#x503C;&#x662F;&#x6709;&#x7B26;&#x865F;&#x985E;&#x578B;&#xFF0C;&#x800C;&#x4E14;&#x6700;&#x5DE6;&#x908A;&#x7684;bit&#x7232;&#x662F;1&#x7684;&#x8A71;&#xFF0C;&#x90A3;&#x9EBD;&#x6700;&#x7D42;&#x7D50;&#x679C;&#x53EF;&#x80FD;&#x662F;&#x8CA0;&#x7684;&#xFF0C;&#x4F8B;&#x5982;int8&#x7684;&#x4F8B;&#x5B50;&#xFF1A;</p>
<pre><code class="lang-Go"><span class="hljs-keyword">var</span> u <span class="hljs-typename">uint8</span> = <span class="hljs-number">255</span>
fmt.Println(u, u+<span class="hljs-number">1</span>, u*u) <span class="hljs-comment">// &quot;255 0 1&quot;</span>
<span class="hljs-keyword">var</span> i <span class="hljs-typename">int8</span> = <span class="hljs-number">127</span>
fmt.Println(i, i+<span class="hljs-number">1</span>, i*i) <span class="hljs-comment">// &quot;127 -128 1&quot;</span>
</code></pre>
<p>&#x5169;&#x500B;&#x76F8;&#x540C;&#x7684;&#x6574;&#x6578;&#x985E;&#x578B;&#x53EF;&#x4EE5;&#x4F7F;&#x7528;&#x4E0B;&#x9762;&#x7684;&#x4E8C;&#x5143;&#x6BD4;&#x8F03;&#x904B;&#x7B97;&#x7B26;&#x9032;&#x884C;&#x6BD4;&#x8F03;&#xFF1B;&#x6BD4;&#x8F03;&#x8868;&#x9054;&#x5F0F;&#x7684;&#x7D50;&#x679C;&#x662F;&#x5E03;&#x723E;&#x985E;&#x578B;&#x3002;</p>
<pre><code>== equal to
!= not equal to
&lt; less than
&lt;= less than or equal to
&gt; greater than
&gt;= greater than or equal to
</code></pre><p>&#x4E8B;&#x5BE6;&#x4E0A;&#xFF0C;&#x5E03;&#x723E;&#x578B;&#x3001;&#x6578;&#x5B57;&#x985E;&#x578B;&#x548C;&#x5B57;&#x7B26;&#x4E32;&#x7B49;&#x57FA;&#x672C;&#x985E;&#x578B;&#x90FD;&#x662F;&#x53EF;&#x6BD4;&#x8F03;&#x7684;&#xFF0C;&#x4E5F;&#x5C31;&#x662F;&#x8AAC;&#x5169;&#x500B;&#x76F8;&#x540C;&#x985E;&#x578B;&#x7684;&#x503C;&#x53EF;&#x4EE5;&#x7528;==&#x548C;!=&#x9032;&#x884C;&#x6BD4;&#x8F03;&#x3002;&#x6B64;&#x5916;&#xFF0C;&#x6574;&#x6578;&#x3001;&#x6D6E;&#x9EDE;&#x6578;&#x548C;&#x5B57;&#x7B26;&#x4E32;&#x53EF;&#x4EE5;&#x6839;&#x64DA;&#x6BD4;&#x8F03;&#x7D50;&#x679C;&#x6392;&#x5E8F;&#x3002;&#x8A31;&#x591A;&#x5176;&#x5B83;&#x985E;&#x578B;&#x7684;&#x503C;&#x53EF;&#x80FD;&#x662F;&#x4E0D;&#x53EF;&#x6BD4;&#x8F03;&#x7684;&#xFF0C;&#x56E0;&#x6B64;&#x4E5F;&#x5C31;&#x53EF;&#x80FD;&#x662F;&#x4E0D;&#x53EF;&#x6392;&#x5E8F;&#x7684;&#x3002;&#x5C0D;&#x65BC;&#x6211;&#x5011;&#x9047;&#x5230;&#x7684;&#x6BCF;&#x7A2E;&#x985E;&#x578B;&#xFF0C;&#x6211;&#x5011;&#x9700;&#x8981;&#x4FDD;&#x8B49;&#x898F;&#x5247;&#x7684;&#x4E00;&#x81F4;&#x6027;&#x3002;</p>
<p>&#x9019;&#x91CC;&#x662F;&#x4E00;&#x5143;&#x7684;&#x52A0;&#x6CD5;&#x548C;&#x6E1B;&#x6CD5;&#x904B;&#x7B97;&#x7B26;&#xFF1A;</p>
<pre><code>+ &#x4E00;&#x5143;&#x52A0;&#x6CD5; (&#x7121;&#x6548;&#x679C;)
- &#x8CA0;&#x6578;
</code></pre><p>&#x5C0D;&#x65BC;&#x6574;&#x6578;&#xFF0C;+x&#x662F;0+x&#x7684;&#x7C21;&#x5BEB;&#xFF0C;-x&#x5247;&#x662F;0-x&#x7684;&#x7C21;&#x5BEB;&#xFF1B;&#x5C0D;&#x65BC;&#x6D6E;&#x9EDE;&#x6578;&#x548C;&#x8907;&#x6578;&#xFF0C;+x&#x5C31;&#x662F;x&#xFF0C;-x&#x5247;&#x662F;x &#x7684;&#x8CA0;&#x6578;&#x3002;</p>
<p>Go&#x8A9E;&#x8A00;&#x9084;&#x63D0;&#x4F9B;&#x4E86;&#x4EE5;&#x4E0B;&#x7684;bit&#x4F4D;&#x64CD;&#x4F5C;&#x904B;&#x7B97;&#x7B26;&#xFF0C;&#x524D;&#x9762;4&#x500B;&#x64CD;&#x4F5C;&#x904B;&#x7B97;&#x7B26;&#x4F75;&#x4E0D;&#x5340;&#x5206;&#x662F;&#x6709;&#x7B26;&#x865F;&#x9084;&#x662F;&#x7121;&#x7B26;&#x865F;&#x6578;&#xFF1A;</p>
<pre><code>&amp; &#x4F4D;&#x904B;&#x7B97; AND
| &#x4F4D;&#x904B;&#x7B97; OR
^ &#x4F4D;&#x904B;&#x7B97; XOR
&amp;^ &#x4F4D;&#x6E05;&#x7A7A; (AND NOT)
&lt;&lt; &#x5DE6;&#x79FB;
&gt;&gt; &#x53F3;&#x79FB;
</code></pre><p>&#x4F4D;&#x64CD;&#x4F5C;&#x904B;&#x7B97;&#x7B26;<code>^</code>&#x4F5C;&#x7232;&#x4E8C;&#x5143;&#x904B;&#x7B97;&#x7B26;&#x6642;&#x662F;&#x6309;&#x4F4D;&#x7570;&#x6216;&#xFF08;XOR&#xFF09;&#xFF0C;&#x7576;&#x7528;&#x4F5C;&#x4E00;&#x5143;&#x904B;&#x7B97;&#x7B26;&#x6642;&#x8868;&#x793A;&#x6309;&#x4F4D;&#x53D6;&#x53CD;&#xFF1B;&#x4E5F;&#x5C31;&#x662F;&#x8AAC;&#xFF0C;&#x5B83;&#x8FD4;&#x8FF4;&#x4E00;&#x500B;&#x6BCF;&#x500B;bit&#x4F4D;&#x90FD;&#x53D6;&#x53CD;&#x7684;&#x6578;&#x3002;&#x4F4D;&#x64CD;&#x4F5C;&#x904B;&#x7B97;&#x7B26;<code>&amp;^</code>&#x7528;&#x65BC;&#x6309;&#x4F4D;&#x7F6E;&#x96F6;&#xFF08;AND NOT&#xFF09;&#xFF1A;&#x8868;&#x9054;&#x5F0F;<code>z = x &amp;^ y</code>&#x7D50;&#x679C;z&#x7684;bit&#x4F4D;&#x7232;0&#xFF0C;&#x5982;&#x679C;&#x5C0D;&#x61C9;y&#x4E2D;bit&#x4F4D;&#x7232;1&#x7684;&#x8A71;&#xFF0C;&#x5426;&#x5247;&#x5C0D;&#x61C9;&#x7684;bit&#x4F4D;&#x7B49;&#x65BC;x&#x76F8;&#x61C9;&#x7684;bit&#x4F4D;&#x7684;&#x503C;&#x3002;</p>
<p>&#x4E0B;&#x9762;&#x7684;&#x4EE3;&#x78BC;&#x6F14;&#x793A;&#x4E86;&#x5982;&#x4F55;&#x4F7F;&#x7528;&#x4F4D;&#x64CD;&#x4F5C;&#x89E3;&#x91CB;uint8&#x985E;&#x578B;&#x503C;&#x7684;8&#x500B;&#x7368;&#x7ACB;&#x7684;bit&#x4F4D;&#x3002;&#x5B83;&#x4F7F;&#x7528;&#x4E86;Printf&#x51FD;&#x6578;&#x7684;%b&#x53C3;&#x6578;&#x6253;&#x5370;&#x4E8C;&#x9032;&#x88FD;&#x683C;&#x5F0F;&#x7684;&#x6578;&#x5B57;&#xFF1B;&#x5176;&#x4E2D;%08b&#x4E2D;08&#x8868;&#x793A;&#x6253;&#x5370;&#x81F3;&#x5C11;8&#x500B;&#x5B57;&#x7B26;&#x5BEC;&#x5EA6;&#xFF0C;&#x4E0D;&#x8DB3;&#x7684;&#x524D;&#x7DB4;&#x90E8;&#x5206;&#x7528;0&#x586B;&#x5145;&#x3002;</p>
<pre><code class="lang-Go"><span class="hljs-keyword">var</span> x <span class="hljs-typename">uint8</span> = <span class="hljs-number">1</span>&lt;&lt;<span class="hljs-number">1</span> | <span class="hljs-number">1</span>&lt;&lt;<span class="hljs-number">5</span>
<span class="hljs-keyword">var</span> y <span class="hljs-typename">uint8</span> = <span class="hljs-number">1</span>&lt;&lt;<span class="hljs-number">1</span> | <span class="hljs-number">1</span>&lt;&lt;<span class="hljs-number">2</span>
fmt.Printf(<span class="hljs-string">&quot;%08b\n&quot;</span>, x) <span class="hljs-comment">// &quot;00100010&quot;, the set {1, 5}</span>
fmt.Printf(<span class="hljs-string">&quot;%08b\n&quot;</span>, y) <span class="hljs-comment">// &quot;00000110&quot;, the set {1, 2}</span>
fmt.Printf(<span class="hljs-string">&quot;%08b\n&quot;</span>, x&amp;y) <span class="hljs-comment">// &quot;00000010&quot;, the intersection {1}</span>
fmt.Printf(<span class="hljs-string">&quot;%08b\n&quot;</span>, x|y) <span class="hljs-comment">// &quot;00100110&quot;, the union {1, 2, 5}</span>
fmt.Printf(<span class="hljs-string">&quot;%08b\n&quot;</span>, x^y) <span class="hljs-comment">// &quot;00100100&quot;, the symmetric difference {2, 5}</span>
fmt.Printf(<span class="hljs-string">&quot;%08b\n&quot;</span>, x&amp;^y) <span class="hljs-comment">// &quot;00100000&quot;, the difference {5}</span>
<span class="hljs-keyword">for</span> i := <span class="hljs-typename">uint</span>(<span class="hljs-number">0</span>); i &lt; <span class="hljs-number">8</span>; i++ {
<span class="hljs-keyword">if</span> x&amp;(<span class="hljs-number">1</span>&lt;&lt;i) != <span class="hljs-number">0</span> { <span class="hljs-comment">// membership test</span>
fmt.Println(i) <span class="hljs-comment">// &quot;1&quot;, &quot;5&quot;</span>
}
}
fmt.Printf(<span class="hljs-string">&quot;%08b\n&quot;</span>, x&lt;&lt;<span class="hljs-number">1</span>) <span class="hljs-comment">// &quot;01000100&quot;, the set {2, 6}</span>
fmt.Printf(<span class="hljs-string">&quot;%08b\n&quot;</span>, x&gt;&gt;<span class="hljs-number">1</span>) <span class="hljs-comment">// &quot;00010001&quot;, the set {0, 4}</span>
</code></pre>
<p>&#xFF08;6.5&#x7BC0;&#x7D66;&#x51FA;&#x4E86;&#x4E00;&#x500B;&#x53EF;&#x4EE5;&#x9060;&#x5927;&#x65BC;&#x4E00;&#x500B;&#x5B57;&#x7BC0;&#x7684;&#x6574;&#x6578;&#x96C6;&#x7684;&#x5BE6;&#x73FE;&#x3002;&#xFF09;</p>
<p>&#x5728;<code>x&lt;&lt;n</code>&#x548C;<code>x&gt;&gt;n</code>&#x79FB;&#x4F4D;&#x904B;&#x7B97;&#x4E2D;&#xFF0C;&#x6C7A;&#x5B9A;&#x4E86;&#x79FB;&#x4F4D;&#x64CD;&#x4F5C;bit&#x6578;&#x90E8;&#x5206;&#x5FC5;&#x9B1A;&#x662F;&#x7121;&#x7B26;&#x865F;&#x6578;&#xFF1B;&#x88AB;&#x64CD;&#x4F5C;&#x7684;x&#x6578;&#x53EF;&#x4EE5;&#x662F;&#x6709;&#x7B26;&#x865F;&#x6216;&#x7121;&#x7B26;&#x865F;&#x6578;&#x3002;&#x7B97;&#x8853;&#x4E0A;&#xFF0C;&#x4E00;&#x500B;<code>x&lt;&lt;n</code>&#x5DE6;&#x79FB;&#x904B;&#x7B97;&#x7B49;&#x50F9;&#x65BC;&#x4E58;&#x4EE5;<span class="katex"><span class="katex-mathml"><math><semantics><mrow><msup><mn>2</mn><mi>n</mi></msup></mrow><annotation encoding="application/x-tex">2^n</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="strut" style="height:0.664392em;"></span><span class="strut bottom" style="height:0.664392em;vertical-align:0em;"></span><span class="base textstyle uncramped"><span class="mord"><span class="mord mathrm">2</span><span class="vlist"><span style="top:-0.363em;margin-right:0.05em;"><span class="fontsize-ensurer reset-size5 size5"><span style="font-size:0em;">&#x200B;</span></span><span class="reset-textstyle scriptstyle uncramped"><span class="mord mathit">n</span></span></span><span class="baseline-fix"><span class="fontsize-ensurer reset-size5 size5"><span style="font-size:0em;">&#x200B;</span></span>&#x200B;</span></span></span></span></span></span>&#xFF0C;&#x4E00;&#x500B;<code>x&gt;&gt;n</code>&#x53F3;&#x79FB;&#x904B;&#x7B97;&#x7B49;&#x50F9;&#x65BC;&#x9664;&#x4EE5;<span class="katex"><span class="katex-mathml"><math><semantics><mrow><msup><mn>2</mn><mi>n</mi></msup></mrow><annotation encoding="application/x-tex">2^n</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="strut" style="height:0.664392em;"></span><span class="strut bottom" style="height:0.664392em;vertical-align:0em;"></span><span class="base textstyle uncramped"><span class="mord"><span class="mord mathrm">2</span><span class="vlist"><span style="top:-0.363em;margin-right:0.05em;"><span class="fontsize-ensurer reset-size5 size5"><span style="font-size:0em;">&#x200B;</span></span><span class="reset-textstyle scriptstyle uncramped"><span class="mord mathit">n</span></span></span><span class="baseline-fix"><span class="fontsize-ensurer reset-size5 size5"><span style="font-size:0em;">&#x200B;</span></span>&#x200B;</span></span></span></span></span></span>&#x3002;</p>
<p>&#x5DE6;&#x79FB;&#x904B;&#x7B97;&#x7528;&#x96F6;&#x586B;&#x5145;&#x53F3;&#x908A;&#x7A7A;&#x7F3A;&#x7684;bit&#x4F4D;&#xFF0C;&#x7121;&#x7B26;&#x865F;&#x6578;&#x7684;&#x53F3;&#x79FB;&#x904B;&#x7B97;&#x4E5F;&#x662F;&#x7528;0&#x586B;&#x5145;&#x5DE6;&#x908A;&#x7A7A;&#x7F3A;&#x7684;bit&#x4F4D;&#xFF0C;&#x4F46;&#x662F;&#x6709;&#x7B26;&#x865F;&#x6578;&#x7684;&#x53F3;&#x79FB;&#x904B;&#x7B97;&#x6703;&#x7528;&#x7B26;&#x865F;&#x4F4D;&#x7684;&#x503C;&#x586B;&#x5145;&#x5DE6;&#x908A;&#x7A7A;&#x7F3A;&#x7684;bit&#x4F4D;&#x3002;&#x56E0;&#x7232;&#x9019;&#x500B;&#x539F;&#x56E0;&#xFF0C;&#x6700;&#x597D;&#x7528;&#x7121;&#x7B26;&#x865F;&#x904B;&#x7B97;&#xFF0C;&#x9019;&#x6A23;&#x4F60;&#x53EF;&#x4EE5;&#x5C07;&#x6574;&#x6578;&#x5B8C;&#x5168;&#x7576;&#x4F5C;&#x4E00;&#x500B;bit&#x4F4D;&#x6A21;&#x5F0F;&#x8655;&#x7406;&#x3002;</p>
<p>&#x76E1;&#x7BA1;Go&#x8A9E;&#x8A00;&#x63D0;&#x4F9B;&#x4E86;&#x7121;&#x7B26;&#x865F;&#x6578;&#x548C;&#x904B;&#x7B97;&#xFF0C;&#x537D;&#x4F7F;&#x6578;&#x503C;&#x672C;&#x8EAB;&#x4E0D;&#x53EF;&#x80FD;&#x51FA;&#x73FE;&#x8CA0;&#x6578;&#x6211;&#x5011;&#x9084;&#x662F;&#x50BE;&#x5411;&#x65BC;&#x4F7F;&#x7528;&#x6709;&#x7B26;&#x865F;&#x7684;int&#x985E;&#x578B;&#xFF0C;&#x5C31;&#x50CF;&#x6578;&#x7D44;&#x7684;&#x9577;&#x5EA6;&#x90A3;&#x6A23;&#xFF0C;&#x96D6;&#x7136;&#x4F7F;&#x7528;uint&#x7121;&#x7B26;&#x865F;&#x985E;&#x578B;&#x4F3C;&#x4E4E;&#x662F;&#x4E00;&#x500B;&#x66F4;&#x5408;&#x7406;&#x7684;&#x9078;&#x64C7;&#x3002;&#x4E8B;&#x5BE6;&#x4E0A;&#xFF0C;&#x5167;&#x7F6E;&#x7684;len&#x51FD;&#x6578;&#x8FD4;&#x8FF4;&#x4E00;&#x500B;&#x6709;&#x7B26;&#x865F;&#x7684;int&#xFF0C;&#x6211;&#x5011;&#x53EF;&#x4EE5;&#x50CF;&#x4E0B;&#x9762;&#x4F8B;&#x5B50;&#x90A3;&#x6A23;&#x8655;&#x7406;&#x9006;&#x5E8F;&#x5FAA;&#x74B0;&#x3002;</p>
<pre><code class="lang-Go">medals := []<span class="hljs-typename">string</span>{<span class="hljs-string">&quot;gold&quot;</span>, <span class="hljs-string">&quot;silver&quot;</span>, <span class="hljs-string">&quot;bronze&quot;</span>}
<span class="hljs-keyword">for</span> i := <span class="hljs-built_in">len</span>(medals) - <span class="hljs-number">1</span>; i &gt;= <span class="hljs-number">0</span>; i-- {
fmt.Println(medals[i]) <span class="hljs-comment">// &quot;bronze&quot;, &quot;silver&quot;, &quot;gold&quot;</span>
}
</code></pre>
<p>&#x53E6;&#x4E00;&#x500B;&#x9078;&#x64C7;&#x5C0D;&#x65BC;&#x4E0A;&#x9762;&#x7684;&#x4F8B;&#x5B50;&#x4F86;&#x8AAC;&#x5C07;&#x662F;&#x707D;&#x96E3;&#x6027;&#x7684;&#x3002;&#x5982;&#x679C;len&#x51FD;&#x6578;&#x8FD4;&#x8FF4;&#x4E00;&#x500B;&#x7121;&#x7B26;&#x865F;&#x6578;&#xFF0C;&#x90A3;&#x9EBD;i&#x4E5F;&#x5C07;&#x662F;&#x7121;&#x7B26;&#x865F;&#x7684;uint&#x985E;&#x578B;&#xFF0C;&#x7136;&#x5F8C;&#x689D;&#x4EF6;<code>i &gt;= 0</code>&#x5247;&#x6C38;&#x9060;&#x7232;&#x771E;&#x3002;&#x5728;&#x4E09;&#x6B21;&#x8FED;&#x4EE3;&#x4E4B;&#x5F8C;&#xFF0C;&#x4E5F;&#x5C31;&#x662F;<code>i == 0</code>&#x6642;&#xFF0C;i--&#x8A9E;&#x53E5;&#x5C07;&#x4E0D;&#x6703;&#x7523;&#x751F;-1&#xFF0C;&#x800C;&#x662F;&#x8B8A;&#x6210;&#x4E00;&#x500B;uint&#x985E;&#x578B;&#x7684;&#x6700;&#x5927;&#x503C;&#xFF08;&#x53EF;&#x80FD;&#x662F;<span class="katex"><span class="katex-mathml"><math><semantics><mrow><msup><mn>2</mn><mn>6</mn></msup><mn>4</mn><mo>&#x2212;</mo><mn>1</mn></mrow><annotation encoding="application/x-tex">2^64-1</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="strut" style="height:0.8141079999999999em;"></span><span class="strut bottom" style="height:0.897438em;vertical-align:-0.08333em;"></span><span class="base textstyle uncramped"><span class="mord"><span class="mord mathrm">2</span><span class="vlist"><span style="top:-0.363em;margin-right:0.05em;"><span class="fontsize-ensurer reset-size5 size5"><span style="font-size:0em;">&#x200B;</span></span><span class="reset-textstyle scriptstyle uncramped"><span class="mord mathrm">6</span></span></span><span class="baseline-fix"><span class="fontsize-ensurer reset-size5 size5"><span style="font-size:0em;">&#x200B;</span></span>&#x200B;</span></span></span><span class="mord mathrm">4</span><span class="mbin">&#x2212;</span><span class="mord mathrm">1</span></span></span></span>&#xFF09;&#xFF0C;&#x7136;&#x5F8C;medals[i]&#x8868;&#x9054;&#x5F0F;&#x5C07;&#x767C;&#x751F;&#x904B;&#x884C;&#x6642;panic&#x7570;&#x5E38;&#xFF08;&#xA7;5.9&#xFF09;&#xFF0C;&#x4E5F;&#x5C31;&#x662F;&#x8A66;&#x5716;&#x8A2A;&#x554F;&#x4E00;&#x500B;slice&#x8303;&#x570D;&#x4EE5;&#x5916;&#x7684;&#x5143;&#x7D20;&#x3002;</p>
<p>&#x51FA;&#x65BC;&#x9019;&#x500B;&#x539F;&#x56E0;&#xFF0C;&#x7121;&#x7B26;&#x865F;&#x6578;&#x5F80;&#x5F80;&#x96BB;&#x6709;&#x5728;&#x4F4D;&#x904B;&#x7B97;&#x6216;&#x5176;&#x5B83;&#x7279;&#x6B8A;&#x7684;&#x904B;&#x7B97;&#x5834;&#x666F;&#x624D;&#x6703;&#x4F7F;&#x7528;&#xFF0C;&#x5C31;&#x50CF;bit&#x96C6;&#x5408;&#x3001;&#x5206;&#x6790;&#x4E8C;&#x9032;&#x88FD;&#x6587;&#x4EF6;&#x683C;&#x5F0F;&#x6216;&#x8005;&#x662F;&#x54C8;&#x5E0C;&#x548C;&#x52A0;&#x5BC6;&#x64CD;&#x4F5C;&#x7B49;&#x3002;&#x5B83;&#x5011;&#x901A;&#x5E38;&#x4F75;&#x4E0D;&#x7528;&#x65BC;&#x50C5;&#x50C5;&#x662F;&#x8868;&#x9054;&#x975E;&#x8CA0;&#x6578;&#x91CF;&#x7684;&#x5834;&#x5408;&#x3002;</p>
<p>&#x4E00;&#x822C;&#x4F86;&#x8AAC;&#xFF0C;&#x9700;&#x8981;&#x4E00;&#x500B;&#x986F;&#x5F0F;&#x7684;&#x8F49;&#x63DB;&#x5C07;&#x4E00;&#x500B;&#x503C;&#x5F9E;&#x4E00;&#x7A2E;&#x985E;&#x578B;&#x8F49;&#x5316;&#x4F4D;&#x53E6;&#x4E00;&#x7A2E;&#x985E;&#x578B;&#xFF0C;&#x4F75;&#x4E14;&#x7B97;&#x8853;&#x548C;&#x908F;&#x8F2F;&#x904B;&#x7B97;&#x7684;&#x4E8C;&#x5143;&#x64CD;&#x4F5C;&#x4E2D;&#x5FC5;&#x9B1A;&#x662F;&#x76F8;&#x540C;&#x7684;&#x985E;&#x578B;&#x3002;&#x96D6;&#x7136;&#x9019;&#x5076;&#x723E;&#x6703;&#x5C0E;&#x81F4;&#x9700;&#x8981;&#x5F88;&#x9577;&#x7684;&#x8868;&#x9054;&#x5F0F;&#xFF0C;&#x4F46;&#x662F;&#x5B83;&#x6D88;&#x9664;&#x4E86;&#x6240;&#x6709;&#x548C;&#x985E;&#x578B;&#x76F8;&#x95DC;&#x7684;&#x554F;&#x984C;&#xFF0C;&#x800C;&#x4E14;&#x4E5F;&#x4F7F;&#x5F97;&#x7A0B;&#x5E8F;&#x5BB9;&#x6613;&#x7406;&#x89E3;&#x3002;</p>
<p>&#x5728;&#x5F88;&#x591A;&#x5834;&#x666F;&#xFF0C;&#x6703;&#x9047;&#x5230;&#x985E;&#x4F3C;&#x4E0B;&#x9762;&#x7684;&#x4EE3;&#x78BC;&#x901A;&#x7528;&#x7684;&#x932F;&#x8AA4;&#xFF1A;</p>
<pre><code class="lang-Go"><span class="hljs-keyword">var</span> apples <span class="hljs-typename">int32</span> = <span class="hljs-number">1</span>
<span class="hljs-keyword">var</span> oranges <span class="hljs-typename">int16</span> = <span class="hljs-number">2</span>
<span class="hljs-keyword">var</span> compote <span class="hljs-typename">int</span> = apples + oranges <span class="hljs-comment">// compile error</span>
</code></pre>
<p>&#x7576;&#x5690;&#x8A66;&#x7DE8;&#x8B6F;&#x9019;&#x4E09;&#x500B;&#x8A9E;&#x53E5;&#x6642;&#xFF0C;&#x5C07;&#x7523;&#x751F;&#x4E00;&#x500B;&#x932F;&#x8AA4;&#x4FE1;&#x606F;&#xFF1A;</p>
<pre><code>invalid operation: apples + oranges (mismatched types int32 and int16)
</code></pre><p>&#x9019;&#x7A2E;&#x985E;&#x578B;&#x4E0D;&#x5339;&#x914D;&#x7684;&#x554F;&#x984C;&#x53EF;&#x4EE5;&#x6709;&#x5E7E;&#x7A2E;&#x4E0D;&#x540C;&#x7684;&#x65B9;&#x6CD5;&#x8129;&#x8907;&#xFF0C;&#x6700;&#x5E38;&#x898B;&#x65B9;&#x6CD5;&#x662F;&#x5C07;&#x5B83;&#x5011;&#x90FD;&#x986F;&#x5F0F;&#x8F49;&#x578B;&#x7232;&#x4E00;&#x500B;&#x5E38;&#x898B;&#x985E;&#x578B;&#xFF1A;</p>
<pre><code class="lang-Go"><span class="hljs-keyword">var</span> compote = <span class="hljs-typename">int</span>(apples) + <span class="hljs-typename">int</span>(oranges)
</code></pre>
<p>&#x5982;2.5&#x7BC0;&#x6240;&#x8FF0;&#xFF0C;&#x5C0D;&#x65BC;&#x6BCF;&#x7A2E;&#x985E;&#x578B;T&#xFF0C;&#x5982;&#x679C;&#x8F49;&#x63DB;&#x5141;&#x8A31;&#x7684;&#x8A71;&#xFF0C;&#x985E;&#x578B;&#x8F49;&#x63DB;&#x64CD;&#x4F5C;T(x)&#x5C07;x&#x8F49;&#x63DB;&#x7232;T&#x985E;&#x578B;&#x3002;&#x8A31;&#x591A;&#x6574;&#x5F62;&#x6578;&#x4E4B;&#x9593;&#x7684;&#x76F8;&#x4E92;&#x8F49;&#x63DB;&#x4F75;&#x4E0D;&#x6703;&#x6539;&#x8B8A;&#x6578;&#x503C;&#xFF1B;&#x5B83;&#x5011;&#x96BB;&#x662F;&#x544A;&#x8A34;&#x7DE8;&#x8B6F;&#x5668;&#x5982;&#x4F55;&#x89E3;&#x91CB;&#x9019;&#x500B;&#x503C;&#x3002;&#x4F46;&#x662F;&#x5C0D;&#x65BC;&#x5C07;&#x4E00;&#x500B;&#x5927;&#x5C3A;&#x5BF8;&#x7684;&#x6574;&#x6578;&#x985E;&#x578B;&#x8F49;&#x7232;&#x4E00;&#x500B;&#x5C0F;&#x5C3A;&#x5BF8;&#x7684;&#x6574;&#x6578;&#x985E;&#x578B;&#xFF0C;&#x6216;&#x8005;&#x662F;&#x5C07;&#x4E00;&#x500B;&#x6D6E;&#x9EDE;&#x6578;&#x8F49;&#x7232;&#x6574;&#x6578;&#xFF0C;&#x53EF;&#x80FD;&#x6703;&#x6539;&#x8B8A;&#x6578;&#x503C;&#x6216;&#x4E1F;&#x5931;&#x7CBE;&#x5EA6;&#xFF1A;</p>
<pre><code class="lang-Go">f := <span class="hljs-number">3.141</span> <span class="hljs-comment">// a float64</span>
i := <span class="hljs-typename">int</span>(f)
fmt.Println(f, i) <span class="hljs-comment">// &quot;3.141 3&quot;</span>
f = <span class="hljs-number">1.99</span>
fmt.Println(<span class="hljs-typename">int</span>(f)) <span class="hljs-comment">// &quot;1&quot;</span>
</code></pre>
<p>&#x6D6E;&#x9EDE;&#x6578;&#x5230;&#x6574;&#x6578;&#x7684;&#x8F49;&#x63DB;&#x5C07;&#x4E1F;&#x5931;&#x4EFB;&#x4F55;&#x5C0F;&#x6578;&#x90E8;&#x5206;&#xFF0C;&#x7136;&#x5F8C;&#x5411;&#x6578;&#x8EF8;&#x96F6;&#x65B9;&#x5411;&#x622A;&#x65B7;&#x3002;&#x4F60;&#x61C9;&#x8A72;&#x907F;&#x514D;&#x5C0D;&#x53EF;&#x80FD;&#x6703;&#x8D85;&#x51FA;&#x76EE;&#x6A19;&#x985E;&#x578B;&#x8868;&#x793A;&#x8303;&#x570D;&#x7684;&#x6578;&#x503C;&#x985E;&#x578B;&#x8F49;&#x63DB;&#xFF0C;&#x56E0;&#x7232;&#x622A;&#x65B7;&#x7684;&#x884C;&#x7232;&#x53EF;&#x80FD;&#x4F9D;&#x8CF4;&#x65BC;&#x5177;&#x9AD4;&#x7684;&#x5BE6;&#x73FE;&#xFF1A;</p>
<pre><code class="lang-Go">f := <span class="hljs-number">1e100</span> <span class="hljs-comment">// a float64</span>
i := <span class="hljs-typename">int</span>(f) <span class="hljs-comment">// &#x7D50;&#x679C;&#x4F9D;&#x8CF4;&#x65BC;&#x5177;&#x9AD4;&#x5BE6;&#x73FE;</span>
</code></pre>
<p>&#x4EFB;&#x4F55;&#x5927;&#x5C0F;&#x7684;&#x6574;&#x6578;&#x5B57;&#x9762;&#x503C;&#x90FD;&#x53EF;&#x4EE5;&#x7528;&#x4EE5;0&#x958B;&#x59CB;&#x7684;&#x516B;&#x9032;&#x88FD;&#x683C;&#x5F0F;&#x66F8;&#x5BEB;&#xFF0C;&#x4F8B;&#x5982;0666&#xFF1B;&#x6216;&#x7528;&#x4EE5;0x&#x6216;0X&#x958B;&#x982D;&#x7684;&#x5341;&#x516D;&#x9032;&#x88FD;&#x683C;&#x5F0F;&#x66F8;&#x5BEB;&#xFF0C;&#x4F8B;&#x5982;0xdeadbeef&#x3002;&#x5341;&#x516D;&#x9032;&#x88FD;&#x6578;&#x5B57;&#x53EF;&#x4EE5;&#x7528;&#x5927;&#x5BEB;&#x6216;&#x5C0F;&#x5BEB;&#x5B57;&#x6BCD;&#x3002;&#x5982;&#x4ECA;&#x516B;&#x9032;&#x88FD;&#x6578;&#x64DA;&#x901A;&#x5E38;&#x7528;&#x65BC;POSIX&#x64CD;&#x4F5C;&#x7E6B;&#x7D71;&#x4E0A;&#x7684;&#x6587;&#x4EF6;&#x8A2A;&#x554F;&#x6B0A;&#x9650;&#x6A19;&#x8A8C;&#xFF0C;&#x5341;&#x516D;&#x9032;&#x88FD;&#x6578;&#x5B57;&#x5247;&#x66F4;&#x5F37;&#x8ABF;&#x6578;&#x5B57;&#x503C;&#x7684;bit&#x4F4D;&#x6A21;&#x5F0F;&#x3002;</p>
<p>&#x7576;&#x4F7F;&#x7528;fmt&#x5305;&#x6253;&#x5370;&#x4E00;&#x500B;&#x6578;&#x503C;&#x6642;&#xFF0C;&#x6211;&#x5011;&#x53EF;&#x4EE5;&#x7528;%d&#x3001;%o&#x6216;%x&#x53C3;&#x6578;&#x63A7;&#x88FD;&#x8F38;&#x51FA;&#x7684;&#x9032;&#x88FD;&#x683C;&#x5F0F;&#xFF0C;&#x5C31;&#x50CF;&#x4E0B;&#x9762;&#x7684;&#x4F8B;&#x5B50;&#xFF1A;</p>
<pre><code class="lang-Go">o := <span class="hljs-number">0666</span>
fmt.Printf(<span class="hljs-string">&quot;%d %[1]o %#[1]o\n&quot;</span>, o) <span class="hljs-comment">// &quot;438 666 0666&quot;</span>
x := <span class="hljs-typename">int64</span>(<span class="hljs-number">0xdeadbeef</span>)
fmt.Printf(<span class="hljs-string">&quot;%d %[1]x %#[1]x %#[1]X\n&quot;</span>, x)
<span class="hljs-comment">// Output:</span>
<span class="hljs-comment">// 3735928559 deadbeef 0xdeadbeef 0XDEADBEEF</span>
</code></pre>
<p>&#x8ACB;&#x8A3B;&#x610F;fmt&#x7684;&#x5169;&#x500B;&#x4F7F;&#x7528;&#x6280;&#x5DE7;&#x3002;&#x901A;&#x5E38;Printf&#x683C;&#x5F0F;&#x5316;&#x5B57;&#x7B26;&#x4E32;&#x5305;&#x542B;&#x591A;&#x500B;%&#x53C3;&#x6578;&#x6642;&#x5C07;&#x6703;&#x5305;&#x542B;&#x5C0D;&#x61C9;&#x76F8;&#x540C;&#x6578;&#x91CF;&#x7684;&#x984D;&#x5916;&#x64CD;&#x4F5C;&#x6578;&#xFF0C;&#x4F46;&#x662F;%&#x4E4B;&#x5F8C;&#x7684;<code>[1]</code>&#x526F;&#x8A5E;&#x544A;&#x8A34;Printf&#x51FD;&#x6578;&#x518D;&#x6B21;&#x4F7F;&#x7528;&#x7B2C;&#x4E00;&#x500B;&#x64CD;&#x4F5C;&#x6578;&#x3002;&#x7B2C;&#x4E8C;&#xFF0C;%&#x5F8C;&#x7684;<code>#</code>&#x526F;&#x8A5E;&#x544A;&#x8A34;Printf&#x5728;&#x7528;%o&#x3001;%x&#x6216;%X&#x8F38;&#x51FA;&#x6642;&#x751F;&#x6210;0&#x3001;0x&#x6216;0X&#x524D;&#x7DB4;&#x3002;</p>
<p>&#x5B57;&#x7B26;&#x9762;&#x503C;&#x901A;&#x904E;&#x4E00;&#x5C0D;&#x55AE;&#x5F15;&#x865F;&#x76F4;&#x63A5;&#x5305;&#x542B;&#x5C0D;&#x61C9;&#x5B57;&#x7B26;&#x3002;&#x6700;&#x7C21;&#x55AE;&#x7684;&#x4F8B;&#x5B50;&#x662F;ASCII&#x4E2D;&#x985E;&#x4F3C;&apos;a&apos;&#x5BEB;&#x6CD5;&#x7684;&#x5B57;&#x7B26;&#x9762;&#x503C;&#xFF0C;&#x4F46;&#x662F;&#x6211;&#x5011;&#x4E5F;&#x53EF;&#x4EE5;&#x901A;&#x904E;&#x8F49;&#x7FA9;&#x7684;&#x6578;&#x503C;&#x4F86;&#x8868;&#x793A;&#x4EFB;&#x610F;&#x7684;Unicode&#x78BC;&#x9EDE;&#x5C0D;&#x61C9;&#x7684;&#x5B57;&#x7B26;&#xFF0C;&#x99AC;&#x4E0A;&#x5C07;&#x6703;&#x770B;&#x5230;&#x9019;&#x6A23;&#x7684;&#x4F8B;&#x5B50;&#x3002;</p>
<p>&#x5B57;&#x7B26;&#x4F7F;&#x7528;<code>%c</code>&#x53C3;&#x6578;&#x6253;&#x5370;&#xFF0C;&#x6216;&#x8005;&#x662F;&#x7528;<code>%q</code>&#x53C3;&#x6578;&#x6253;&#x5370;&#x5E36;&#x55AE;&#x5F15;&#x865F;&#x7684;&#x5B57;&#x7B26;&#xFF1A;</p>
<pre><code class="lang-Go">ascii := <span class="hljs-string">&apos;a&apos;</span>
unicode := <span class="hljs-string">&apos;&#x56FD;&apos;</span>
newline := <span class="hljs-string">&apos;\n&apos;</span>
fmt.Printf(<span class="hljs-string">&quot;%d %[1]c %[1]q\n&quot;</span>, ascii) <span class="hljs-comment">// &quot;97 a &apos;a&apos;&quot;</span>
fmt.Printf(<span class="hljs-string">&quot;%d %[1]c %[1]q\n&quot;</span>, unicode) <span class="hljs-comment">// &quot;22269 &#x56FD; &apos;&#x56FD;&apos;&quot;</span>
fmt.Printf(<span class="hljs-string">&quot;%d %[1]q\n&quot;</span>, newline) <span class="hljs-comment">// &quot;10 &apos;\n&apos;&quot;</span>
</code></pre>
</section>
</div>
</div>
</div>
<a href="../ch3/ch3.html" class="navigation navigation-prev " aria-label="Previous page: 基礎數據類型"><i class="fa fa-angle-left"></i></a>
<a href="../ch3/ch3-02.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-sharing/buttons.js"></script>
<script src="../gitbook/plugins/gitbook-plugin-fontsettings/buttons.js"></script>
<script>
require(["gitbook"], function(gitbook) {
var config = {"katex":{},"highlight":{},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"fontsettings":{"theme":"white","family":"sans","size":2}};
gitbook.start(config);
});
</script>
</body>
</html>