gopl-zh.github.com/ch3/ch3-01.html
2015-12-21 12:55:18 +08:00

2191 lines
83 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="../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="Mon Dec 21 2015 12:51: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 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>
切片
</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="errata.html">
<a href="../errata.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;, &#x6D6E;&#x9EDE;&#x6578;, &#x548C;&#x8907;&#x6578;. &#x6BCF;&#x7A2E;&#x6578;&#x503C;&#x985E;&#x578B;&#x90FD;&#x6C7A;&#x5B9A;&#x4E86;&#x5C0D;&#x61C9;&#x7684;&#x5927;&#x5C0F;&#x8303;&#x570D;&#x548C;&#x662F;&#x5426;&#x6709;&#x6B63;&#x8CA0;&#x7B26;&#x865F;. &#x8B93;&#x6211;&#x5011;&#x5148;&#x5F9E;&#x6574;&#x5F62;&#x6578;&#x985E;&#x578B;&#x958B;&#x59CB;&#x4ECB;&#x7D39;.</p>
<p>Go&#x540C;&#x6642;&#x63D0;&#x4F9B;&#x4E86;&#x6709;&#x7B26;&#x865F;&#x548C;&#x7121;&#x7B26;&#x865F;&#x7684;&#x6574;&#x6578;&#x904B;&#x7B97;. &#x9019;&#x91CC;&#x6709;&#x56DB;&#x7A2E;int8, int16, int32 &#x548C; int64&#x622A;&#x7136;&#x4E0D;&#x540C;&#x5927;&#x5C0F;&#x7684;&#x6709;&#x7B26;&#x865F;&#x6574;&#x5F62;&#x6578;&#x985E;&#x578B;, &#x5206;&#x522B;&#x5C0D;&#x61C9; 8, 16, 32, 64 bit &#x5927;&#x5C0F;&#x7684;&#x6709;&#x7B26;&#x865F;&#x6574;&#x5F62;&#x6578;, &#x8207;&#x6B64;&#x5C0D;&#x61C9;&#x7684;&#x662F; uint8, uint16, uint32, &#x548C; uint64 &#x56DB;&#x7A2E;&#x7121;&#x7B26;&#x865F;&#x6574;&#x5F62;&#x6578;&#x985E;&#x578B;.</p>
<p>&#x9019;&#x91CC;&#x9084;&#x6709;&#x5169;&#x7A2E;&#x5C0D;&#x61C9;&#x7279;&#x5B9A;&#x5E73;&#x98B1;&#x6700;&#x5929;&#x7136;&#x6216;&#x6700;&#x6709;&#x6548;&#x7387;&#x7684;&#x5927;&#x5C0F;&#x6709;&#x7B26;&#x865F;&#x548C;&#x7121;&#x7B26;&#x865F;&#x6574;&#x6578;int&#x548C;uint; &#x5176;&#x4E2D;int&#x662F;&#x61C9;&#x7528;&#x6700;&#x5EE3;&#x6CDB;&#x7684;&#x6578;&#x503C;&#x985E;&#x578B;. &#x9019;&#x5169;&#x7A2E;&#x985E;&#x578B;&#x90FD;&#x6709;&#x540C;&#x6A23;&#x7684;&#x5927;&#x5C0F;, 32 &#x6216; 64 bit, &#x4F46;&#x662F;&#x6211;&#x5011;&#x4E0D;&#x80FD;&#x5C0D;&#x6B64;&#x505A;&#x4EFB;&#x4F55;&#x7684;&#x5047;&#x8A2D;; &#x56E0;&#x7232;&#x4E0D;&#x540C;&#x7684;&#x7DE8;&#x8B6F;&#x5668;&#x5728;&#x76F8;&#x540C;&#x7684;&#x786C;&#x4EF6;&#x5E73;&#x98B1;&#x4E0A;&#x53EF;&#x80FD;&#x7523;&#x751F;&#x4E0D;&#x540C;&#x7684;&#x5927;&#x5C0F;.</p>
<p>&#x5B57;&#x7B26;rune&#x985E;&#x578B;&#x662F;&#x548C;int32&#x7B49;&#x50F9;&#x7684;&#x985E;&#x578B;, &#x901A;&#x5E38;&#x7528;&#x65BC;&#x8868;&#x793A;&#x4E00;&#x500B;Unicode&#x78BC;&#x9EDE;. &#x9019;&#x5169;&#x500B;&#x540D;&#x7A31;&#x53EF;&#x4EE5;&#x4E92;&#x63DB;&#x4F7F;&#x7528;. &#x540C;&#x6A23;byte&#x4E5F;&#x662F;uint8&#x985E;&#x578B;&#x7684;&#x7B49;&#x50F9;&#x985E;&#x578B;, byte&#x985E;&#x578B;&#x7528;&#x65BC;&#x5F37;&#x8ABF;&#x6578;&#x503C;&#x662F;&#x4E00;&#x500B;&#x539F;&#x59CB;&#x7684;&#x6578;&#x64DA;&#x800C;&#x4E0D;&#x662F;&#x4E00;&#x500B;&#x5C0F;&#x7684;&#x6574;&#x6578;.</p>
<p>&#x6700;&#x597D;, &#x9084;&#x6709;&#x4E00;&#x500B;&#x7121;&#x7B26;&#x865F;&#x7684;&#x6574;&#x6578;&#x985E;&#x578B; uintptr, &#x6C92;&#x6709;&#x6307;&#x5B9A;&#x5177;&#x9AD4;&#x7684;bit&#x5927;&#x5C0F;&#x4F46;&#x662F;&#x8DB3;&#x4EE5;&#x5BB9;&#x7D0D;&#x6307;&#x91DD;. uintptr &#x985E;&#x578B;&#x96BB;&#x6709;&#x5728;&#x5E95;&#x5C64;&#x7DE8;&#x7A0B;&#x662F;&#x7E94;&#x9700;&#x8981;, &#x7279;&#x522B;&#x662F;Go&#x8A9E;&#x8A00;&#x548C;C&#x51FD;&#x6578;&#x5EAB;&#x6216;&#x64CD;&#x4F5C;&#x7E6B;&#x7D71;&#x76F8;&#x4EA4;&#x4E92;&#x7684;&#x5730;&#x65B9;. &#x6211;&#x5011;&#x5C07;&#x5728;&#x7B2C;&#x5341;&#x4E09;&#x7AE0;&#x7684; unsafe &#x5305;&#x76F8;&#x95DC;&#x90E8;&#x5206;&#x770B;&#x5230;&#x985E;&#x4F3C;&#x7684;&#x4F8B;&#x5B50;.</p>
<p>&#x4E0D;&#x7BA1;&#x5B83;&#x5011;&#x7684;&#x5927;&#x5C0F;, int, uint, &#x548C; uintptr &#x662F;&#x4E0D;&#x540C;&#x985E;&#x578B;&#x5927;&#x5C0F;&#x7684;&#x5144;&#x5F1F;&#x985E;&#x578B;. &#x5176;&#x4E2D; int &#x548C; int32 &#x4E5F;&#x662F;&#x4E0D;&#x540C;&#x7684;&#x985E;&#x578B;, &#x537D;&#x4F7F;int&#x7684;&#x5927;&#x5C0F;&#x4E5F;&#x662F;32bit, &#x5728;&#x9700;&#x8981;&#x5C07;int&#x7576;&#x4F5C;int32&#x985E;&#x578B;&#x7684;&#x5730;&#x65B9;&#x9700;&#x8981;&#x4E00;&#x500B;&#x986F;&#x5F0F;&#x7684;&#x985E;&#x578B;&#x8F49;&#x63DB;, &#x53CD;&#x4E4B;&#x4EA6;&#x7136;.</p>
<p>&#x6709;&#x7B26;&#x865F;&#x6578;&#x91C7;&#x7528;2&#x7684;&#x88DC;&#x78BC;&#x5F62;&#x5F0F;&#x8868;&#x793A;, &#x4E5F;&#x5C31;&#x662F;&#x6700;&#x9AD8;&#x4F4D;&#x7528;&#x4F5C;&#x7B26;&#x865F;&#x4F4D;, &#x4E00;&#x500B;nbit&#x7684;&#x6709;&#x7B26;&#x865F;&#x6578;&#x7684;&#x503C;&#x57DF;&#x662F; <code>-2^(n-1)</code> &#x5230; <code>(2^(n-1)) - 1</code>. &#x7121;&#x7B26;&#x865F;&#x6574;&#x6578;&#x7684;&#x6240;&#x6709;bit&#x4F4D;&#x90FD;&#x7528;&#x65BC;&#x8868;&#x793A;&#x975E;&#x8CA0;&#x6578;, &#x503C;&#x57DF;&#x662F; 0 &#x5230; <code>(2^n) - 1</code>. &#x4F8B;&#x5982;, int8 &#x7684;&#x503C;&#x57DF;&#x662F; -128 &#x5230; 127, &#x800C; uint8 &#x7684;&#x503C;&#x57DF;&#x662F; 0 &#x5230; 255.</p>
<p>&#x4E0B;&#x9762;&#x662F;Go&#x4E2D;&#x95DC;&#x65BC;&#x7B97;&#x8853;, &#x908F;&#x8F2F;&#x548C;&#x6BD4;&#x8F03;&#x7684;&#x4E8C;&#x5143;&#x904B;&#x7B97;&#x7B26;&#x6309;&#x7167;&#x5148;&#x7D1A;&#x905E;&#x6E1B;&#x7684;&#x9806;&#x5E8F;&#x7684;&#x5217;&#x8868;:</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;. &#x5728;&#x540C;&#x4E00;&#x512A;&#x5148;&#x7D1A;, &#x4F7F;&#x7528;&#x5DE6;&#x512A;&#x5148;&#x7D50;&#x5408;&#x5F8B;, &#x4F7F;&#x7528;&#x62EC;&#x865F;&#x53EF;&#x4EE5;&#x660E;&#x78BA;&#x512A;&#x5148;&#x9806;&#x5E8F;, &#x62EC;&#x865F;&#x4E5F;&#x53EF;&#x4EE5;&#x7528;&#x65BC;&#x63D0;&#x965E;&#x512A;&#x5148;&#x7D1A;, &#x4F8B;&#x5982; <code>mask &amp; (1 &lt;&lt; 28)</code>.</p>
<p>&#x5C0D;&#x65BC;&#x4E0A;&#x8868;&#x4E2D;&#x524D;&#x5169;&#x884C;&#x7684;&#x904B;&#x7B97;&#x7B26;, &#x4F8B;&#x5982; + &#x6709;&#x4E00;&#x500B;&#x76F8;&#x61C9;&#x7684;&#x8CE6;&#x503C;&#x7D50;&#x5408;&#x904B;&#x7B97;&#x7B26; +=, &#x53EF;&#x4EE5;&#x7528;&#x65BC;&#x7C21;&#x5316;&#x8CE6;&#x503C;&#x8A9E;&#x53E5;.</p>
<p>&#x6574;&#x6578;&#x7684;&#x7B97;&#x8853;&#x904B;&#x7B97;&#x7B26; +, -, *, &#x548C; / &#x53EF;&#x4EE5;&#x9069;&#x7528;&#x8207;&#x6574;&#x6578;, &#x6D6E;&#x9EDE;&#x6578;&#x548C;&#x8907;&#x6578;, &#x4F46;&#x662F;&#x53D6;&#x6A21;&#x904B;&#x7B97;&#x7B26; % &#x50C5;&#x7528;&#x65BC;&#x6574;&#x6578;. &#x4E0D;&#x540C;&#x7DE8;&#x7A0B;&#x8A9E;&#x8A00;&#x9593;, % &#x53D6;&#x6A21;&#x904B;&#x7B97;&#x7684;&#x884C;&#x7232;&#x4F75;&#x4E0D;&#x76F8;&#x540C;. &#x5728;Go&#x8A9E;&#x8A00;&#x4E2D;, % &#x53D6;&#x6A21;&#x904B;&#x7B97;&#x7B26;&#x7684;&#x7B26;&#x865F;&#x548C;&#x88AB;&#x53D6;&#x6A21;&#x6578;&#x7684;&#x7B26;&#x865F;&#x7E3D;&#x662F;&#x4E00;&#x81F4;&#x7684;, &#x56E0;&#x6B64; <code>-5%3</code> &#x548C; <code>-5%-3</code> &#x7D50;&#x679C;&#x90FD;&#x662F; -2.&#x9664;&#x6CD5;&#x904B;&#x7B97;&#x7B26; <code>/</code> &#x7684;&#x884C;&#x7232;&#x4F9D;&#x8CF4;&#x65BC;&#x64CD;&#x4F5C;&#x6578;&#x662F;&#x5426;&#x7232;&#x6574;&#x6578;, &#x56E0;&#x6B64; <code>5.0/4.0</code> &#x7684;&#x7D50;&#x679C;&#x662F; 1.25, &#x4F46;&#x662F; 5/4 &#x7684;&#x7D50;&#x679C;&#x662F; 1, &#x56E0;&#x6B64;&#x6574;&#x6578;&#x9664;&#x6CD5;&#x6703;&#x5411;&#x7740;0&#x65B9;&#x5411;&#x622A;&#x65B7;&#x9918;&#x6578;.</p>
<p>&#x5982;&#x679C;&#x4E00;&#x500B;&#x7B97;&#x8853;&#x904B;&#x7B97;&#x7684;&#x7D50;&#x679C;, &#x4E0D;&#x7BA1;&#x662F;&#x6709;&#x7B26;&#x865F;&#x6216;&#x8005;&#x662F;&#x7121;&#x7B26;&#x865F;&#x7684;, &#x5982;&#x679C;&#x9700;&#x8981;&#x66F4;&#x591A;&#x7684;bit&#x4F4D;&#x7E94;&#x80FD;&#x8868;&#x793A;, &#x5C31;&#x8AAC;&#x660E;&#x662F;&#x6EA2;&#x9F63;&#x4E86;. &#x8D85;&#x9F63;&#x7684;&#x9AD8;&#x4F4D;&#x7684;bit&#x4F4D;&#x90E8;&#x5206;&#x5C07;&#x88AB;&#x4E1F;&#x68C4;. &#x5982;&#x679C;&#x539F;&#x59CB;&#x7684;&#x6578;&#x503C;&#x662F;&#x6709;&#x7B26;&#x865F;&#x985E;&#x578B;, &#x90A3;&#x9EBD;&#x6700;&#x7D42;&#x7D50;&#x679C;&#x53EF;&#x80FD;&#x662F;&#x8CA0;&#x7684;, &#x5982;&#x679C;&#x6700;&#x5DE6;&#x908A;&#x7684;bit&#x7232;&#x662F;1&#x7684;&#x8A71;, &#x4F8B;&#x5982;int8&#x7684;&#x4F8B;&#x5B50;:</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;; &#x6BD4;&#x8F03;&#x8868;&#x9054;&#x5F0F;&#x7684;&#x7D50;&#x679C;&#x662F;&#x5E03;&#x723E;&#x985E;&#x578B;.</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;, &#x5E03;&#x723E;&#x578B;, &#x6578;&#x5B57;&#x985E;&#x578B; &#x548C; &#x5B57;&#x7B26;&#x4E32; &#x7B49;&#x57FA;&#x672C;&#x985E;&#x578B;&#x90FD;&#x662F;&#x53EF;&#x6BD4;&#x8F03;&#x7684;, &#x4E5F;&#x5C31;&#x662F;&#x8AAC;&#x5169;&#x500B;&#x76F8;&#x540C;&#x985E;&#x578B;&#x7684;&#x503C;&#x53EF;&#x4EE5;&#x7528; == &#x548C; != &#x9032;&#x884C;&#x6BD4;&#x8F03;. &#x6B64;&#x5916;, &#x6574;&#x6578;, &#x6D6E;&#x9EDE;&#x6578;&#x548C;&#x5B57;&#x7B26;&#x4E32;&#x53EF;&#x4EE5;&#x6839;&#x64DA;&#x6BD4;&#x8F03;&#x7D50;&#x679C;&#x6392;&#x5E8F;. &#x8A31;&#x591A;&#x5176;&#x4ED6;&#x985E;&#x578B;&#x7684;&#x503C;&#x662F;&#x4E0D;&#x53EF;&#x6BD4;&#x8F03;, &#x56E0;&#x6B64;&#x4E5F;&#x5C31;&#x662F;&#x4E0D;&#x53EF;&#x6392;&#x5E8F;&#x7684;. &#x5C0D;&#x65BC;&#x6211;&#x5011;&#x9047;&#x5230;&#x7684;&#x6BCF;&#x7A2E;&#x985E;&#x578B;, &#x6211;&#x5011;&#x9700;&#x8981;&#x4FDD;&#x8B49;&#x898F;&#x5247;&#x662F;&#x985E;&#x4F3C;&#x7684;.</p>
<p>&#x9019;&#x91CC;&#x662F;&#x4E00;&#x5143;&#x7684;&#x52A0;&#x6CD5;&#x548C;&#x6E1B;&#x6CD5;&#x904B;&#x7B97;&#x7B26;:</p>
<pre><code>+ &#x4E00;&#x5143;&#x52A0;&#x6CD5; (&#x7121;&#x6548;&#x679C;)
- &#x8CA0;&#x6578;
</code></pre><p>&#x5C0D;&#x65BC;&#x6574;&#x6578;, +x &#x662F; 0+x &#x7684;&#x7C21;&#x5BEB;, -x &#x662F; 0-x &#x7684;&#x7C21;&#x5BEB;; &#x5C0D;&#x65BC;&#x6D6E;&#x9EDE;&#x6578;&#x548C;&#x8907;&#x6578;, +x &#x5C31;&#x662F; x, -x &#x5247;&#x662F; x &#x7684;&#x8CA0;&#x6578;.</p>
<p>Go&#x8A9E;&#x8A00;&#x9084;&#x63D0;&#x4F9B;&#x4E86;&#x4EE5;&#x4E0B;&#x7684;bit&#x4F4D;&#x64CD;&#x4F5C;&#x904B;&#x7B97;&#x7B26;, &#x524D;&#x9762;4&#x500B;&#x64CD;&#x4F5C;&#x904B;&#x7B97;&#x7B26;&#x4F75;&#x4E0D;&#x5340;&#x5206;&#x662F;&#x6709;&#x7B26;&#x865F;&#x9084;&#x662F;&#x7121;&#x7B26;&#x865F;&#x6578;:</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;(XOR), &#x7576;&#x7528;&#x4F5C;&#x4E00;&#x5143;&#x904B;&#x7B97;&#x7B26;&#x6642;&#x8868;&#x793A;&#x6309;&#x4F4D;&#x53D6;&#x53CD;; &#x4E5F;&#x5C31;&#x662F;&#x8AAC;, &#x5B83;&#x8FD4;&#x8FF4;&#x4E00;&#x500B;&#x6BCF;&#x500B;bit&#x4F4D;&#x90FD;&#x53D6;&#x53CD;&#x7684;&#x6578;. &#x4F4D;&#x64CD;&#x4F5C;&#x904B;&#x7B97;&#x7B26; <code>&amp;^</code> &#x7528;&#x65BC;&#x6309;&#x4F4D;&#x7F6E;&#x96F6;(AND NOT): &#x8868;&#x9054;&#x5F0F; <code>z = x &amp;^ y</code> &#x7D50;&#x679C;z&#x7684;bit&#x4F4D;1, &#x5982;&#x679C;&#x5C0D;&#x61C9;y&#x4E2D;bit&#x4F4D;&#x7232;1, &#x5426;&#x5247;&#x5C0D;&#x61C9;&#x7684;bit&#x4F4D;&#x7B49;&#x65BC;x&#x76F8;&#x61C9;&#x7684;bit&#x4F4D;&#x7684;&#x503C;.</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;. &#x5B83;&#x4F7F;&#x7528;&#x4E86; Printf &#x51FD;&#x6578;&#x7684; %b &#x53C3;&#x6578;&#x6253;&#x5370;&#x4E8C;&#x9032;&#x88FD;&#x683C;&#x5F0F;&#x7684;&#x6578;&#x5B57;; &#x5176;&#x4E2D; %08b &#x4E2D;08&#x8868;&#x793A;&#x6253;&#x5370;&#x81F3;&#x5C11;8&#x500B;&#x6578;&#x5B57;, &#x4E0D;&#x8DB3;&#x7684;&#x524D;&#x7DB4;&#x7528;0&#x586B;&#x5145;.</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>(6.5&#x7BC0;&#x7D66;&#x9F63;&#x4E86;&#x4E00;&#x500B;&#x53EF;&#x4EE5;&#x9060;&#x5927;&#x65BC;&#x4E00;&#x500B;&#x5B57;&#x7BC0;&#x7684;&#x6574;&#x6578;&#x96C6;&#x7684;&#x5BE6;&#x73FE;.)</p>
<p>&#x5728; x&lt;<n ="" x="">&gt;n &#x79FB;&#x4F4D;&#x904B;&#x7B97;&#x4E2D;, &#x6C7A;&#x5B9A;&#x4E86;&#x79FB;&#x4F4D;&#x64CD;&#x4F5C;bit&#x6578;&#x90E8;&#x5206;&#x5FC5;&#x9B1A;&#x662F;&#x7121;&#x7B26;&#x865F;&#x6578;; &#x88AB;&#x64CD;&#x4F5C;&#x7684; x &#x6578;&#x53EF;&#x4EE5;&#x662F;&#x6709;&#x7B26;&#x865F;&#x6216;&#x7121;&#x7B26;&#x865F;&#x6578;. &#x7B97;&#x8853;&#x4E0A;, &#x4E00;&#x500B; x&lt;<n 左移運算等價於乘以="" 2^n,="" 一個="" x="">&gt;n &#x53F3;&#x79FB;&#x904B;&#x7B97;&#x7B49;&#x50F9;&#x65BC;&#x9664;&#x4EE5; 2^n.</n></n></p>
<p>&#x5DE6;&#x79FB;&#x904B;&#x7B97;&#x7528;&#x96F6;&#x586B;&#x5145;&#x53F3;&#x908A;&#x7A7A;&#x7F3A;&#x7684;bit&#x4F4D;, &#x7121;&#x7B26;&#x865F;&#x6578;&#x7684;&#x53F3;&#x79FB;&#x904B;&#x7B97;&#x4E5F;&#x662F;&#x7528;0&#x586B;&#x5145;&#x5DE6;&#x908A;&#x7A7A;&#x7F3A;&#x7684;bit&#x4F4D;, &#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;. &#x56E0;&#x7232;&#x9019;&#x500B;&#x539F;&#x56E0;, &#x6700;&#x597D;&#x7528;&#x7121;&#x7B26;&#x865F;&#x904B;&#x7B97;, &#x9019;&#x6A23;&#x4F60;&#x53EF;&#x4EE5;&#x5C07;&#x6574;&#x6578;&#x5B8C;&#x5168;&#x7576;&#x4F5C;&#x4E00;&#x500B;bit&#x4F4D;&#x6A21;&#x5F0F;&#x8655;&#x7406;.</p>
<p>&#x76E1;&#x7BA1;Go&#x63D0;&#x4F9B;&#x4E86;&#x7121;&#x7B26;&#x865F;&#x6578;&#x548C;&#x904B;&#x7B97;, &#x537D;&#x4F7F;&#x6578;&#x503C;&#x672C;&#x8EAB;&#x4E0D;&#x53EF;&#x80FD;&#x9F63;&#x73FE;&#x8CA0;&#x6578;&#x6211;&#x5011;&#x9084;&#x662F;&#x50BE;&#x5411;&#x65BC;&#x4F7F;&#x7528;&#x6709;&#x7B26;&#x865F;&#x7684;int&#x985E;&#x578B;, &#x5C31;&#x662F;&#x6578;&#x7D44;&#x7684;&#x9577;&#x5EA6;&#x90A3;&#x6A23;, &#x96D6;&#x7136;&#x4F7F;&#x7528; uint &#x4F3C;&#x4E4E;&#x662F;&#x4E00;&#x500B;&#x66F4;&#x5408;&#x7406;&#x7684;&#x9078;&#x64C7;. &#x4E8B;&#x5BE6;&#x4E0A;, &#x5167;&#x7F6E;&#x7684; len &#x51FD;&#x6578;&#x8FD4;&#x8FF4;&#x4E00;&#x500B;&#x6709;&#x7B26;&#x865F;&#x7684;int, &#x6211;&#x5011;&#x53EF;&#x4EE5;&#x50CF;&#x4E0B;&#x9762;&#x9019;&#x500B;&#x9006;&#x5E8F;&#x5FAA;&#x74B0;&#x90A3;&#x6A23;&#x8655;&#x7406;.</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;&#x5C07;&#x662F;&#x707D;&#x96E3;&#x6027;&#x7684;. &#x5982;&#x679C; len &#x8FD4;&#x8FF4;&#x4E00;&#x500B;&#x7121;&#x7B26;&#x865F;&#x6578;, &#x90A3;&#x9EBD; i &#x4E5F;&#x5C07;&#x662F;&#x7121;&#x7B26;&#x865F;&#x7684; uint, &#x7136;&#x5F8C;&#x689D;&#x4EF6; i &gt;= 0 &#x5247;&#x6C38;&#x9060;&#x7232;&#x771E;. &#x5728;&#x4E09;&#x6B21;&#x8FED;&#x4EE3;&#x4E4B;&#x5F8C;, &#x4E5F;&#x5C31;&#x662F; i == 0 &#x6642;, i-- &#x8A9E;&#x53E5;&#x5C07;&#x4E0D;&#x6703;&#x7523;&#x751F; -1, &#x800C;&#x662F;&#x8B8A;&#x6210;&#x4E00;&#x500B;uint&#x7684;&#x6700;&#x5927;&#x503C;(&#x53EF;&#x80FD;&#x662F; 2^64 - 1), &#x7136;&#x5F8C; medals[i] &#x8868;&#x9054;&#x5F0F;&#x5C07;&#x767C;&#x751F;&#x904B;&#x884C;&#x6642; panic &#x7570;&#x5E38;(&#xA7;5.9), &#x4E5F;&#x5C31;&#x662F;&#x8A66;&#x5716;&#x8A2A;&#x554F;&#x4E00;&#x500B;&#x5207;&#x7247;&#x8303;&#x570D;&#x4EE5;&#x5916;&#x7684;&#x5143;&#x7D20;.</p>
<p>&#x9F63;&#x65BC;&#x9019;&#x500B;&#x539F;&#x56E0;, &#x7121;&#x7B26;&#x865F;&#x6578;&#x5F80;&#x5F80;&#x96BB;&#x6709;&#x5728;&#x4F4D;&#x904B;&#x7B97;&#x6216;&#x5176;&#x5B83;&#x7279;&#x6B8A;&#x7684;&#x904B;&#x7B97;&#x5E38;&#x898B;&#x7E94;&#x6703;&#x4F7F;&#x7528;, &#x5C31;&#x50CF; bit &#x96C6;&#x5408;, &#x5206;&#x5F62;&#x4E8C;&#x9032;&#x88FD;&#x6587;&#x4EF6;&#x683C;&#x5F0F;, &#x6216;&#x8005;&#x662F;&#x54C8;&#x5E0C;&#x548C;&#x52A0;&#x5BC6;&#x64CD;&#x4F5C;&#x7B49;. &#x5B83;&#x5011;&#x901A;&#x5E38;&#x4F75;&#x4E0D;&#x7528;&#x65BC;&#x50C5;&#x50C5;&#x662F;&#x8868;&#x9054;&#x975E;&#x8CA0;&#x6578;&#x91CF;&#x7684;&#x5834;&#x5408;.</p>
<p>&#x4E00;&#x822C;&#x4F86;&#x8AAC;, &#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;, &#x4F75;&#x4E14;&#x7B97;&#x8853;&#x548C;&#x908F;&#x8F2F;&#x904B;&#x7B97;&#x7684;&#x4E8C;&#x5143;&#x64CD;&#x4F5C;&#x4E2D;&#x5FC5;&#x9B1A;&#x662F;&#x76F8;&#x540C;&#x7684;&#x985E;&#x578B;. &#x96D6;&#x7136;&#x9019;&#x5076;&#x723E;&#x6703;&#x5C0E;&#x81F4;&#x5F88;&#x9577;&#x7684;&#x8868;&#x9054;&#x5F0F;, &#x4F46;&#x662F;&#x5B83;&#x6D88;&#x9664;&#x4E86;&#x6240;&#x6709;&#x7684;&#x985E;&#x578B;&#x76F8;&#x95DC;&#x7684;&#x554F;&#x984C;, &#x4E5F;&#x4F7F;&#x5F97;&#x7A0B;&#x5E8F;&#x5BB9;&#x6613;&#x7406;&#x89E3;.</p>
<p>&#x5F9E;&#x5176;&#x4ED6;&#x985E;&#x4F3C;&#x5834;&#x666F;&#x4E0B;, &#x8003;&#x616E;&#x4E0B;&#x9762;&#x9019;&#x500B;&#x4EE3;&#x78BC;:</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;, &#x5C07;&#x7523;&#x751F;&#x4E00;&#x500B;&#x932F;&#x8AA4;&#x4FE1;&#x606F;:</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;, &#x6700;&#x5E38;&#x898B;&#x65B9;&#x6CD5;&#x662F;&#x5C07;&#x5B83;&#x5011;&#x90FD;&#x986F;&#x5F0F;&#x8F49;&#x578B;&#x4F4D;&#x4E00;&#x500B;&#x5E38;&#x898B;&#x985E;&#x578B;:</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;, &#x5C0D;&#x65BC;&#x6BCF;&#x7A2E;&#x985E;&#x578B;T, &#x985E;&#x578B;&#x8F49;&#x63DB;&#x64CD;&#x4F5C;T(x)&#x5C07;x&#x8F49;&#x63DB;&#x4F4D;T&#x985E;&#x578B;, &#x5982;&#x679C;&#x8F49;&#x63DB;&#x5141;&#x8A31;&#x7684;&#x8A71;. &#x8A31;&#x591A; &#x6574;&#x5F62;&#x6578;&#x4E4B;&#x9593;&#x7684;&#x76F8;&#x4E92;&#x8F49;&#x63DB;&#x4F75;&#x4E0D;&#x6703;&#x6539;&#x8B8A;&#x6578;&#x503C;; &#x5B83;&#x5011;&#x96BB;&#x662F;&#x544A;&#x8A34;&#x7DE8;&#x8B6F;&#x5668;&#x5982;&#x4F55;&#x89E3;&#x91CB;&#x9019;&#x500B;&#x503C;. &#x4F46;&#x662F;&#x5C0D;&#x65BC;&#x5C07;&#x4E00;&#x500B;&#x5927;&#x5C3A;&#x5BF8;&#x7684;&#x6574;&#x6578;&#x985E;&#x578B;&#x8F49;&#x4F4D;&#x4E00;&#x500B;&#x5C0F;&#x5C3A;&#x5BF8;&#x7684;&#x6574;&#x6578;&#x985E;&#x578B;, &#x6216;&#x8005;&#x662F;&#x5C07;&#x4E00;&#x500B;&#x6D6E;&#x9EDE;&#x6578;&#x8F49;&#x4F4D;&#x6574;&#x6578;, &#x53EF;&#x80FD;&#x6703;&#x6539;&#x8B8A;&#x6578;&#x503C;&#x6216;&#x4E1F;&#x5931;&#x7CBE;&#x5EA6;:</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;, &#x5411;&#x6578;&#x8EF8;&#x96F6;&#x65B9;&#x5411;&#x622A;&#x65B7;. &#x4F60;&#x61C9;&#x8A72;&#x907F;&#x514D;&#x64CD;&#x4F5C;&#x76EE;&#x6A19;&#x985E;&#x578B;&#x8868;&#x793A;&#x8303;&#x570D;&#x7684;&#x6578;&#x503C;&#x985E;&#x578B;&#x8F49;&#x63DB;, &#x56E0;&#x7232;&#x622A;&#x65B7;&#x7684;&#x884C;&#x7232;&#x4F9D;&#x8CF4;&#x65BC;&#x5177;&#x9AD4;&#x7684;&#x5BE6;&#x73FE;:</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;, &#x4F8B;&#x5982; 0666, &#x6216;&#x7528;&#x4EE5;0x&#x6216;0X&#x958B;&#x982D;&#x7684;&#x5341;&#x516D;&#x9032;&#x88FD;&#x683C;&#x5F0F;&#x66F8;&#x5BEB;, &#x4F8B;&#x5982; 0xdeadbeef. &#x5341;&#x516D;&#x9032;&#x88FD;&#x6578;&#x5B57;&#x53EF;&#x4EE5;&#x7528;&#x5927;&#x5BEB;&#x6216;&#x5C0F;&#x5BEB;&#x5B57;&#x6BCD;. &#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;, &#x5341;&#x516D;&#x9032;&#x88FD;&#x6578;&#x5B57;&#x5247;&#x66F4;&#x5F37;&#x8ABF;&#x6578;&#x5B57;&#x503C;&#x7684;bit&#x4F4D;&#x6A21;&#x5F0F;.</p>
<p>&#x7576;&#x4F7F;&#x7528; fmt &#x5305;&#x6253;&#x5370;&#x4E00;&#x500B;&#x6578;&#x503C;&#x6642;, &#x6211;&#x5011;&#x53EF;&#x4EE5;&#x7528; %d, %o, &#x6216; %x &#x63A7;&#x88FD;&#x8F38;&#x9F63;&#x7684;&#x9032;&#x88FD;&#x683C;&#x5F0F;, &#x5C31;&#x50CF;&#x4E0B;&#x9762;&#x7684;&#x4F8B;&#x5B50;:</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;. &#x901A;&#x5E38; Printf &#x683C;&#x5F0F;&#x5316;&#x5B57;&#x7B26;&#x4E32;&#x5305;&#x542B;&#x591A;&#x500B; % &#x53C3;&#x6578;&#x6642;&#x5C07;&#x5C0D;&#x61C9;&#x76F8;&#x540C;&#x6578;&#x91CF;&#x7684;&#x984D;&#x5916;&#x64CD;&#x4F5C;&#x6578;, &#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;. &#x7B2C;&#x4E8C;, % &#x5F8C;&#x7684; <code>#</code> &#x526F;&#x8A5E;&#x544A;&#x8A34; Printf &#x5728;&#x7528; %o, %x &#x6216; %X &#x8F38;&#x9F63;&#x6642;&#x751F;&#x6210; 0, 0x &#x6216; 0X&#x524D;&#x7DB4;.</p>
<p>&#x5B57;&#x7B26;&#x9762;&#x503C;&#x901A;&#x904E;&#x4E00;&#x5C0D;&#x55AE;&#x5F15;&#x865F;&#x76F4;&#x63A5;&#x5305;&#x542B;&#x5C0D;&#x61C9;&#x5B57;&#x7B26;. &#x6700;&#x7C21;&#x55AE;&#x7684;&#x4F8B;&#x5B50;&#x662F; ASCII &#x4E2D;&#x985E;&#x4F3C; &apos;a&apos; &#x5B57;&#x7B26;&#x9762;&#x503C;, &#x4F46;&#x662F;&#x6211;&#x5011;&#x53EF;&#x4EE5;&#x901A;&#x904E;&#x8F49;&#x7FA9;&#x7684;&#x6578;&#x503C;&#x4F86;&#x8868;&#x793A;&#x4EFB;&#x610F;&#x7684;Unicode&#x78BC;&#x9EDE;&#x5C0D;&#x61C9;&#x7684;&#x5B57;&#x7B26;, &#x99AC;&#x4E0A;&#x5C07;&#x6703;&#x770B;&#x5230;&#x4F8B;&#x5B50;.</p>
<p>&#x5B57;&#x7B26;&#x4F7F;&#x7528; <code>%c</code> &#x53C3;&#x6578;&#x6253;&#x5370;, &#x6216;&#x8005;&#x662F; <code>%q</code> &#x53C3;&#x6578;&#x6253;&#x5370;&#x5E36;&#x55AE;&#x5F15;&#x865F;&#x7684;&#x5B57;&#x7B26;:</p>
<pre><code class="lang-Go">ascii := <span class="hljs-string">&apos;a&apos;</span>
unicode := <span class="hljs-string">&apos;&#x570B;&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 &#x570B; &apos;&#x570B;&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-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>