mirror of
https://github.com/gopl-zh/gopl-zh.github.com.git
synced 2024-12-27 07:16:22 +00:00
2295 lines
93 KiB
HTML
2295 lines
93 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="../ch2/ch2-04.html" />
|
||
|
|
||
|
|
||
|
<link rel="prev" href="../ch2/ch2-02.html" />
|
||
|
|
||
|
|
||
|
|
||
|
</head>
|
||
|
<body>
|
||
|
|
||
|
|
||
|
<div class="book" data-level="2.3" data-chapter-title="變量" data-filepath="ch2/ch2-03.md" data-basepath=".." data-revision="Wed Dec 09 2015 15:54:13 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 active" data-level="2.3" data-path="ch2/ch2-03.html">
|
||
|
|
||
|
|
||
|
<a href="../ch2/ch2-03.html">
|
||
|
|
||
|
<i class="fa fa-check"></i>
|
||
|
|
||
|
<b>2.3.</b>
|
||
|
|
||
|
變量
|
||
|
</a>
|
||
|
|
||
|
|
||
|
</li>
|
||
|
|
||
|
<li class="chapter " data-level="2.4" data-path="ch2/ch2-04.html">
|
||
|
|
||
|
|
||
|
<a href="../ch2/ch2-04.html">
|
||
|
|
||
|
<i class="fa fa-check"></i>
|
||
|
|
||
|
<b>2.4.</b>
|
||
|
|
||
|
賦值
|
||
|
</a>
|
||
|
|
||
|
|
||
|
</li>
|
||
|
|
||
|
<li class="chapter " data-level="2.5" data-path="ch2/ch2-05.html">
|
||
|
|
||
|
|
||
|
<a href="../ch2/ch2-05.html">
|
||
|
|
||
|
<i class="fa fa-check"></i>
|
||
|
|
||
|
<b>2.5.</b>
|
||
|
|
||
|
類型
|
||
|
</a>
|
||
|
|
||
|
|
||
|
</li>
|
||
|
|
||
|
<li class="chapter " data-level="2.6" data-path="ch2/ch2-06.html">
|
||
|
|
||
|
|
||
|
<a href="../ch2/ch2-06.html">
|
||
|
|
||
|
<i class="fa fa-check"></i>
|
||
|
|
||
|
<b>2.6.</b>
|
||
|
|
||
|
包和文件
|
||
|
</a>
|
||
|
|
||
|
|
||
|
</li>
|
||
|
|
||
|
<li class="chapter " data-level="2.7" data-path="ch2/ch2-07.html">
|
||
|
|
||
|
|
||
|
<a href="../ch2/ch2-07.html">
|
||
|
|
||
|
<i class="fa fa-check"></i>
|
||
|
|
||
|
<b>2.7.</b>
|
||
|
|
||
|
作用域
|
||
|
</a>
|
||
|
|
||
|
|
||
|
</li>
|
||
|
|
||
|
|
||
|
</ul>
|
||
|
|
||
|
</li>
|
||
|
|
||
|
<li class="chapter " data-level="3" data-path="ch3/ch3.html">
|
||
|
|
||
|
|
||
|
<a href="../ch3/ch3.html">
|
||
|
|
||
|
<i class="fa fa-check"></i>
|
||
|
|
||
|
<b>3.</b>
|
||
|
|
||
|
基礎數據類型
|
||
|
</a>
|
||
|
|
||
|
|
||
|
<ul class="articles">
|
||
|
|
||
|
|
||
|
<li class="chapter " data-level="3.1" data-path="ch3/ch3-01.html">
|
||
|
|
||
|
|
||
|
<a href="../ch3/ch3-01.html">
|
||
|
|
||
|
<i class="fa fa-check"></i>
|
||
|
|
||
|
<b>3.1.</b>
|
||
|
|
||
|
整型
|
||
|
</a>
|
||
|
|
||
|
|
||
|
</li>
|
||
|
|
||
|
<li class="chapter " data-level="3.2" data-path="ch3/ch3-02.html">
|
||
|
|
||
|
|
||
|
<a href="../ch3/ch3-02.html">
|
||
|
|
||
|
<i class="fa fa-check"></i>
|
||
|
|
||
|
<b>3.2.</b>
|
||
|
|
||
|
浮點數
|
||
|
</a>
|
||
|
|
||
|
|
||
|
</li>
|
||
|
|
||
|
<li class="chapter " data-level="3.3" data-path="ch3/ch3-03.html">
|
||
|
|
||
|
|
||
|
<a href="../ch3/ch3-03.html">
|
||
|
|
||
|
<i class="fa fa-check"></i>
|
||
|
|
||
|
<b>3.3.</b>
|
||
|
|
||
|
復數
|
||
|
</a>
|
||
|
|
||
|
|
||
|
</li>
|
||
|
|
||
|
<li class="chapter " data-level="3.4" data-path="ch3/ch3-04.html">
|
||
|
|
||
|
|
||
|
<a href="../ch3/ch3-04.html">
|
||
|
|
||
|
<i class="fa fa-check"></i>
|
||
|
|
||
|
<b>3.4.</b>
|
||
|
|
||
|
佈爾型
|
||
|
</a>
|
||
|
|
||
|
|
||
|
</li>
|
||
|
|
||
|
<li class="chapter " data-level="3.5" data-path="ch3/ch3-05.html">
|
||
|
|
||
|
|
||
|
<a href="../ch3/ch3-05.html">
|
||
|
|
||
|
<i class="fa fa-check"></i>
|
||
|
|
||
|
<b>3.5.</b>
|
||
|
|
||
|
字符串
|
||
|
</a>
|
||
|
|
||
|
|
||
|
</li>
|
||
|
|
||
|
<li class="chapter " data-level="3.6" data-path="ch3/ch3-06.html">
|
||
|
|
||
|
|
||
|
<a href="../ch3/ch3-06.html">
|
||
|
|
||
|
<i class="fa fa-check"></i>
|
||
|
|
||
|
<b>3.6.</b>
|
||
|
|
||
|
常量
|
||
|
</a>
|
||
|
|
||
|
|
||
|
</li>
|
||
|
|
||
|
|
||
|
</ul>
|
||
|
|
||
|
</li>
|
||
|
|
||
|
<li class="chapter " data-level="4" data-path="ch4/ch4.html">
|
||
|
|
||
|
|
||
|
<a href="../ch4/ch4.html">
|
||
|
|
||
|
<i class="fa fa-check"></i>
|
||
|
|
||
|
<b>4.</b>
|
||
|
|
||
|
復閤數據類型
|
||
|
</a>
|
||
|
|
||
|
|
||
|
<ul class="articles">
|
||
|
|
||
|
|
||
|
<li class="chapter " data-level="4.1" data-path="ch4/ch4-01.html">
|
||
|
|
||
|
|
||
|
<a href="../ch4/ch4-01.html">
|
||
|
|
||
|
<i class="fa fa-check"></i>
|
||
|
|
||
|
<b>4.1.</b>
|
||
|
|
||
|
數組
|
||
|
</a>
|
||
|
|
||
|
|
||
|
</li>
|
||
|
|
||
|
<li class="chapter " data-level="4.2" data-path="ch4/ch4-02.html">
|
||
|
|
||
|
|
||
|
<a href="../ch4/ch4-02.html">
|
||
|
|
||
|
<i class="fa fa-check"></i>
|
||
|
|
||
|
<b>4.2.</b>
|
||
|
|
||
|
切片
|
||
|
</a>
|
||
|
|
||
|
|
||
|
</li>
|
||
|
|
||
|
<li class="chapter " data-level="4.3" data-path="ch4/ch4-03.html">
|
||
|
|
||
|
|
||
|
<a href="../ch4/ch4-03.html">
|
||
|
|
||
|
<i class="fa fa-check"></i>
|
||
|
|
||
|
<b>4.3.</b>
|
||
|
|
||
|
字典
|
||
|
</a>
|
||
|
|
||
|
|
||
|
</li>
|
||
|
|
||
|
<li class="chapter " data-level="4.4" data-path="ch4/ch4-04.html">
|
||
|
|
||
|
|
||
|
<a href="../ch4/ch4-04.html">
|
||
|
|
||
|
<i class="fa fa-check"></i>
|
||
|
|
||
|
<b>4.4.</b>
|
||
|
|
||
|
結構體
|
||
|
</a>
|
||
|
|
||
|
|
||
|
</li>
|
||
|
|
||
|
<li class="chapter " data-level="4.5" data-path="ch4/ch4-05.html">
|
||
|
|
||
|
|
||
|
<a href="../ch4/ch4-05.html">
|
||
|
|
||
|
<i class="fa fa-check"></i>
|
||
|
|
||
|
<b>4.5.</b>
|
||
|
|
||
|
JSON
|
||
|
</a>
|
||
|
|
||
|
|
||
|
</li>
|
||
|
|
||
|
<li class="chapter " data-level="4.6" data-path="ch4/ch4-06.html">
|
||
|
|
||
|
|
||
|
<a href="../ch4/ch4-06.html">
|
||
|
|
||
|
<i class="fa fa-check"></i>
|
||
|
|
||
|
<b>4.6.</b>
|
||
|
|
||
|
文本和HTML模闆
|
||
|
</a>
|
||
|
|
||
|
|
||
|
</li>
|
||
|
|
||
|
|
||
|
</ul>
|
||
|
|
||
|
</li>
|
||
|
|
||
|
<li class="chapter " data-level="5" data-path="ch5/ch5.html">
|
||
|
|
||
|
|
||
|
<a href="../ch5/ch5.html">
|
||
|
|
||
|
<i class="fa fa-check"></i>
|
||
|
|
||
|
<b>5.</b>
|
||
|
|
||
|
函數
|
||
|
</a>
|
||
|
|
||
|
|
||
|
<ul class="articles">
|
||
|
|
||
|
|
||
|
<li class="chapter " data-level="5.1" data-path="ch5/ch5-01.html">
|
||
|
|
||
|
|
||
|
<a href="../ch5/ch5-01.html">
|
||
|
|
||
|
<i class="fa fa-check"></i>
|
||
|
|
||
|
<b>5.1.</b>
|
||
|
|
||
|
函數聲明
|
||
|
</a>
|
||
|
|
||
|
|
||
|
</li>
|
||
|
|
||
|
<li class="chapter " data-level="5.2" data-path="ch5/ch5-02.html">
|
||
|
|
||
|
|
||
|
<a href="../ch5/ch5-02.html">
|
||
|
|
||
|
<i class="fa fa-check"></i>
|
||
|
|
||
|
<b>5.2.</b>
|
||
|
|
||
|
遞歸
|
||
|
</a>
|
||
|
|
||
|
|
||
|
</li>
|
||
|
|
||
|
<li class="chapter " data-level="5.3" data-path="ch5/ch5-03.html">
|
||
|
|
||
|
|
||
|
<a href="../ch5/ch5-03.html">
|
||
|
|
||
|
<i class="fa fa-check"></i>
|
||
|
|
||
|
<b>5.3.</b>
|
||
|
|
||
|
多返迴值
|
||
|
</a>
|
||
|
|
||
|
|
||
|
</li>
|
||
|
|
||
|
<li class="chapter " data-level="5.4" data-path="ch5/ch5-04.html">
|
||
|
|
||
|
|
||
|
<a href="../ch5/ch5-04.html">
|
||
|
|
||
|
<i class="fa fa-check"></i>
|
||
|
|
||
|
<b>5.4.</b>
|
||
|
|
||
|
錯誤
|
||
|
</a>
|
||
|
|
||
|
|
||
|
</li>
|
||
|
|
||
|
<li class="chapter " data-level="5.5" data-path="ch5/ch5-05.html">
|
||
|
|
||
|
|
||
|
<a href="../ch5/ch5-05.html">
|
||
|
|
||
|
<i class="fa fa-check"></i>
|
||
|
|
||
|
<b>5.5.</b>
|
||
|
|
||
|
函數值
|
||
|
</a>
|
||
|
|
||
|
|
||
|
</li>
|
||
|
|
||
|
<li class="chapter " data-level="5.6" data-path="ch5/ch5-06.html">
|
||
|
|
||
|
|
||
|
<a href="../ch5/ch5-06.html">
|
||
|
|
||
|
<i class="fa fa-check"></i>
|
||
|
|
||
|
<b>5.6.</b>
|
||
|
|
||
|
匿名函數
|
||
|
</a>
|
||
|
|
||
|
|
||
|
</li>
|
||
|
|
||
|
<li class="chapter " data-level="5.7" data-path="ch5/ch5-07.html">
|
||
|
|
||
|
|
||
|
<a href="../ch5/ch5-07.html">
|
||
|
|
||
|
<i class="fa fa-check"></i>
|
||
|
|
||
|
<b>5.7.</b>
|
||
|
|
||
|
可變參數
|
||
|
</a>
|
||
|
|
||
|
|
||
|
</li>
|
||
|
|
||
|
<li class="chapter " data-level="5.8" data-path="ch5/ch5-08.html">
|
||
|
|
||
|
|
||
|
<a href="../ch5/ch5-08.html">
|
||
|
|
||
|
<i class="fa fa-check"></i>
|
||
|
|
||
|
<b>5.8.</b>
|
||
|
|
||
|
Deferred函數
|
||
|
</a>
|
||
|
|
||
|
|
||
|
</li>
|
||
|
|
||
|
<li class="chapter " data-level="5.9" data-path="ch5/ch5-09.html">
|
||
|
|
||
|
|
||
|
<a href="../ch5/ch5-09.html">
|
||
|
|
||
|
<i class="fa fa-check"></i>
|
||
|
|
||
|
<b>5.9.</b>
|
||
|
|
||
|
Panic異常
|
||
|
</a>
|
||
|
|
||
|
|
||
|
</li>
|
||
|
|
||
|
<li class="chapter " data-level="5.10" data-path="ch5/ch5-10.html">
|
||
|
|
||
|
|
||
|
<a href="../ch5/ch5-10.html">
|
||
|
|
||
|
<i class="fa fa-check"></i>
|
||
|
|
||
|
<b>5.10.</b>
|
||
|
|
||
|
Recover捕穫異常
|
||
|
</a>
|
||
|
|
||
|
|
||
|
</li>
|
||
|
|
||
|
|
||
|
</ul>
|
||
|
|
||
|
</li>
|
||
|
|
||
|
<li class="chapter " data-level="6" data-path="ch6/ch6.html">
|
||
|
|
||
|
|
||
|
<a href="../ch6/ch6.html">
|
||
|
|
||
|
<i class="fa fa-check"></i>
|
||
|
|
||
|
<b>6.</b>
|
||
|
|
||
|
方法
|
||
|
</a>
|
||
|
|
||
|
|
||
|
<ul class="articles">
|
||
|
|
||
|
|
||
|
<li class="chapter " data-level="6.1" data-path="ch6/ch6-01.html">
|
||
|
|
||
|
|
||
|
<a href="../ch6/ch6-01.html">
|
||
|
|
||
|
<i class="fa fa-check"></i>
|
||
|
|
||
|
<b>6.1.</b>
|
||
|
|
||
|
方法聲明
|
||
|
</a>
|
||
|
|
||
|
|
||
|
</li>
|
||
|
|
||
|
<li class="chapter " data-level="6.2" data-path="ch6/ch6-02.html">
|
||
|
|
||
|
|
||
|
<a href="../ch6/ch6-02.html">
|
||
|
|
||
|
<i class="fa fa-check"></i>
|
||
|
|
||
|
<b>6.2.</b>
|
||
|
|
||
|
基於指鍼對象的方法
|
||
|
</a>
|
||
|
|
||
|
|
||
|
</li>
|
||
|
|
||
|
<li class="chapter " data-level="6.3" data-path="ch6/ch6-03.html">
|
||
|
|
||
|
|
||
|
<a href="../ch6/ch6-03.html">
|
||
|
|
||
|
<i class="fa fa-check"></i>
|
||
|
|
||
|
<b>6.3.</b>
|
||
|
|
||
|
通過嵌入結構體來擴展類型
|
||
|
</a>
|
||
|
|
||
|
|
||
|
</li>
|
||
|
|
||
|
<li class="chapter " data-level="6.4" data-path="ch6/ch6-04.html">
|
||
|
|
||
|
|
||
|
<a href="../ch6/ch6-04.html">
|
||
|
|
||
|
<i class="fa fa-check"></i>
|
||
|
|
||
|
<b>6.4.</b>
|
||
|
|
||
|
方法值和方法錶達式
|
||
|
</a>
|
||
|
|
||
|
|
||
|
</li>
|
||
|
|
||
|
<li class="chapter " data-level="6.5" data-path="ch6/ch6-05.html">
|
||
|
|
||
|
|
||
|
<a href="../ch6/ch6-05.html">
|
||
|
|
||
|
<i class="fa fa-check"></i>
|
||
|
|
||
|
<b>6.5.</b>
|
||
|
|
||
|
示例: Bit數組
|
||
|
</a>
|
||
|
|
||
|
|
||
|
</li>
|
||
|
|
||
|
<li class="chapter " data-level="6.6" data-path="ch6/ch6-06.html">
|
||
|
|
||
|
|
||
|
<a href="../ch6/ch6-06.html">
|
||
|
|
||
|
<i class="fa fa-check"></i>
|
||
|
|
||
|
<b>6.6.</b>
|
||
|
|
||
|
封裝
|
||
|
</a>
|
||
|
|
||
|
|
||
|
</li>
|
||
|
|
||
|
|
||
|
</ul>
|
||
|
|
||
|
</li>
|
||
|
|
||
|
<li class="chapter " data-level="7" data-path="ch7/ch7.html">
|
||
|
|
||
|
|
||
|
<a href="../ch7/ch7.html">
|
||
|
|
||
|
<i class="fa fa-check"></i>
|
||
|
|
||
|
<b>7.</b>
|
||
|
|
||
|
接口
|
||
|
</a>
|
||
|
|
||
|
|
||
|
<ul class="articles">
|
||
|
|
||
|
|
||
|
<li class="chapter " data-level="7.1" data-path="ch7/ch7-01.html">
|
||
|
|
||
|
|
||
|
<a href="../ch7/ch7-01.html">
|
||
|
|
||
|
<i class="fa fa-check"></i>
|
||
|
|
||
|
<b>7.1.</b>
|
||
|
|
||
|
接口是閤約
|
||
|
</a>
|
||
|
|
||
|
|
||
|
</li>
|
||
|
|
||
|
<li class="chapter " data-level="7.2" data-path="ch7/ch7-02.html">
|
||
|
|
||
|
|
||
|
<a href="../ch7/ch7-02.html">
|
||
|
|
||
|
<i class="fa fa-check"></i>
|
||
|
|
||
|
<b>7.2.</b>
|
||
|
|
||
|
接口類型
|
||
|
</a>
|
||
|
|
||
|
|
||
|
</li>
|
||
|
|
||
|
<li class="chapter " data-level="7.3" data-path="ch7/ch7-03.html">
|
||
|
|
||
|
|
||
|
<a href="../ch7/ch7-03.html">
|
||
|
|
||
|
<i class="fa fa-check"></i>
|
||
|
|
||
|
<b>7.3.</b>
|
||
|
|
||
|
實現接口的條件
|
||
|
</a>
|
||
|
|
||
|
|
||
|
</li>
|
||
|
|
||
|
<li class="chapter " data-level="7.4" data-path="ch7/ch7-04.html">
|
||
|
|
||
|
|
||
|
<a href="../ch7/ch7-04.html">
|
||
|
|
||
|
<i class="fa fa-check"></i>
|
||
|
|
||
|
<b>7.4.</b>
|
||
|
|
||
|
flag.Value接口
|
||
|
</a>
|
||
|
|
||
|
|
||
|
</li>
|
||
|
|
||
|
<li class="chapter " data-level="7.5" data-path="ch7/ch7-05.html">
|
||
|
|
||
|
|
||
|
<a href="../ch7/ch7-05.html">
|
||
|
|
||
|
<i class="fa fa-check"></i>
|
||
|
|
||
|
<b>7.5.</b>
|
||
|
|
||
|
接口值
|
||
|
</a>
|
||
|
|
||
|
|
||
|
</li>
|
||
|
|
||
|
<li class="chapter " data-level="7.6" data-path="ch7/ch7-06.html">
|
||
|
|
||
|
|
||
|
<a href="../ch7/ch7-06.html">
|
||
|
|
||
|
<i class="fa fa-check"></i>
|
||
|
|
||
|
<b>7.6.</b>
|
||
|
|
||
|
sort.Interface接口
|
||
|
</a>
|
||
|
|
||
|
|
||
|
</li>
|
||
|
|
||
|
<li class="chapter " data-level="7.7" data-path="ch7/ch7-07.html">
|
||
|
|
||
|
|
||
|
<a href="../ch7/ch7-07.html">
|
||
|
|
||
|
<i class="fa fa-check"></i>
|
||
|
|
||
|
<b>7.7.</b>
|
||
|
|
||
|
http.Handler接口
|
||
|
</a>
|
||
|
|
||
|
|
||
|
</li>
|
||
|
|
||
|
<li class="chapter " data-level="7.8" data-path="ch7/ch7-08.html">
|
||
|
|
||
|
|
||
|
<a href="../ch7/ch7-08.html">
|
||
|
|
||
|
<i class="fa fa-check"></i>
|
||
|
|
||
|
<b>7.8.</b>
|
||
|
|
||
|
error接口
|
||
|
</a>
|
||
|
|
||
|
|
||
|
</li>
|
||
|
|
||
|
<li class="chapter " data-level="7.9" data-path="ch7/ch7-09.html">
|
||
|
|
||
|
|
||
|
<a href="../ch7/ch7-09.html">
|
||
|
|
||
|
<i class="fa fa-check"></i>
|
||
|
|
||
|
<b>7.9.</b>
|
||
|
|
||
|
示例: 錶達式求值
|
||
|
</a>
|
||
|
|
||
|
|
||
|
</li>
|
||
|
|
||
|
<li class="chapter " data-level="7.10" data-path="ch7/ch7-10.html">
|
||
|
|
||
|
|
||
|
<a href="../ch7/ch7-10.html">
|
||
|
|
||
|
<i class="fa fa-check"></i>
|
||
|
|
||
|
<b>7.10.</b>
|
||
|
|
||
|
類型斷言
|
||
|
</a>
|
||
|
|
||
|
|
||
|
</li>
|
||
|
|
||
|
<li class="chapter " data-level="7.11" data-path="ch7/ch7-11.html">
|
||
|
|
||
|
|
||
|
<a href="../ch7/ch7-11.html">
|
||
|
|
||
|
<i class="fa fa-check"></i>
|
||
|
|
||
|
<b>7.11.</b>
|
||
|
|
||
|
基於類型斷言識彆錯誤類型
|
||
|
</a>
|
||
|
|
||
|
|
||
|
</li>
|
||
|
|
||
|
<li class="chapter " data-level="7.12" data-path="ch7/ch7-12.html">
|
||
|
|
||
|
|
||
|
<a href="../ch7/ch7-12.html">
|
||
|
|
||
|
<i class="fa fa-check"></i>
|
||
|
|
||
|
<b>7.12.</b>
|
||
|
|
||
|
通過類型斷言査詢接口
|
||
|
</a>
|
||
|
|
||
|
|
||
|
</li>
|
||
|
|
||
|
<li class="chapter " data-level="7.13" data-path="ch7/ch7-13.html">
|
||
|
|
||
|
|
||
|
<a href="../ch7/ch7-13.html">
|
||
|
|
||
|
<i class="fa fa-check"></i>
|
||
|
|
||
|
<b>7.13.</b>
|
||
|
|
||
|
類型分支
|
||
|
</a>
|
||
|
|
||
|
|
||
|
</li>
|
||
|
|
||
|
<li class="chapter " data-level="7.14" data-path="ch7/ch7-14.html">
|
||
|
|
||
|
|
||
|
<a href="../ch7/ch7-14.html">
|
||
|
|
||
|
<i class="fa fa-check"></i>
|
||
|
|
||
|
<b>7.14.</b>
|
||
|
|
||
|
示例: 基於標記的XML解碼
|
||
|
</a>
|
||
|
|
||
|
|
||
|
</li>
|
||
|
|
||
|
<li class="chapter " data-level="7.15" data-path="ch7/ch7-15.html">
|
||
|
|
||
|
|
||
|
<a href="../ch7/ch7-15.html">
|
||
|
|
||
|
<i class="fa fa-check"></i>
|
||
|
|
||
|
<b>7.15.</b>
|
||
|
|
||
|
補充幾點
|
||
|
</a>
|
||
|
|
||
|
|
||
|
</li>
|
||
|
|
||
|
|
||
|
</ul>
|
||
|
|
||
|
</li>
|
||
|
|
||
|
<li class="chapter " data-level="8" data-path="ch8/ch8.html">
|
||
|
|
||
|
|
||
|
<a href="../ch8/ch8.html">
|
||
|
|
||
|
<i class="fa fa-check"></i>
|
||
|
|
||
|
<b>8.</b>
|
||
|
|
||
|
Goroutines和Channels
|
||
|
</a>
|
||
|
|
||
|
|
||
|
<ul class="articles">
|
||
|
|
||
|
|
||
|
<li class="chapter " data-level="8.1" data-path="ch8/ch8-01.html">
|
||
|
|
||
|
|
||
|
<a href="../ch8/ch8-01.html">
|
||
|
|
||
|
<i class="fa fa-check"></i>
|
||
|
|
||
|
<b>8.1.</b>
|
||
|
|
||
|
Goroutines
|
||
|
</a>
|
||
|
|
||
|
|
||
|
</li>
|
||
|
|
||
|
<li class="chapter " data-level="8.2" data-path="ch8/ch8-02.html">
|
||
|
|
||
|
|
||
|
<a href="../ch8/ch8-02.html">
|
||
|
|
||
|
<i class="fa fa-check"></i>
|
||
|
|
||
|
<b>8.2.</b>
|
||
|
|
||
|
示例: 併髮的Clock服務
|
||
|
</a>
|
||
|
|
||
|
|
||
|
</li>
|
||
|
|
||
|
<li class="chapter " data-level="8.3" data-path="ch8/ch8-03.html">
|
||
|
|
||
|
|
||
|
<a href="../ch8/ch8-03.html">
|
||
|
|
||
|
<i class="fa fa-check"></i>
|
||
|
|
||
|
<b>8.3.</b>
|
||
|
|
||
|
示例: 併髮的Echo服務
|
||
|
</a>
|
||
|
|
||
|
|
||
|
</li>
|
||
|
|
||
|
<li class="chapter " data-level="8.4" data-path="ch8/ch8-04.html">
|
||
|
|
||
|
|
||
|
<a href="../ch8/ch8-04.html">
|
||
|
|
||
|
<i class="fa fa-check"></i>
|
||
|
|
||
|
<b>8.4.</b>
|
||
|
|
||
|
Channels
|
||
|
</a>
|
||
|
|
||
|
|
||
|
</li>
|
||
|
|
||
|
<li class="chapter " data-level="8.5" data-path="ch8/ch8-05.html">
|
||
|
|
||
|
|
||
|
<a href="../ch8/ch8-05.html">
|
||
|
|
||
|
<i class="fa fa-check"></i>
|
||
|
|
||
|
<b>8.5.</b>
|
||
|
|
||
|
併行的循環
|
||
|
</a>
|
||
|
|
||
|
|
||
|
</li>
|
||
|
|
||
|
<li class="chapter " data-level="8.6" data-path="ch8/ch8-06.html">
|
||
|
|
||
|
|
||
|
<a href="../ch8/ch8-06.html">
|
||
|
|
||
|
<i class="fa fa-check"></i>
|
||
|
|
||
|
<b>8.6.</b>
|
||
|
|
||
|
示例: 併髮的Web爬蟲
|
||
|
</a>
|
||
|
|
||
|
|
||
|
</li>
|
||
|
|
||
|
<li class="chapter " data-level="8.7" data-path="ch8/ch8-07.html">
|
||
|
|
||
|
|
||
|
<a href="../ch8/ch8-07.html">
|
||
|
|
||
|
<i class="fa fa-check"></i>
|
||
|
|
||
|
<b>8.7.</b>
|
||
|
|
||
|
基於select的多路復用
|
||
|
</a>
|
||
|
|
||
|
|
||
|
</li>
|
||
|
|
||
|
<li class="chapter " data-level="8.8" data-path="ch8/ch8-08.html">
|
||
|
|
||
|
|
||
|
<a href="../ch8/ch8-08.html">
|
||
|
|
||
|
<i class="fa fa-check"></i>
|
||
|
|
||
|
<b>8.8.</b>
|
||
|
|
||
|
示例: 併髮的字典遍歷
|
||
|
</a>
|
||
|
|
||
|
|
||
|
</li>
|
||
|
|
||
|
<li class="chapter " data-level="8.9" data-path="ch8/ch8-09.html">
|
||
|
|
||
|
|
||
|
<a href="../ch8/ch8-09.html">
|
||
|
|
||
|
<i class="fa fa-check"></i>
|
||
|
|
||
|
<b>8.9.</b>
|
||
|
|
||
|
併髮的退齣
|
||
|
</a>
|
||
|
|
||
|
|
||
|
</li>
|
||
|
|
||
|
<li class="chapter " data-level="8.10" data-path="ch8/ch8-10.html">
|
||
|
|
||
|
|
||
|
<a href="../ch8/ch8-10.html">
|
||
|
|
||
|
<i class="fa fa-check"></i>
|
||
|
|
||
|
<b>8.10.</b>
|
||
|
|
||
|
示例: 聊天服務
|
||
|
</a>
|
||
|
|
||
|
|
||
|
</li>
|
||
|
|
||
|
|
||
|
</ul>
|
||
|
|
||
|
</li>
|
||
|
|
||
|
<li class="chapter " data-level="9" data-path="ch9/ch9.html">
|
||
|
|
||
|
|
||
|
<a href="../ch9/ch9.html">
|
||
|
|
||
|
<i class="fa fa-check"></i>
|
||
|
|
||
|
<b>9.</b>
|
||
|
|
||
|
基於共享變量的併髮
|
||
|
</a>
|
||
|
|
||
|
|
||
|
<ul class="articles">
|
||
|
|
||
|
|
||
|
<li class="chapter " data-level="9.1" data-path="ch9/ch9-01.html">
|
||
|
|
||
|
|
||
|
<a href="../ch9/ch9-01.html">
|
||
|
|
||
|
<i class="fa fa-check"></i>
|
||
|
|
||
|
<b>9.1.</b>
|
||
|
|
||
|
競爭條件
|
||
|
</a>
|
||
|
|
||
|
|
||
|
</li>
|
||
|
|
||
|
<li class="chapter " data-level="9.2" data-path="ch9/ch9-02.html">
|
||
|
|
||
|
|
||
|
<a href="../ch9/ch9-02.html">
|
||
|
|
||
|
<i class="fa fa-check"></i>
|
||
|
|
||
|
<b>9.2.</b>
|
||
|
|
||
|
sync.Mutex互斥鎖
|
||
|
</a>
|
||
|
|
||
|
|
||
|
</li>
|
||
|
|
||
|
<li class="chapter " data-level="9.3" data-path="ch9/ch9-03.html">
|
||
|
|
||
|
|
||
|
<a href="../ch9/ch9-03.html">
|
||
|
|
||
|
<i class="fa fa-check"></i>
|
||
|
|
||
|
<b>9.3.</b>
|
||
|
|
||
|
sync.RWMutex讀寫鎖
|
||
|
</a>
|
||
|
|
||
|
|
||
|
</li>
|
||
|
|
||
|
<li class="chapter " data-level="9.4" data-path="ch9/ch9-04.html">
|
||
|
|
||
|
|
||
|
<a href="../ch9/ch9-04.html">
|
||
|
|
||
|
<i class="fa fa-check"></i>
|
||
|
|
||
|
<b>9.4.</b>
|
||
|
|
||
|
內存衕步
|
||
|
</a>
|
||
|
|
||
|
|
||
|
</li>
|
||
|
|
||
|
<li class="chapter " data-level="9.5" data-path="ch9/ch9-05.html">
|
||
|
|
||
|
|
||
|
<a href="../ch9/ch9-05.html">
|
||
|
|
||
|
<i class="fa fa-check"></i>
|
||
|
|
||
|
<b>9.5.</b>
|
||
|
|
||
|
sync.Once初始化
|
||
|
</a>
|
||
|
|
||
|
|
||
|
</li>
|
||
|
|
||
|
<li class="chapter " data-level="9.6" data-path="ch9/ch9-06.html">
|
||
|
|
||
|
|
||
|
<a href="../ch9/ch9-06.html">
|
||
|
|
||
|
<i class="fa fa-check"></i>
|
||
|
|
||
|
<b>9.6.</b>
|
||
|
|
||
|
競爭條件檢測
|
||
|
</a>
|
||
|
|
||
|
|
||
|
</li>
|
||
|
|
||
|
<li class="chapter " data-level="9.7" data-path="ch9/ch9-07.html">
|
||
|
|
||
|
|
||
|
<a href="../ch9/ch9-07.html">
|
||
|
|
||
|
<i class="fa fa-check"></i>
|
||
|
|
||
|
<b>9.7.</b>
|
||
|
|
||
|
示例: 併髮的非阻塞緩存
|
||
|
</a>
|
||
|
|
||
|
|
||
|
</li>
|
||
|
|
||
|
<li class="chapter " data-level="9.8" data-path="ch9/ch9-08.html">
|
||
|
|
||
|
|
||
|
<a href="../ch9/ch9-08.html">
|
||
|
|
||
|
<i class="fa fa-check"></i>
|
||
|
|
||
|
<b>9.8.</b>
|
||
|
|
||
|
Goroutines和綫程
|
||
|
</a>
|
||
|
|
||
|
|
||
|
</li>
|
||
|
|
||
|
|
||
|
</ul>
|
||
|
|
||
|
</li>
|
||
|
|
||
|
<li class="chapter " data-level="10" data-path="ch10/ch10.html">
|
||
|
|
||
|
|
||
|
<a href="../ch10/ch10.html">
|
||
|
|
||
|
<i class="fa fa-check"></i>
|
||
|
|
||
|
<b>10.</b>
|
||
|
|
||
|
包和工具
|
||
|
</a>
|
||
|
|
||
|
|
||
|
<ul class="articles">
|
||
|
|
||
|
|
||
|
<li class="chapter " data-level="10.1" data-path="ch10/ch10-01.html">
|
||
|
|
||
|
|
||
|
<a href="../ch10/ch10-01.html">
|
||
|
|
||
|
<i class="fa fa-check"></i>
|
||
|
|
||
|
<b>10.1.</b>
|
||
|
|
||
|
簡介
|
||
|
</a>
|
||
|
|
||
|
|
||
|
</li>
|
||
|
|
||
|
<li class="chapter " data-level="10.2" data-path="ch10/ch10-02.html">
|
||
|
|
||
|
|
||
|
<a href="../ch10/ch10-02.html">
|
||
|
|
||
|
<i class="fa fa-check"></i>
|
||
|
|
||
|
<b>10.2.</b>
|
||
|
|
||
|
導入路徑
|
||
|
</a>
|
||
|
|
||
|
|
||
|
</li>
|
||
|
|
||
|
<li class="chapter " data-level="10.3" data-path="ch10/ch10-03.html">
|
||
|
|
||
|
|
||
|
<a href="../ch10/ch10-03.html">
|
||
|
|
||
|
<i class="fa fa-check"></i>
|
||
|
|
||
|
<b>10.3.</b>
|
||
|
|
||
|
包聲明
|
||
|
</a>
|
||
|
|
||
|
|
||
|
</li>
|
||
|
|
||
|
<li class="chapter " data-level="10.4" data-path="ch10/ch10-04.html">
|
||
|
|
||
|
|
||
|
<a href="../ch10/ch10-04.html">
|
||
|
|
||
|
<i class="fa fa-check"></i>
|
||
|
|
||
|
<b>10.4.</b>
|
||
|
|
||
|
導入聲明
|
||
|
</a>
|
||
|
|
||
|
|
||
|
</li>
|
||
|
|
||
|
<li class="chapter " data-level="10.5" data-path="ch10/ch10-05.html">
|
||
|
|
||
|
|
||
|
<a href="../ch10/ch10-05.html">
|
||
|
|
||
|
<i class="fa fa-check"></i>
|
||
|
|
||
|
<b>10.5.</b>
|
||
|
|
||
|
匿名導入
|
||
|
</a>
|
||
|
|
||
|
|
||
|
</li>
|
||
|
|
||
|
<li class="chapter " data-level="10.6" data-path="ch10/ch10-06.html">
|
||
|
|
||
|
|
||
|
<a href="../ch10/ch10-06.html">
|
||
|
|
||
|
<i class="fa fa-check"></i>
|
||
|
|
||
|
<b>10.6.</b>
|
||
|
|
||
|
包和命名
|
||
|
</a>
|
||
|
|
||
|
|
||
|
</li>
|
||
|
|
||
|
<li class="chapter " data-level="10.7" data-path="ch10/ch10-07.html">
|
||
|
|
||
|
|
||
|
<a href="../ch10/ch10-07.html">
|
||
|
|
||
|
<i class="fa fa-check"></i>
|
||
|
|
||
|
<b>10.7.</b>
|
||
|
|
||
|
工具
|
||
|
</a>
|
||
|
|
||
|
|
||
|
</li>
|
||
|
|
||
|
|
||
|
</ul>
|
||
|
|
||
|
</li>
|
||
|
|
||
|
<li class="chapter " data-level="11" data-path="ch11/ch11.html">
|
||
|
|
||
|
|
||
|
<a href="../ch11/ch11.html">
|
||
|
|
||
|
<i class="fa fa-check"></i>
|
||
|
|
||
|
<b>11.</b>
|
||
|
|
||
|
測試
|
||
|
</a>
|
||
|
|
||
|
|
||
|
<ul class="articles">
|
||
|
|
||
|
|
||
|
<li class="chapter " data-level="11.1" data-path="ch11/ch11-01.html">
|
||
|
|
||
|
|
||
|
<a href="../ch11/ch11-01.html">
|
||
|
|
||
|
<i class="fa fa-check"></i>
|
||
|
|
||
|
<b>11.1.</b>
|
||
|
|
||
|
go test
|
||
|
</a>
|
||
|
|
||
|
|
||
|
</li>
|
||
|
|
||
|
<li class="chapter " data-level="11.2" data-path="ch11/ch11-02.html">
|
||
|
|
||
|
|
||
|
<a href="../ch11/ch11-02.html">
|
||
|
|
||
|
<i class="fa fa-check"></i>
|
||
|
|
||
|
<b>11.2.</b>
|
||
|
|
||
|
測試函數
|
||
|
</a>
|
||
|
|
||
|
|
||
|
</li>
|
||
|
|
||
|
<li class="chapter " data-level="11.3" data-path="ch11/ch11-03.html">
|
||
|
|
||
|
|
||
|
<a href="../ch11/ch11-03.html">
|
||
|
|
||
|
<i class="fa fa-check"></i>
|
||
|
|
||
|
<b>11.3.</b>
|
||
|
|
||
|
測試覆蓋率
|
||
|
</a>
|
||
|
|
||
|
|
||
|
</li>
|
||
|
|
||
|
<li class="chapter " data-level="11.4" data-path="ch11/ch11-04.html">
|
||
|
|
||
|
|
||
|
<a href="../ch11/ch11-04.html">
|
||
|
|
||
|
<i class="fa fa-check"></i>
|
||
|
|
||
|
<b>11.4.</b>
|
||
|
|
||
|
基準測試
|
||
|
</a>
|
||
|
|
||
|
|
||
|
</li>
|
||
|
|
||
|
<li class="chapter " data-level="11.5" data-path="ch11/ch11-05.html">
|
||
|
|
||
|
|
||
|
<a href="../ch11/ch11-05.html">
|
||
|
|
||
|
<i class="fa fa-check"></i>
|
||
|
|
||
|
<b>11.5.</b>
|
||
|
|
||
|
剖析
|
||
|
</a>
|
||
|
|
||
|
|
||
|
</li>
|
||
|
|
||
|
<li class="chapter " data-level="11.6" data-path="ch11/ch11-06.html">
|
||
|
|
||
|
|
||
|
<a href="../ch11/ch11-06.html">
|
||
|
|
||
|
<i class="fa fa-check"></i>
|
||
|
|
||
|
<b>11.6.</b>
|
||
|
|
||
|
示例函數
|
||
|
</a>
|
||
|
|
||
|
|
||
|
</li>
|
||
|
|
||
|
|
||
|
</ul>
|
||
|
|
||
|
</li>
|
||
|
|
||
|
<li class="chapter " data-level="12" data-path="ch12/ch12.html">
|
||
|
|
||
|
|
||
|
<a href="../ch12/ch12.html">
|
||
|
|
||
|
<i class="fa fa-check"></i>
|
||
|
|
||
|
<b>12.</b>
|
||
|
|
||
|
反射
|
||
|
</a>
|
||
|
|
||
|
|
||
|
<ul class="articles">
|
||
|
|
||
|
|
||
|
<li class="chapter " data-level="12.1" data-path="ch12/ch12-01.html">
|
||
|
|
||
|
|
||
|
<a href="../ch12/ch12-01.html">
|
||
|
|
||
|
<i class="fa fa-check"></i>
|
||
|
|
||
|
<b>12.1.</b>
|
||
|
|
||
|
為何需要反射?
|
||
|
</a>
|
||
|
|
||
|
|
||
|
</li>
|
||
|
|
||
|
<li class="chapter " data-level="12.2" data-path="ch12/ch12-02.html">
|
||
|
|
||
|
|
||
|
<a href="../ch12/ch12-02.html">
|
||
|
|
||
|
<i class="fa fa-check"></i>
|
||
|
|
||
|
<b>12.2.</b>
|
||
|
|
||
|
reflect.Type和reflect.Value
|
||
|
</a>
|
||
|
|
||
|
|
||
|
</li>
|
||
|
|
||
|
<li class="chapter " data-level="12.3" data-path="ch12/ch12-03.html">
|
||
|
|
||
|
|
||
|
<a href="../ch12/ch12-03.html">
|
||
|
|
||
|
<i class="fa fa-check"></i>
|
||
|
|
||
|
<b>12.3.</b>
|
||
|
|
||
|
Display遞歸打印
|
||
|
</a>
|
||
|
|
||
|
|
||
|
</li>
|
||
|
|
||
|
<li class="chapter " data-level="12.4" data-path="ch12/ch12-04.html">
|
||
|
|
||
|
|
||
|
<a href="../ch12/ch12-04.html">
|
||
|
|
||
|
<i class="fa fa-check"></i>
|
||
|
|
||
|
<b>12.4.</b>
|
||
|
|
||
|
示例: 編碼S錶達式
|
||
|
</a>
|
||
|
|
||
|
|
||
|
</li>
|
||
|
|
||
|
<li class="chapter " data-level="12.5" data-path="ch12/ch12-05.html">
|
||
|
|
||
|
|
||
|
<a href="../ch12/ch12-05.html">
|
||
|
|
||
|
<i class="fa fa-check"></i>
|
||
|
|
||
|
<b>12.5.</b>
|
||
|
|
||
|
通過reflect.Value脩改值
|
||
|
</a>
|
||
|
|
||
|
|
||
|
</li>
|
||
|
|
||
|
<li class="chapter " data-level="12.6" data-path="ch12/ch12-06.html">
|
||
|
|
||
|
|
||
|
<a href="../ch12/ch12-06.html">
|
||
|
|
||
|
<i class="fa fa-check"></i>
|
||
|
|
||
|
<b>12.6.</b>
|
||
|
|
||
|
示例: 解碼S錶達式
|
||
|
</a>
|
||
|
|
||
|
|
||
|
</li>
|
||
|
|
||
|
<li class="chapter " data-level="12.7" data-path="ch12/ch12-07.html">
|
||
|
|
||
|
|
||
|
<a href="../ch12/ch12-07.html">
|
||
|
|
||
|
<i class="fa fa-check"></i>
|
||
|
|
||
|
<b>12.7.</b>
|
||
|
|
||
|
穫取結構體字段標識
|
||
|
</a>
|
||
|
|
||
|
|
||
|
</li>
|
||
|
|
||
|
<li class="chapter " data-level="12.8" data-path="ch12/ch12-08.html">
|
||
|
|
||
|
|
||
|
<a href="../ch12/ch12-08.html">
|
||
|
|
||
|
<i class="fa fa-check"></i>
|
||
|
|
||
|
<b>12.8.</b>
|
||
|
|
||
|
顯示一個類型的方法集
|
||
|
</a>
|
||
|
|
||
|
|
||
|
</li>
|
||
|
|
||
|
<li class="chapter " data-level="12.9" data-path="ch12/ch12-09.html">
|
||
|
|
||
|
|
||
|
<a href="../ch12/ch12-09.html">
|
||
|
|
||
|
<i class="fa fa-check"></i>
|
||
|
|
||
|
<b>12.9.</b>
|
||
|
|
||
|
幾點忠告
|
||
|
</a>
|
||
|
|
||
|
|
||
|
</li>
|
||
|
|
||
|
|
||
|
</ul>
|
||
|
|
||
|
</li>
|
||
|
|
||
|
<li class="chapter " data-level="13" data-path="ch13/ch13.html">
|
||
|
|
||
|
|
||
|
<a href="../ch13/ch13.html">
|
||
|
|
||
|
<i class="fa fa-check"></i>
|
||
|
|
||
|
<b>13.</b>
|
||
|
|
||
|
底層編程
|
||
|
</a>
|
||
|
|
||
|
|
||
|
<ul class="articles">
|
||
|
|
||
|
|
||
|
<li class="chapter " data-level="13.1" data-path="ch13/ch13-01.html">
|
||
|
|
||
|
|
||
|
<a href="../ch13/ch13-01.html">
|
||
|
|
||
|
<i class="fa fa-check"></i>
|
||
|
|
||
|
<b>13.1.</b>
|
||
|
|
||
|
unsafe.Sizeof, Alignof 和 Offsetof
|
||
|
</a>
|
||
|
|
||
|
|
||
|
</li>
|
||
|
|
||
|
<li class="chapter " data-level="13.2" data-path="ch13/ch13-02.html">
|
||
|
|
||
|
|
||
|
<a href="../ch13/ch13-02.html">
|
||
|
|
||
|
<i class="fa fa-check"></i>
|
||
|
|
||
|
<b>13.2.</b>
|
||
|
|
||
|
unsafe.Pointer
|
||
|
</a>
|
||
|
|
||
|
|
||
|
</li>
|
||
|
|
||
|
<li class="chapter " data-level="13.3" data-path="ch13/ch13-03.html">
|
||
|
|
||
|
|
||
|
<a href="../ch13/ch13-03.html">
|
||
|
|
||
|
<i class="fa fa-check"></i>
|
||
|
|
||
|
<b>13.3.</b>
|
||
|
|
||
|
示例: 深度相等判斷
|
||
|
</a>
|
||
|
|
||
|
|
||
|
</li>
|
||
|
|
||
|
<li class="chapter " data-level="13.4" data-path="ch13/ch13-04.html">
|
||
|
|
||
|
|
||
|
<a href="../ch13/ch13-04.html">
|
||
|
|
||
|
<i class="fa fa-check"></i>
|
||
|
|
||
|
<b>13.4.</b>
|
||
|
|
||
|
通過cgo調用C代碼
|
||
|
</a>
|
||
|
|
||
|
|
||
|
</li>
|
||
|
|
||
|
<li class="chapter " data-level="13.5" data-path="ch13/ch13-05.html">
|
||
|
|
||
|
|
||
|
<a href="../ch13/ch13-05.html">
|
||
|
|
||
|
<i class="fa fa-check"></i>
|
||
|
|
||
|
<b>13.5.</b>
|
||
|
|
||
|
幾點忠告
|
||
|
</a>
|
||
|
|
||
|
|
||
|
</li>
|
||
|
|
||
|
|
||
|
</ul>
|
||
|
|
||
|
</li>
|
||
|
|
||
|
<li class="chapter " data-level="14" data-path="exercise/ex.html">
|
||
|
|
||
|
|
||
|
<a href="../exercise/ex.html">
|
||
|
|
||
|
<i class="fa fa-check"></i>
|
||
|
|
||
|
<b>14.</b>
|
||
|
|
||
|
習題解答
|
||
|
</a>
|
||
|
|
||
|
|
||
|
<ul class="articles">
|
||
|
|
||
|
|
||
|
<li class="chapter " data-level="14.1" data-path="exercise/ex-ch1.html">
|
||
|
|
||
|
|
||
|
<a href="../exercise/ex-ch1.html">
|
||
|
|
||
|
<i class="fa fa-check"></i>
|
||
|
|
||
|
<b>14.1.</b>
|
||
|
|
||
|
第一章 入門
|
||
|
</a>
|
||
|
|
||
|
|
||
|
</li>
|
||
|
|
||
|
|
||
|
</ul>
|
||
|
|
||
|
</li>
|
||
|
|
||
|
<li class="chapter " data-level="15" data-path="errata.html">
|
||
|
|
||
|
|
||
|
<a href="../errata.html">
|
||
|
|
||
|
<i class="fa fa-check"></i>
|
||
|
|
||
|
<b>15.</b>
|
||
|
|
||
|
勘誤
|
||
|
</a>
|
||
|
|
||
|
|
||
|
</li>
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
<li class="divider"></li>
|
||
|
<li>
|
||
|
<a href="https://www.gitbook.com" target="blank" class="gitbook-link">
|
||
|
本書使用 GitBook 釋出
|
||
|
</a>
|
||
|
</li>
|
||
|
|
||
|
</ul>
|
||
|
</nav>
|
||
|
</div>
|
||
|
|
||
|
<div class="book-body">
|
||
|
<div class="body-inner">
|
||
|
<div class="book-header" role="navigation">
|
||
|
<!-- Actions Left -->
|
||
|
|
||
|
|
||
|
<!-- Title -->
|
||
|
<h1>
|
||
|
<i class="fa fa-circle-o-notch fa-spin"></i>
|
||
|
<a href="../" >Go编程语言</a>
|
||
|
</h1>
|
||
|
</div>
|
||
|
|
||
|
<div class="page-wrapper" tabindex="-1" role="main">
|
||
|
<div class="page-inner">
|
||
|
|
||
|
|
||
|
<section class="normal" id="section-">
|
||
|
|
||
|
<h2 id="23-變量">2.3. 變量</h2>
|
||
|
<p>var 聲明可以創建一個特定類型的變量, 然後給變量附加一個名字, 併且設置變量的初始值. 變量聲明的一般語法:</p>
|
||
|
<pre><code class="lang-Go"><span class="hljs-keyword">var</span> name <span class="hljs-keyword">type</span> = 錶達式
|
||
|
</code></pre>
|
||
|
<p>其中類型或 <code>= 錶達式</code> 可以省略其中的一個. 如果省略的是類型信息, 那麼將根據初始化錶達式類推導類型信息. 如果初始化錶達式被省略, 那麼將用零值初始化變量. 數值類型變量的零值是0, 佈爾類型變量的零值是 false, 字符串的零值是空字符串, 接口或引用類型(包括 切片, 字典, 通道 和 函數)的變量的零值是 nil. 數組或結構體等聚閤類型的零值是每個元素或字段都是零值.</p>
|
||
|
<p>零值機製可以確保每個聲明的變量總是有一個良好定義的值, 在 Go 中不存在未初始化的變量. 這個可以簡化很多代碼, 在沒有增加額外工作的前提下確保邊界條件下的閤理行為. 例如:</p>
|
||
|
<pre><code class="lang-Go"><span class="hljs-keyword">var</span> s <span class="hljs-typename">string</span>
|
||
|
fmt.Println(s) <span class="hljs-comment">// ""</span>
|
||
|
</code></pre>
|
||
|
<p>這段代碼將打印一個空字符串, 而不是導緻錯誤或產生不可預知的行為. Go 程序員經常讓一些聚閤類型的零值也有意義, 這樣不管任何類型的變量總是有一個閤理的零值狀態.</p>
|
||
|
<p>可以在一個聲明語句中衕時聲明一組變量, 或用一組初始化錶達式聲明併初始化一組變量.
|
||
|
如果省略每個變量的類型, 將可以聲明多個不衕類型的變量(類型由初始化錶達式推導):</p>
|
||
|
<pre><code class="lang-Go"><span class="hljs-keyword">var</span> i, j, k <span class="hljs-typename">int</span> <span class="hljs-comment">// int, int, int</span>
|
||
|
<span class="hljs-keyword">var</span> b, f, s = <span class="hljs-constant">true</span>, <span class="hljs-number">2.3</span>, <span class="hljs-string">"four"</span> <span class="hljs-comment">// bool, float64, string</span>
|
||
|
</code></pre>
|
||
|
<p>初始化可以是字麫量或任意的錶達式. 包級彆聲明的變量會在 main 函數執行前完成初始化 (§2.6.2), 侷部變量將在聲明語句被執行到的時候初始化.</p>
|
||
|
<p>一組變量的初始化也可以通過調用一個函數, 由函數返迴的多個返迴值初始化:</p>
|
||
|
<pre><code class="lang-Go"><span class="hljs-keyword">var</span> f, err = os.Open(name) <span class="hljs-comment">// os.Open returns a file and an error</span>
|
||
|
</code></pre>
|
||
|
<h3 id="231-簡短變量聲明">2.3.1. 簡短變量聲明</h3>
|
||
|
<p>在函數內部, 有一種稱為簡短變量聲明的形式可用於聲明和初始化侷部變量. 以 <code>名字 := 錶達式</code> 方式聲明變量, 變量的類型根據錶達式來推導. 這裏函數中是三個簡短變量聲明語句(§1.4):</p>
|
||
|
<pre><code class="lang-Go">anim := gif.GIF{LoopCount: nframes}
|
||
|
freq := rand.Float64() * <span class="hljs-number">3.0</span>
|
||
|
t := <span class="hljs-number">0.0</span>
|
||
|
</code></pre>
|
||
|
<p>因為簡潔和靈活性, 簡短變量聲明用於大部分的侷部變量的聲明和初始化. var 方式的聲明往往是用於需要顯示指定類型的侷部變量, 或者因為稍後會被賦值而初始值無關緊要的變量.</p>
|
||
|
<pre><code class="lang-Go">i := <span class="hljs-number">100</span> <span class="hljs-comment">// an int</span>
|
||
|
<span class="hljs-keyword">var</span> boiling <span class="hljs-typename">float64</span> = <span class="hljs-number">100</span> <span class="hljs-comment">// a float64</span>
|
||
|
<span class="hljs-keyword">var</span> names []<span class="hljs-typename">string</span>
|
||
|
<span class="hljs-keyword">var</span> err error
|
||
|
<span class="hljs-keyword">var</span> p Point
|
||
|
</code></pre>
|
||
|
<p>於 var 聲明變量一樣, 簡短變量聲明也可以用來聲明和初始化一組變量:</p>
|
||
|
<pre><code class="lang-Go">i, j := <span class="hljs-number">0</span>, <span class="hljs-number">1</span>
|
||
|
</code></pre>
|
||
|
<p>但是這種聲明多個變量的方式隻簡易在可以提高代碼可讀性的地方使用, 比如 for 循環的初始化部分.</p>
|
||
|
<p>請記住 <code>:=</code> 是一個變量聲明, 而 <code>=</code> 是一個賦值操作. 不要混淆多個變量的聲明和元組的多重(§2.4.1), 後者是將右邊的錶達式值賦給左邊對應位置的變量:</p>
|
||
|
<pre><code class="lang-Go">i, j = j, i <span class="hljs-comment">// 交換 i 和 j 的值</span>
|
||
|
</code></pre>
|
||
|
<p>和普通 var 變量聲明一樣, 簡短變量聲明也可以用調用函數的返迴值來聲明, 像 os.Open 函數返迴兩個值:</p>
|
||
|
<pre><code class="lang-Go">f, err := os.Open(name)
|
||
|
<span class="hljs-keyword">if</span> err != <span class="hljs-constant">nil</span> {
|
||
|
<span class="hljs-keyword">return</span> err
|
||
|
}
|
||
|
<span class="hljs-comment">// ...use f...</span>
|
||
|
f.Close()
|
||
|
</code></pre>
|
||
|
<p>這裏有一個比較微妙的地方: 簡短變量聲明左邊的全部變量可能併不是全部都是剛剛聲明的. 如果有一些已經在相衕的詞法塊聲明過了(§2.7), 那麼簡短變量聲明對這些已經聲明過的變量就隻有賦值行為了.</p>
|
||
|
<p>在下麫的代碼中, 第一個語句聲明了 in 和 err 變量. 第二個語句隻聲明了 out, 然後對已經聲明的 err 進行賦值.</p>
|
||
|
<pre><code class="lang-Go">in, err := os.Open(infile)
|
||
|
<span class="hljs-comment">// ...</span>
|
||
|
out, err := os.Create(outfile)
|
||
|
</code></pre>
|
||
|
<p>簡短變量聲明必鬚至少聲明一個新的變量, 否則編譯將不能通過:</p>
|
||
|
<pre><code class="lang-Go">f, err := os.Open(infile)
|
||
|
<span class="hljs-comment">// ...</span>
|
||
|
f, err := os.Create(outfile) <span class="hljs-comment">// compile error: no new variables</span>
|
||
|
</code></pre>
|
||
|
<p>解決的方法是第二個語句改用普通的賦值語言.</p>
|
||
|
<p>簡短變量聲明隻有對在變量已經在衕級詞法域聲明過的變量纔和賦值操作等衕, 如果變量是在外部詞法域聲明了, 那麼將會聲明一個新變量. 我們在本章後麫將會看到類似的例子.</p>
|
||
|
<h3 id="232-指鍼">2.3.2 指鍼</h3>
|
||
|
<p>一個變量對應一個保存了一個值的內存空間. 變量在聲明語句創建時綁定一個名字, 比如 x, 但是還有很多變量始終以錶達式方式引入, 例如 x[i] 或 x.f. 所有這些錶達式都讀取一個變量的值, 除非它們是齣現在賦值語句的左邊, 這種時候是給變量賦予一個新值.</p>
|
||
|
<p>一個指鍼的值是一個變量的地址. 一個指鍼對應變量在內存中的存儲位置. 併不是每一個值都會有一個地址, 但是對於每一個變量必然有對應的地址. 通過指鍼, 我們可以直接讀或更新變量的值, 而不需要知道變量的名字(卽使變量有名字的話).</p>
|
||
|
<p>如果這樣聲明一個變量 <code>var x int</code>, 那麼 <code>&x</code> 錶達式(x的地址)將產生一個指曏整數變量的指鍼, 對應的數據類型是 <code>*int</code>, 稱之為 "指曏 int 的指鍼". 如果指鍼名字為 p, 那麼可以說 "p 指鍼指曏 x", 或者說 "p 指鍼保存了 x 變量的地址". <code>*p</code> 對應 p 指鍼指曏的變量的值. <code>*p</code> 錶達式讀取變量的值, 為 int 類型, 衕時因為 <code>*p</code> 對應一個變量, 所以可以齣現在賦值語句的左邊, 用於更新所指曏的變量的值.</p>
|
||
|
<pre><code class="lang-Go">x := <span class="hljs-number">1</span>
|
||
|
p := &x <span class="hljs-comment">// p, of type *int, points to x</span>
|
||
|
fmt.Println(*p) <span class="hljs-comment">// "1"</span>
|
||
|
*p = <span class="hljs-number">2</span> <span class="hljs-comment">// equivalent to x = 2</span>
|
||
|
fmt.Println(x) <span class="hljs-comment">// "2"</span>
|
||
|
</code></pre>
|
||
|
<p>對於聚閤類型, 比如結構體的每個字段, 或者是數組的每個元素, 也都是對應一個變量, 併且可以被穫取地址.</p>
|
||
|
<p>變量有時候被稱為可尋址的值. 如果變量由錶達式臨時生成, 那麼錶達式必鬚能接受 <code>&</code> 取地址操作.</p>
|
||
|
<p>任何類型的指鍼的零值都是 nil. 如果 <code>p != nil</code> 測試為眞, 那麼 p 是指曏變量. 指鍼直接也是可以進行相等測試的, 隻有當它們指曏衕一個變量或全部是 nil 時纔相等.</p>
|
||
|
<pre><code class="lang-Go"><span class="hljs-keyword">var</span> x, y <span class="hljs-typename">int</span>
|
||
|
fmt.Println(&x == &x, &x == &y, &x == <span class="hljs-constant">nil</span>) <span class="hljs-comment">// "true false false"</span>
|
||
|
</code></pre>
|
||
|
<p>在Go語言中, 返迴函數中侷部變量的地址是安全的. 例如下麫的代碼, 調用 f 函數時創建 v 侷部變量, 在地址被返迴之後依然有效, 因為指鍼 p 依然引用這個變量.</p>
|
||
|
<pre><code class="lang-Go"><span class="hljs-keyword">var</span> p = f()
|
||
|
|
||
|
<span class="hljs-keyword">func</span> f() *<span class="hljs-typename">int</span> {
|
||
|
v := <span class="hljs-number">1</span>
|
||
|
<span class="hljs-keyword">return</span> &v
|
||
|
}
|
||
|
</code></pre>
|
||
|
<p>每次調用 f 函數都將返迴不衕的結果:</p>
|
||
|
<pre><code class="lang-Go">fmt.Println(f() == f()) <span class="hljs-comment">// "false"</span>
|
||
|
</code></pre>
|
||
|
<p>因為指鍼包含了一個變量的地址, 因此將指鍼作為參數調用函數, 將可以在函數中通過指鍼更新變量的值. 例如這個通過指鍼來更新變量的值, 然後返迴更新後的值, 可用在一個錶達式中:</p>
|
||
|
<pre><code class="lang-Go"><span class="hljs-keyword">func</span> incr(p *<span class="hljs-typename">int</span>) <span class="hljs-typename">int</span> {
|
||
|
*p++ <span class="hljs-comment">// increments what p points to; does not change p</span>
|
||
|
<span class="hljs-keyword">return</span> *p
|
||
|
}
|
||
|
|
||
|
v := <span class="hljs-number">1</span>
|
||
|
incr(&v) <span class="hljs-comment">// side effect: v is now 2</span>
|
||
|
fmt.Println(incr(&v)) <span class="hljs-comment">// "3" (and v is 3)</span>
|
||
|
</code></pre>
|
||
|
<p>每次我們對變量取地址, 或者復製指鍼, 我們都創建了變量的新的彆名. 例如, *p 是 變量 v 的彆名. 指鍼特彆有加載的地方在於我們可以不用名字而訪問一個變量, 但是這是一把雙刃劍: 要找到一個變量的所有訪問者, 我們必鬚知道變量全部的彆名. 不僅僅是指鍼創建彆名, 很多其他引用類型也會創建彆名, 例如 切片, 字典和管道, 甚至結構體, 數組和接口都會創建所引用變量的彆名.</p>
|
||
|
<p>指鍼是 flag 包的關鍵, 它使用命令行參數來設置對應的變量, 而這些分佈在整個程序中. 為了說明這一點, 在早些的echo版本中, 包含了兩個可選的命令行參數: <code>-n</code> 用於忽略行尾的換行符, <code>-s sep</code> 用於指定分隔字符(默認是空格). 這是第四個版本, 對應包 gopl.io/ch2/echo4.</p>
|
||
|
<pre><code class="lang-Go">gopl.io/ch2/echo4
|
||
|
<span class="hljs-comment">// Echo4 prints its command-line arguments.</span>
|
||
|
<span class="hljs-keyword">package</span> main
|
||
|
|
||
|
<span class="hljs-keyword">import</span> (
|
||
|
<span class="hljs-string">"flag"</span>
|
||
|
<span class="hljs-string">"fmt"</span>
|
||
|
<span class="hljs-string">"strings"</span>
|
||
|
)
|
||
|
|
||
|
<span class="hljs-keyword">var</span> n = flag.Bool(<span class="hljs-string">"n"</span>, <span class="hljs-constant">false</span>, <span class="hljs-string">"omit trailing newline"</span>)
|
||
|
<span class="hljs-keyword">var</span> sep = flag.String(<span class="hljs-string">"s"</span>, <span class="hljs-string">" "</span>, <span class="hljs-string">"separator"</span>)
|
||
|
|
||
|
<span class="hljs-keyword">func</span> main() {
|
||
|
flag.Parse()
|
||
|
fmt.Print(strings.Join(flag.Args(), *sep))
|
||
|
<span class="hljs-keyword">if</span> !*n {
|
||
|
fmt.Println()
|
||
|
}
|
||
|
}
|
||
|
</code></pre>
|
||
|
<p><code>flag.Bool</code> 函數調用創建了一個新的佈爾型標誌參數變量. 它有三個屬性: 第一個是的名字"n", 然後是標誌的默認值(這裏是false), 最後是對應的描述信息. 如果用戶輸入了無效的標誌參數, 或者輸入 <code>-h</code> 或 <code>-help</code> 標誌參數, 將打印標誌參數的名字, 默認值和描述信息. 類似的, flag.String 用於創建一個字符串類型的標誌參數變量, 衕樣包含參數名, 默認值, 和描述信息. 變量 <code>sep</code> 和 <code>n</code> 是一個指曏標誌參數變量的指鍼, 因此必鬚用 <em>sep 和 </em>n 的方式間接引用.</p>
|
||
|
<p>當程序運行時, 必鬚在標誌參數變量使用之前調用 flag.Parse 函數更新標誌參數變量的值(之前是默認值). 非標誌參數的普通類型參數可以用 flag.Args() 訪問, 對應一個 字符串切片. 如果 flag.Parse 解析遇到錯誤, 將打印提示信息, 然後調用 os.Exit(2) 終止程序.</p>
|
||
|
<p>讓我們運行一些 echo 測試用例:</p>
|
||
|
<pre><code>$ go build gopl.io/ch2/echo4
|
||
|
$ ./echo4 a bc def
|
||
|
a bc def
|
||
|
$ ./echo4 -s / a bc def
|
||
|
a/bc/def
|
||
|
$ ./echo4 -n a bc def
|
||
|
a bc def$
|
||
|
$ ./echo4 -help
|
||
|
Usage of ./echo4:
|
||
|
-n omit trailing newline
|
||
|
-s string
|
||
|
separator (default " ")
|
||
|
</code></pre><h3 id="233-new-函數">2.3.3 new 函數</h3>
|
||
|
<p>另一個創建變量的方法是用內建的 new 函數. 錶達式 <code>new(T)</code> 創建一個T類型的匿名變量, 初始化為T類型的零值, 返迴返迴變量地址, 返迴指鍼類型為 <code>*T</code>.</p>
|
||
|
<pre><code class="lang-Go">p := <span class="hljs-built_in">new</span>(<span class="hljs-typename">int</span>) <span class="hljs-comment">// p, *int 類型, 指曏匿名的 int 變量</span>
|
||
|
fmt.Println(*p) <span class="hljs-comment">// "0"</span>
|
||
|
*p = <span class="hljs-number">2</span> <span class="hljs-comment">// 設置 int 匿名變量的值為 2</span>
|
||
|
fmt.Println(*p) <span class="hljs-comment">// "2"</span>
|
||
|
</code></pre>
|
||
|
<p>從 new 創建變量和普通聲明方式創建變量沒有什麼區彆, 除了不需要聲明一個臨時變量的名字外, 我們還可以在錶達式中使用 <code>new(T)</code>. 換言之, new 類似是一種語法醣, 而不是一個新的基礎概唸.</p>
|
||
|
<p>下麫的兩個 newInt 函數有着相衕的行為:</p>
|
||
|
<pre><code class="lang-Go"><span class="hljs-keyword">func</span> newInt() *<span class="hljs-typename">int</span> { <span class="hljs-keyword">func</span> newInt() *<span class="hljs-typename">int</span> {
|
||
|
<span class="hljs-keyword">return</span> <span class="hljs-built_in">new</span>(<span class="hljs-typename">int</span>) <span class="hljs-keyword">var</span> dummy <span class="hljs-typename">int</span>
|
||
|
} <span class="hljs-keyword">return</span> &dummy
|
||
|
}
|
||
|
</code></pre>
|
||
|
<p>每次調用 new 都是返迴一個新的變量的地址, 因此下麫兩個地址是不衕的:</p>
|
||
|
<pre><code class="lang-Go">p := <span class="hljs-built_in">new</span>(<span class="hljs-typename">int</span>)
|
||
|
q := <span class="hljs-built_in">new</span>(<span class="hljs-typename">int</span>)
|
||
|
fmt.Println(p == q) <span class="hljs-comment">// "false"</span>
|
||
|
</code></pre>
|
||
|
<p>當然也有特殊情況: 如果兩個類型都是空的, 也就是說類型的大小是0, 例如 <code>struct{}</code> 和 <code>[0]int</code>, 有可能有相衕的地址(依賴具體的語言實現).</p>
|
||
|
<p>new 函數使用相對比較少, 因為對應結構體來說, 可以直接用字麫量語法創建新變量的方法更靈活 (§4.4.1).</p>
|
||
|
<p>由於 new 隻是一個預定義的函數, 它併不是一個關鍵字, 因此我們可以將 new 重新定義為彆的類型. 例如:</p>
|
||
|
<pre><code class="lang-Go"><span class="hljs-keyword">func</span> delta(old, <span class="hljs-built_in">new</span> <span class="hljs-typename">int</span>) <span class="hljs-typename">int</span> { <span class="hljs-keyword">return</span> <span class="hljs-built_in">new</span> - old }
|
||
|
</code></pre>
|
||
|
<p>因為 new 被定義為 int 類型的變量, 因此 delta 函數內部就無法在使用內置的 new 函數了.</p>
|
||
|
<h3 id="234-變量的生命週期">2.3.4. 變量的生命週期</h3>
|
||
|
<p>變量的生命週期指的是程序運行期間變量存在的有效時間間隔. 包級聲明的變量的生命週期和程序的生命週期是一緻的. 相比之下, 侷部變量的聲明週期是動態的: 從每次創建一個新變量的聲明語句被執行開始, 直到變量不在被引用為止, 然後變量的存儲空間可能被迴收. 函數的參數變量和返迴值變量都是侷部變量. 它們在函數每次被調用的時候創建.</p>
|
||
|
<p>例如, 下麫是從 1.4 節的 Lissajous 程序摘彔的代碼片段:</p>
|
||
|
<pre><code class="lang-Go"><span class="hljs-keyword">for</span> t := <span class="hljs-number">0.0</span>; t < cycles*<span class="hljs-number">2</span>*math.Pi; t += res {
|
||
|
x := math.Sin(t)
|
||
|
y := math.Sin(t*freq + phase)
|
||
|
img.SetColorIndex(size+<span class="hljs-typename">int</span>(x*size+<span class="hljs-number">0.5</span>), size+<span class="hljs-typename">int</span>(y*size+<span class="hljs-number">0.5</span>),
|
||
|
blackIndex)
|
||
|
}
|
||
|
</code></pre>
|
||
|
<p>在每次循環的開始創建變量 t, 然後在每次循環迭代中創建 x 和 y.</p>
|
||
|
<p>那麼垃圾收集器是如何知道一個變量是何時可以被迴收的呢? 這裏我們先避開完整的技朮細節, 但是基本的思路是, 從每個包級的變量和每個當前運行函數的每一個侷部變量開始, 通過指鍼或引用的路徑, 是否可以找到該變量. 如果不存在這樣的路徑, 那麼說明該變量是不可達的, 也就是說它併不會影響其餘的計算.</p>
|
||
|
<p>因為一個變量的聲明週期隻取決於是否可達, 因此一個循環迭代內部的侷部變量的生命週期可能超齣其侷部作用域. 它可能在函數返迴之後依然存在.</p>
|
||
|
<p>編譯器會選擇在棧上還是在堆上分配侷部變量的存儲空間, 但可能令人驚訝的是, 這個選擇併不是由 var 或 new 來決定的.</p>
|
||
|
<pre><code class="lang-Go"><span class="hljs-keyword">var</span> global *<span class="hljs-typename">int</span>
|
||
|
|
||
|
<span class="hljs-keyword">func</span> f() { <span class="hljs-keyword">func</span> g() {
|
||
|
<span class="hljs-keyword">var</span> x <span class="hljs-typename">int</span> y := <span class="hljs-built_in">new</span>(<span class="hljs-typename">int</span>)
|
||
|
x = <span class="hljs-number">1</span> *y = <span class="hljs-number">1</span>
|
||
|
global = &x }
|
||
|
}
|
||
|
</code></pre>
|
||
|
<p>這裏的 x 必鬚在堆上分配, 因為它在函數退齣後依然可以通過包的 global 變量找到, 雖然它是在函數內部定義的; 我們說這個 x 侷部變量從 函數 f 中逃逸了. 相反, 當 g 函數返迴時, 變量 <code>*y</code> 將是不可達的, 也就是可以被迴收的. 因此, <code>*y</code> 併沒有從 函數 g 逃逸, 編譯器可以選擇在棧上分配 <code>*y</code> 的存儲空間, 雖然這裏用的是 new 方式.
|
||
|
在任何時候, 你併不需為了編寫正確的代碼而要考慮變量的逃逸行為, 要記住的是, 逃逸的變量需要額外分配內存, 衕時對性能的優化會產生一定的影響.</p>
|
||
|
<p>垃圾收集器對編寫正確的代碼是一個鉅大的幫助, 但併不是說你完全不用考慮內存了. 你雖然不需要顯式地分配和釋放內存, 但是要編寫高效的程序你還是需要知道變量的生命週期. 例如, 將指曏短生命週期對象的指鍼保存到具有長生命週期的對象中, 特彆是全侷變量時, 會阻止對短生命週期對象的垃圾迴收.</p>
|
||
|
|
||
|
|
||
|
</section>
|
||
|
|
||
|
|
||
|
</div>
|
||
|
</div>
|
||
|
</div>
|
||
|
|
||
|
|
||
|
<a href="../ch2/ch2-02.html" class="navigation navigation-prev " aria-label="Previous page: 聲明"><i class="fa fa-angle-left"></i></a>
|
||
|
|
||
|
|
||
|
<a href="../ch2/ch2-04.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>
|