mirror of
https://github.com/gopl-zh/gopl-zh.github.com.git
synced 2024-12-26 14:56:21 +00:00
751 lines
38 KiB
HTML
751 lines
38 KiB
HTML
<!DOCTYPE HTML>
|
||
<html lang="zh" class="sidebar-visible no-js light">
|
||
<head>
|
||
<!-- Book generated using https://github.com/wa-lang/wabook -->
|
||
<meta charset="UTF-8">
|
||
<title>并发的循环 - Go语言圣经</title>
|
||
<!-- Custom HTML head -->
|
||
<meta content="text/html; charset=utf-8" http-equiv="Content-Type">
|
||
<meta name="description" content="">
|
||
<meta name="viewport" content="width=device-width, initial-scale=1">
|
||
<meta name="theme-color" content="#ffffff" />
|
||
|
||
<link rel="icon" href="../favicon.svg">
|
||
<link rel="shortcut icon" href="../favicon.png">
|
||
<link rel="stylesheet" href="../static/wabook/css/variables.css">
|
||
<link rel="stylesheet" href="../static/wabook/css/general.css">
|
||
<link rel="stylesheet" href="../static/wabook/css/chrome.css">
|
||
<link rel="stylesheet" href="../static/wabook/css/print.css" media="print">
|
||
<!-- Fonts -->
|
||
<link rel="stylesheet" href="../static/wabook/FontAwesome/css/font-awesome.css">
|
||
<link rel="stylesheet" href="../static/wabook/fonts/fonts.css">
|
||
<!-- Highlight.js Stylesheets -->
|
||
<link rel="stylesheet" href="../static/wabook/highlight.css">
|
||
<link rel="stylesheet" href="../static/wabook/tomorrow-night.css">
|
||
<link rel="stylesheet" href="../static/wabook/ayu-highlight.css">
|
||
|
||
<!-- Custom theme stylesheets -->
|
||
</head>
|
||
<body>
|
||
<!-- Provide site root to javascript -->
|
||
<script type="text/javascript">
|
||
var path_to_root = "../";
|
||
var default_theme = window.matchMedia("(prefers-color-scheme: dark)").matches ? "navy" : "light";
|
||
</script>
|
||
|
||
<!-- Work around some values being stored in localStorage wrapped in quotes -->
|
||
<script type="text/javascript">
|
||
try {
|
||
var theme = localStorage.getItem('wabook-theme');
|
||
var sidebar = localStorage.getItem('wabook-sidebar');
|
||
|
||
if (theme.startsWith('"') && theme.endsWith('"')) {
|
||
localStorage.setItem('wabook-theme', theme.slice(1, theme.length - 1));
|
||
}
|
||
|
||
if (sidebar.startsWith('"') && sidebar.endsWith('"')) {
|
||
localStorage.setItem('wabook-sidebar', sidebar.slice(1, sidebar.length - 1));
|
||
}
|
||
} catch (e) { }
|
||
</script>
|
||
|
||
<!-- Set the theme before any content is loaded, prevents flash -->
|
||
<script type="text/javascript">
|
||
var theme;
|
||
try { theme = localStorage.getItem('wabook-theme'); } catch(e) { }
|
||
if (theme === null || theme === undefined) { theme = default_theme; }
|
||
var html = document.querySelector('html');
|
||
html.classList.remove('no-js')
|
||
html.classList.remove('light')
|
||
html.classList.add(theme);
|
||
html.classList.add('js');
|
||
</script>
|
||
|
||
<!-- Hide / unhide sidebar before it is displayed -->
|
||
<script type="text/javascript">
|
||
var html = document.querySelector('html');
|
||
var sidebar = 'hidden';
|
||
if (document.body.clientWidth >= 1080) {
|
||
try { sidebar = localStorage.getItem('wabook-sidebar'); } catch(e) { }
|
||
sidebar = sidebar || 'visible';
|
||
}
|
||
html.classList.remove('sidebar-visible');
|
||
html.classList.add("sidebar-" + sidebar);
|
||
</script>
|
||
|
||
<nav id="sidebar" class="sidebar" aria-label="Table of contents">
|
||
<div class="sidebar-scrollbox">
|
||
<ol class="chapter">
|
||
<li class="chapter-item expanded ">
|
||
<a href="../index.html" >Go语言圣经</a>
|
||
</li>
|
||
<li class="chapter-item expanded ">
|
||
<a href="../preface-zh.html" >译者序</a>
|
||
</li>
|
||
<li class="chapter-item expanded ">
|
||
<a href="../preface.html" >前言</a>
|
||
</li>
|
||
<li class="chapter-item expanded ">
|
||
<a href="../ch1/ch1.html" ><strong aria-hidden="true">1.</strong> 入门</a>
|
||
</li>
|
||
<ol class="section">
|
||
<li class="chapter-item expanded ">
|
||
<a href="../ch1/ch1-01.html" ><strong aria-hidden="true">1.1.</strong> Hello, World</a>
|
||
</li>
|
||
<li class="chapter-item expanded ">
|
||
<a href="../ch1/ch1-02.html" ><strong aria-hidden="true">1.2.</strong> 命令行参数</a>
|
||
</li>
|
||
<li class="chapter-item expanded ">
|
||
<a href="../ch1/ch1-03.html" ><strong aria-hidden="true">1.3.</strong> 查找重复的行</a>
|
||
</li>
|
||
<li class="chapter-item expanded ">
|
||
<a href="../ch1/ch1-04.html" ><strong aria-hidden="true">1.4.</strong> GIF动画</a>
|
||
</li>
|
||
<li class="chapter-item expanded ">
|
||
<a href="../ch1/ch1-05.html" ><strong aria-hidden="true">1.5.</strong> 获取URL</a>
|
||
</li>
|
||
<li class="chapter-item expanded ">
|
||
<a href="../ch1/ch1-06.html" ><strong aria-hidden="true">1.6.</strong> 并发获取多个URL</a>
|
||
</li>
|
||
<li class="chapter-item expanded ">
|
||
<a href="../ch1/ch1-07.html" ><strong aria-hidden="true">1.7.</strong> Web服务</a>
|
||
</li>
|
||
<li class="chapter-item expanded ">
|
||
<a href="../ch1/ch1-08.html" ><strong aria-hidden="true">1.8.</strong> 本章要点</a>
|
||
</li>
|
||
</ol>
|
||
<li class="chapter-item expanded ">
|
||
<a href="../ch2/ch2.html" ><strong aria-hidden="true">2.</strong> 程序结构</a>
|
||
</li>
|
||
<ol class="section">
|
||
<li class="chapter-item expanded ">
|
||
<a href="../ch2/ch2-01.html" ><strong aria-hidden="true">2.1.</strong> 命名</a>
|
||
</li>
|
||
<li class="chapter-item expanded ">
|
||
<a href="../ch2/ch2-02.html" ><strong aria-hidden="true">2.2.</strong> 声明</a>
|
||
</li>
|
||
<li class="chapter-item expanded ">
|
||
<a href="../ch2/ch2-03.html" ><strong aria-hidden="true">2.3.</strong> 变量</a>
|
||
</li>
|
||
<li class="chapter-item expanded ">
|
||
<a href="../ch2/ch2-04.html" ><strong aria-hidden="true">2.4.</strong> 赋值</a>
|
||
</li>
|
||
<li class="chapter-item expanded ">
|
||
<a href="../ch2/ch2-05.html" ><strong aria-hidden="true">2.5.</strong> 类型</a>
|
||
</li>
|
||
<li class="chapter-item expanded ">
|
||
<a href="../ch2/ch2-06.html" ><strong aria-hidden="true">2.6.</strong> 包和文件</a>
|
||
</li>
|
||
<li class="chapter-item expanded ">
|
||
<a href="../ch2/ch2-07.html" ><strong aria-hidden="true">2.7.</strong> 作用域</a>
|
||
</li>
|
||
</ol>
|
||
<li class="chapter-item expanded ">
|
||
<a href="../ch3/ch3.html" ><strong aria-hidden="true">3.</strong> 基础数据类型</a>
|
||
</li>
|
||
<ol class="section">
|
||
<li class="chapter-item expanded ">
|
||
<a href="../ch3/ch3-01.html" ><strong aria-hidden="true">3.1.</strong> 整型</a>
|
||
</li>
|
||
<li class="chapter-item expanded ">
|
||
<a href="../ch3/ch3-02.html" ><strong aria-hidden="true">3.2.</strong> 浮点数</a>
|
||
</li>
|
||
<li class="chapter-item expanded ">
|
||
<a href="../ch3/ch3-03.html" ><strong aria-hidden="true">3.3.</strong> 复数</a>
|
||
</li>
|
||
<li class="chapter-item expanded ">
|
||
<a href="../ch3/ch3-04.html" ><strong aria-hidden="true">3.4.</strong> 布尔型</a>
|
||
</li>
|
||
<li class="chapter-item expanded ">
|
||
<a href="../ch3/ch3-05.html" ><strong aria-hidden="true">3.5.</strong> 字符串</a>
|
||
</li>
|
||
<li class="chapter-item expanded ">
|
||
<a href="../ch3/ch3-06.html" ><strong aria-hidden="true">3.6.</strong> 常量</a>
|
||
</li>
|
||
</ol>
|
||
<li class="chapter-item expanded ">
|
||
<a href="../ch4/ch4.html" ><strong aria-hidden="true">4.</strong> 复合数据类型</a>
|
||
</li>
|
||
<ol class="section">
|
||
<li class="chapter-item expanded ">
|
||
<a href="../ch4/ch4-01.html" ><strong aria-hidden="true">4.1.</strong> 数组</a>
|
||
</li>
|
||
<li class="chapter-item expanded ">
|
||
<a href="../ch4/ch4-02.html" ><strong aria-hidden="true">4.2.</strong> Slice</a>
|
||
</li>
|
||
<li class="chapter-item expanded ">
|
||
<a href="../ch4/ch4-03.html" ><strong aria-hidden="true">4.3.</strong> Map</a>
|
||
</li>
|
||
<li class="chapter-item expanded ">
|
||
<a href="../ch4/ch4-04.html" ><strong aria-hidden="true">4.4.</strong> 结构体</a>
|
||
</li>
|
||
<li class="chapter-item expanded ">
|
||
<a href="../ch4/ch4-05.html" ><strong aria-hidden="true">4.5.</strong> JSON</a>
|
||
</li>
|
||
<li class="chapter-item expanded ">
|
||
<a href="../ch4/ch4-06.html" ><strong aria-hidden="true">4.6.</strong> 文本和HTML模板</a>
|
||
</li>
|
||
</ol>
|
||
<li class="chapter-item expanded ">
|
||
<a href="../ch5/ch5.html" ><strong aria-hidden="true">5.</strong> 函数</a>
|
||
</li>
|
||
<ol class="section">
|
||
<li class="chapter-item expanded ">
|
||
<a href="../ch5/ch5-01.html" ><strong aria-hidden="true">5.1.</strong> 函数声明</a>
|
||
</li>
|
||
<li class="chapter-item expanded ">
|
||
<a href="../ch5/ch5-02.html" ><strong aria-hidden="true">5.2.</strong> 递归</a>
|
||
</li>
|
||
<li class="chapter-item expanded ">
|
||
<a href="../ch5/ch5-03.html" ><strong aria-hidden="true">5.3.</strong> 多返回值</a>
|
||
</li>
|
||
<li class="chapter-item expanded ">
|
||
<a href="../ch5/ch5-04.html" ><strong aria-hidden="true">5.4.</strong> 错误</a>
|
||
</li>
|
||
<li class="chapter-item expanded ">
|
||
<a href="../ch5/ch5-05.html" ><strong aria-hidden="true">5.5.</strong> 函数值</a>
|
||
</li>
|
||
<li class="chapter-item expanded ">
|
||
<a href="../ch5/ch5-06.html" ><strong aria-hidden="true">5.6.</strong> 匿名函数</a>
|
||
</li>
|
||
<li class="chapter-item expanded ">
|
||
<a href="../ch5/ch5-07.html" ><strong aria-hidden="true">5.7.</strong> 可变参数</a>
|
||
</li>
|
||
<li class="chapter-item expanded ">
|
||
<a href="../ch5/ch5-08.html" ><strong aria-hidden="true">5.8.</strong> Deferred函数</a>
|
||
</li>
|
||
<li class="chapter-item expanded ">
|
||
<a href="../ch5/ch5-09.html" ><strong aria-hidden="true">5.9.</strong> Panic异常</a>
|
||
</li>
|
||
<li class="chapter-item expanded ">
|
||
<a href="../ch5/ch5-10.html" ><strong aria-hidden="true">5.10.</strong> Recover捕获异常</a>
|
||
</li>
|
||
</ol>
|
||
<li class="chapter-item expanded ">
|
||
<a href="../ch6/ch6.html" ><strong aria-hidden="true">6.</strong> 方法</a>
|
||
</li>
|
||
<ol class="section">
|
||
<li class="chapter-item expanded ">
|
||
<a href="../ch6/ch6-01.html" ><strong aria-hidden="true">6.1.</strong> 方法声明</a>
|
||
</li>
|
||
<li class="chapter-item expanded ">
|
||
<a href="../ch6/ch6-02.html" ><strong aria-hidden="true">6.2.</strong> 基于指针对象的方法</a>
|
||
</li>
|
||
<li class="chapter-item expanded ">
|
||
<a href="../ch6/ch6-03.html" ><strong aria-hidden="true">6.3.</strong> 通过嵌入结构体来扩展类型</a>
|
||
</li>
|
||
<li class="chapter-item expanded ">
|
||
<a href="../ch6/ch6-04.html" ><strong aria-hidden="true">6.4.</strong> 方法值和方法表达式</a>
|
||
</li>
|
||
<li class="chapter-item expanded ">
|
||
<a href="../ch6/ch6-05.html" ><strong aria-hidden="true">6.5.</strong> 示例: Bit数组</a>
|
||
</li>
|
||
<li class="chapter-item expanded ">
|
||
<a href="../ch6/ch6-06.html" ><strong aria-hidden="true">6.6.</strong> 封装</a>
|
||
</li>
|
||
</ol>
|
||
<li class="chapter-item expanded ">
|
||
<a href="../ch7/ch7.html" ><strong aria-hidden="true">7.</strong> 接口</a>
|
||
</li>
|
||
<ol class="section">
|
||
<li class="chapter-item expanded ">
|
||
<a href="../ch7/ch7-01.html" ><strong aria-hidden="true">7.1.</strong> 接口是合约</a>
|
||
</li>
|
||
<li class="chapter-item expanded ">
|
||
<a href="../ch7/ch7-02.html" ><strong aria-hidden="true">7.2.</strong> 接口类型</a>
|
||
</li>
|
||
<li class="chapter-item expanded ">
|
||
<a href="../ch7/ch7-03.html" ><strong aria-hidden="true">7.3.</strong> 实现接口的条件</a>
|
||
</li>
|
||
<li class="chapter-item expanded ">
|
||
<a href="../ch7/ch7-04.html" ><strong aria-hidden="true">7.4.</strong> flag.Value接口</a>
|
||
</li>
|
||
<li class="chapter-item expanded ">
|
||
<a href="../ch7/ch7-05.html" ><strong aria-hidden="true">7.5.</strong> 接口值</a>
|
||
</li>
|
||
<li class="chapter-item expanded ">
|
||
<a href="../ch7/ch7-06.html" ><strong aria-hidden="true">7.6.</strong> sort.Interface接口</a>
|
||
</li>
|
||
<li class="chapter-item expanded ">
|
||
<a href="../ch7/ch7-07.html" ><strong aria-hidden="true">7.7.</strong> http.Handler接口</a>
|
||
</li>
|
||
<li class="chapter-item expanded ">
|
||
<a href="../ch7/ch7-08.html" ><strong aria-hidden="true">7.8.</strong> error接口</a>
|
||
</li>
|
||
<li class="chapter-item expanded ">
|
||
<a href="../ch7/ch7-09.html" ><strong aria-hidden="true">7.9.</strong> 示例: 表达式求值</a>
|
||
</li>
|
||
<li class="chapter-item expanded ">
|
||
<a href="../ch7/ch7-10.html" ><strong aria-hidden="true">7.10.</strong> 类型断言</a>
|
||
</li>
|
||
<li class="chapter-item expanded ">
|
||
<a href="../ch7/ch7-11.html" ><strong aria-hidden="true">7.11.</strong> 基于类型断言识别错误类型</a>
|
||
</li>
|
||
<li class="chapter-item expanded ">
|
||
<a href="../ch7/ch7-12.html" ><strong aria-hidden="true">7.12.</strong> 通过类型断言查询接口</a>
|
||
</li>
|
||
<li class="chapter-item expanded ">
|
||
<a href="../ch7/ch7-13.html" ><strong aria-hidden="true">7.13.</strong> 类型分支</a>
|
||
</li>
|
||
<li class="chapter-item expanded ">
|
||
<a href="../ch7/ch7-14.html" ><strong aria-hidden="true">7.14.</strong> 示例: 基于标记的XML解码</a>
|
||
</li>
|
||
<li class="chapter-item expanded ">
|
||
<a href="../ch7/ch7-15.html" ><strong aria-hidden="true">7.15.</strong> 补充几点</a>
|
||
</li>
|
||
</ol>
|
||
<li class="chapter-item expanded ">
|
||
<a href="../ch8/ch8.html" ><strong aria-hidden="true">8.</strong> Goroutines和Channels</a>
|
||
</li>
|
||
<ol class="section">
|
||
<li class="chapter-item expanded ">
|
||
<a href="../ch8/ch8-01.html" ><strong aria-hidden="true">8.1.</strong> Goroutines</a>
|
||
</li>
|
||
<li class="chapter-item expanded ">
|
||
<a href="../ch8/ch8-02.html" ><strong aria-hidden="true">8.2.</strong> 示例: 并发的Clock服务</a>
|
||
</li>
|
||
<li class="chapter-item expanded ">
|
||
<a href="../ch8/ch8-03.html" ><strong aria-hidden="true">8.3.</strong> 示例: 并发的Echo服务</a>
|
||
</li>
|
||
<li class="chapter-item expanded ">
|
||
<a href="../ch8/ch8-04.html" ><strong aria-hidden="true">8.4.</strong> Channels</a>
|
||
</li>
|
||
<li class="chapter-item expanded ">
|
||
<a href="../ch8/ch8-05.html" class="active"><strong aria-hidden="true">8.5.</strong> 并发的循环</a>
|
||
</li>
|
||
<li class="chapter-item expanded ">
|
||
<a href="../ch8/ch8-06.html" ><strong aria-hidden="true">8.6.</strong> 示例: 并发的Web爬虫</a>
|
||
</li>
|
||
<li class="chapter-item expanded ">
|
||
<a href="../ch8/ch8-07.html" ><strong aria-hidden="true">8.7.</strong> 基于select的多路复用</a>
|
||
</li>
|
||
<li class="chapter-item expanded ">
|
||
<a href="../ch8/ch8-08.html" ><strong aria-hidden="true">8.8.</strong> 示例: 并发的目录遍历</a>
|
||
</li>
|
||
<li class="chapter-item expanded ">
|
||
<a href="../ch8/ch8-09.html" ><strong aria-hidden="true">8.9.</strong> 并发的退出</a>
|
||
</li>
|
||
<li class="chapter-item expanded ">
|
||
<a href="../ch8/ch8-10.html" ><strong aria-hidden="true">8.10.</strong> 示例: 聊天服务</a>
|
||
</li>
|
||
</ol>
|
||
<li class="chapter-item expanded ">
|
||
<a href="../ch9/ch9.html" ><strong aria-hidden="true">9.</strong> 基于共享变量的并发</a>
|
||
</li>
|
||
<ol class="section">
|
||
<li class="chapter-item expanded ">
|
||
<a href="../ch9/ch9-01.html" ><strong aria-hidden="true">9.1.</strong> 竞争条件</a>
|
||
</li>
|
||
<li class="chapter-item expanded ">
|
||
<a href="../ch9/ch9-02.html" ><strong aria-hidden="true">9.2.</strong> sync.Mutex互斥锁</a>
|
||
</li>
|
||
<li class="chapter-item expanded ">
|
||
<a href="../ch9/ch9-03.html" ><strong aria-hidden="true">9.3.</strong> sync.RWMutex读写锁</a>
|
||
</li>
|
||
<li class="chapter-item expanded ">
|
||
<a href="../ch9/ch9-04.html" ><strong aria-hidden="true">9.4.</strong> 内存同步</a>
|
||
</li>
|
||
<li class="chapter-item expanded ">
|
||
<a href="../ch9/ch9-05.html" ><strong aria-hidden="true">9.5.</strong> sync.Once惰性初始化</a>
|
||
</li>
|
||
<li class="chapter-item expanded ">
|
||
<a href="../ch9/ch9-06.html" ><strong aria-hidden="true">9.6.</strong> 竞争条件检测</a>
|
||
</li>
|
||
<li class="chapter-item expanded ">
|
||
<a href="../ch9/ch9-07.html" ><strong aria-hidden="true">9.7.</strong> 示例: 并发的非阻塞缓存</a>
|
||
</li>
|
||
<li class="chapter-item expanded ">
|
||
<a href="../ch9/ch9-08.html" ><strong aria-hidden="true">9.8.</strong> Goroutines和线程</a>
|
||
</li>
|
||
</ol>
|
||
<li class="chapter-item expanded ">
|
||
<a href="../ch10/ch10.html" ><strong aria-hidden="true">10.</strong> 包和工具</a>
|
||
</li>
|
||
<ol class="section">
|
||
<li class="chapter-item expanded ">
|
||
<a href="../ch10/ch10-01.html" ><strong aria-hidden="true">10.1.</strong> 包简介</a>
|
||
</li>
|
||
<li class="chapter-item expanded ">
|
||
<a href="../ch10/ch10-02.html" ><strong aria-hidden="true">10.2.</strong> 导入路径</a>
|
||
</li>
|
||
<li class="chapter-item expanded ">
|
||
<a href="../ch10/ch10-03.html" ><strong aria-hidden="true">10.3.</strong> 包声明</a>
|
||
</li>
|
||
<li class="chapter-item expanded ">
|
||
<a href="../ch10/ch10-04.html" ><strong aria-hidden="true">10.4.</strong> 导入声明</a>
|
||
</li>
|
||
<li class="chapter-item expanded ">
|
||
<a href="../ch10/ch10-05.html" ><strong aria-hidden="true">10.5.</strong> 包的匿名导入</a>
|
||
</li>
|
||
<li class="chapter-item expanded ">
|
||
<a href="../ch10/ch10-06.html" ><strong aria-hidden="true">10.6.</strong> 包和命名</a>
|
||
</li>
|
||
<li class="chapter-item expanded ">
|
||
<a href="../ch10/ch10-07.html" ><strong aria-hidden="true">10.7.</strong> 工具</a>
|
||
</li>
|
||
</ol>
|
||
<li class="chapter-item expanded ">
|
||
<a href="../ch11/ch11.html" ><strong aria-hidden="true">11.</strong> 测试</a>
|
||
</li>
|
||
<ol class="section">
|
||
<li class="chapter-item expanded ">
|
||
<a href="../ch11/ch11-01.html" ><strong aria-hidden="true">11.1.</strong> go test</a>
|
||
</li>
|
||
<li class="chapter-item expanded ">
|
||
<a href="../ch11/ch11-02.html" ><strong aria-hidden="true">11.2.</strong> 测试函数</a>
|
||
</li>
|
||
<li class="chapter-item expanded ">
|
||
<a href="../ch11/ch11-03.html" ><strong aria-hidden="true">11.3.</strong> 测试覆盖率</a>
|
||
</li>
|
||
<li class="chapter-item expanded ">
|
||
<a href="../ch11/ch11-04.html" ><strong aria-hidden="true">11.4.</strong> 基准测试</a>
|
||
</li>
|
||
<li class="chapter-item expanded ">
|
||
<a href="../ch11/ch11-05.html" ><strong aria-hidden="true">11.5.</strong> 剖析</a>
|
||
</li>
|
||
<li class="chapter-item expanded ">
|
||
<a href="../ch11/ch11-06.html" ><strong aria-hidden="true">11.6.</strong> 示例函数</a>
|
||
</li>
|
||
</ol>
|
||
<li class="chapter-item expanded ">
|
||
<a href="../ch12/ch12.html" ><strong aria-hidden="true">12.</strong> 反射</a>
|
||
</li>
|
||
<ol class="section">
|
||
<li class="chapter-item expanded ">
|
||
<a href="../ch12/ch12-01.html" ><strong aria-hidden="true">12.1.</strong> 为何需要反射?</a>
|
||
</li>
|
||
<li class="chapter-item expanded ">
|
||
<a href="../ch12/ch12-02.html" ><strong aria-hidden="true">12.2.</strong> reflect.Type和reflect.Value</a>
|
||
</li>
|
||
<li class="chapter-item expanded ">
|
||
<a href="../ch12/ch12-03.html" ><strong aria-hidden="true">12.3.</strong> Display递归打印</a>
|
||
</li>
|
||
<li class="chapter-item expanded ">
|
||
<a href="../ch12/ch12-04.html" ><strong aria-hidden="true">12.4.</strong> 示例: 编码S表达式</a>
|
||
</li>
|
||
<li class="chapter-item expanded ">
|
||
<a href="../ch12/ch12-05.html" ><strong aria-hidden="true">12.5.</strong> 通过reflect.Value修改值</a>
|
||
</li>
|
||
<li class="chapter-item expanded ">
|
||
<a href="../ch12/ch12-06.html" ><strong aria-hidden="true">12.6.</strong> 示例: 解码S表达式</a>
|
||
</li>
|
||
<li class="chapter-item expanded ">
|
||
<a href="../ch12/ch12-07.html" ><strong aria-hidden="true">12.7.</strong> 获取结构体字段标签</a>
|
||
</li>
|
||
<li class="chapter-item expanded ">
|
||
<a href="../ch12/ch12-08.html" ><strong aria-hidden="true">12.8.</strong> 显示一个类型的方法集</a>
|
||
</li>
|
||
<li class="chapter-item expanded ">
|
||
<a href="../ch12/ch12-09.html" ><strong aria-hidden="true">12.9.</strong> 几点忠告</a>
|
||
</li>
|
||
</ol>
|
||
<li class="chapter-item expanded ">
|
||
<a href="../ch13/ch13.html" ><strong aria-hidden="true">13.</strong> 底层编程</a>
|
||
</li>
|
||
<ol class="section">
|
||
<li class="chapter-item expanded ">
|
||
<a href="../ch13/ch13-01.html" ><strong aria-hidden="true">13.1.</strong> unsafe.Sizeof, Alignof 和 Offsetof</a>
|
||
</li>
|
||
<li class="chapter-item expanded ">
|
||
<a href="../ch13/ch13-02.html" ><strong aria-hidden="true">13.2.</strong> unsafe.Pointer</a>
|
||
</li>
|
||
<li class="chapter-item expanded ">
|
||
<a href="../ch13/ch13-03.html" ><strong aria-hidden="true">13.3.</strong> 示例: 深度相等判断</a>
|
||
</li>
|
||
<li class="chapter-item expanded ">
|
||
<a href="../ch13/ch13-04.html" ><strong aria-hidden="true">13.4.</strong> 通过cgo调用C代码</a>
|
||
</li>
|
||
<li class="chapter-item expanded ">
|
||
<a href="../ch13/ch13-05.html" ><strong aria-hidden="true">13.5.</strong> 几点忠告</a>
|
||
</li>
|
||
</ol>
|
||
<li class="chapter-item expanded ">
|
||
<a href="../appendix/appendix.html" ><strong aria-hidden="true">14.</strong> 附录</a>
|
||
</li>
|
||
<ol class="section">
|
||
<li class="chapter-item expanded ">
|
||
<a href="../appendix/appendix-a-errata.html" ><strong aria-hidden="true">14.1.</strong> 附录A:原文勘误</a>
|
||
</li>
|
||
<li class="chapter-item expanded ">
|
||
<a href="../appendix/appendix-b-author.html" ><strong aria-hidden="true">14.2.</strong> 附录B:作者译者</a>
|
||
</li>
|
||
<li class="chapter-item expanded ">
|
||
<a href="../appendix/appendix-c-cpoyright.html" ><strong aria-hidden="true">14.3.</strong> 附录C:译文授权</a>
|
||
</li>
|
||
<li class="chapter-item expanded ">
|
||
<a href="../appendix/appendix-d-translations.html" ><strong aria-hidden="true">14.4.</strong> 附录D:其它语言</a>
|
||
</li>
|
||
</ol>
|
||
</ol>
|
||
|
||
</div>
|
||
<div id="sidebar-resize-handle" class="sidebar-resize-handle"></div>
|
||
</nav>
|
||
|
||
<div id="page-wrapper" class="page-wrapper">
|
||
|
||
<div class="page">
|
||
<div id="menu-bar-hover-placeholder"></div>
|
||
<div id="menu-bar" class="menu-bar sticky bordered">
|
||
<div class="left-buttons">
|
||
<button id="sidebar-toggle" class="icon-button" type="button" title="Toggle Table of Contents" aria-label="Toggle Table of Contents" aria-controls="sidebar">
|
||
<i class="fa fa-bars"></i>
|
||
</button>
|
||
<button id="theme-toggle" class="icon-button" type="button" title="Change theme" aria-label="Change theme" aria-haspopup="true" aria-expanded="false" aria-controls="theme-list">
|
||
<i class="fa fa-paint-brush"></i>
|
||
</button>
|
||
<ul id="theme-list" class="theme-popup" aria-label="Themes" role="menu">
|
||
<li role="none"><button role="menuitem" class="theme" id="light">Light (default)</button></li>
|
||
<li role="none"><button role="menuitem" class="theme" id="coal">Coal</button></li>
|
||
<li role="none"><button role="menuitem" class="theme" id="navy">Navy</button></li>
|
||
<li role="none"><button role="menuitem" class="theme" id="ayu">Ayu</button></li>
|
||
</ul>
|
||
</div>
|
||
|
||
<h1 class="menu-title"><a href="../index.html">Go语言圣经</a></h1>
|
||
|
||
<div class="right-buttons">
|
||
<a href="https://github.com/gopl-zh/gopl-zh.github.com" title="Git repository" aria-label="Git repository">
|
||
<i id="git-repository-button" class="fa fa-github"></i>
|
||
</a>
|
||
<a href="https://github.com/gopl-zh/gopl-zh.github.com/edit/master/ch8/ch8-05.md" title="Suggest an edit" aria-label="Suggest an edit">
|
||
<i id="git-edit-button" class="fa fa-edit"></i>
|
||
</a>
|
||
</div>
|
||
</div>
|
||
|
||
<!-- Apply ARIA attributes after the sidebar and the sidebar toggle button are added to the DOM -->
|
||
<script type="text/javascript">
|
||
document.getElementById('sidebar-toggle').setAttribute('aria-expanded', sidebar === 'visible');
|
||
document.getElementById('sidebar').setAttribute('aria-hidden', sidebar !== 'visible');
|
||
Array.from(document.querySelectorAll('#sidebar a')).forEach(function(link) {
|
||
link.setAttribute('tabIndex', sidebar === 'visible' ? 0 : -1);
|
||
});
|
||
</script>
|
||
|
||
<div id="content" class="content">
|
||
<!-- Page table of contents -->
|
||
<div class="sidetoc"><nav class="pagetoc"></nav></div>
|
||
|
||
<main>
|
||
<ul dir="auto"><li><em>凹语言(Go实现, 面向WASM设计): <a href="https://github.com/wa-lang/wa">https://github.com/wa-lang/wa</a></em></li><li><em>WaBook(Go语言实现的MD电子书构建工具): <a href="https://github.com/wa-lang/wabook">https://github.com/wa-lang/wabook</a></em></li></ul><hr>
|
||
|
||
<h2>8.5. 并发的循环</h2>
|
||
<p>本节中,我们会探索一些用来在并行时循环迭代的常见并发模型。我们会探究从全尺寸图片生成一些缩略图的问题。gopl.io/ch8/thumbnail包提供了ImageFile函数来帮我们拉伸图片。我们不会说明这个函数的实现,只需要从gopl.io下载它。</p>
|
||
<p><!-- raw HTML omitted --><!-- raw HTML omitted -->gopl.io/ch8/thumbnail<!-- raw HTML omitted --><!-- raw HTML omitted --></p>
|
||
<pre><code class="language-go">package thumbnail
|
||
|
||
// ImageFile reads an image from infile and writes
|
||
// a thumbnail-size version of it in the same directory.
|
||
// It returns the generated file name, e.g., "foo.thumb.jpg".
|
||
func ImageFile(infile string) (string, error)
|
||
</code></pre>
|
||
<p>下面的程序会循环迭代一些图片文件名,并为每一张图片生成一个缩略图:</p>
|
||
<p><!-- raw HTML omitted --><!-- raw HTML omitted -->gopl.io/ch8/thumbnail<!-- raw HTML omitted --><!-- raw HTML omitted --></p>
|
||
<pre><code class="language-go">// makeThumbnails makes thumbnails of the specified files.
|
||
func makeThumbnails(filenames []string) {
|
||
for _, f := range filenames {
|
||
if _, err := thumbnail.ImageFile(f); err != nil {
|
||
log.Println(err)
|
||
}
|
||
}
|
||
}
|
||
</code></pre>
|
||
<p>显然我们处理文件的顺序无关紧要,因为每一个图片的拉伸操作和其它图片的处理操作都是彼此独立的。像这种子问题都是完全彼此独立的问题被叫做易并行问题(译注:embarrassingly parallel,直译的话更像是尴尬并行)。易并行问题是最容易被实现成并行的一类问题(废话),并且最能够享受到并发带来的好处,能够随着并行的规模线性地扩展。</p>
|
||
<p>下面让我们并行地执行这些操作,从而将文件IO的延迟隐藏掉,并用上多核cpu的计算能力来拉伸图像。我们的第一个并发程序只是使用了一个go关键字。这里我们先忽略掉错误,之后再进行处理。</p>
|
||
<pre><code class="language-go">// NOTE: incorrect!
|
||
func makeThumbnails2(filenames []string) {
|
||
for _, f := range filenames {
|
||
go thumbnail.ImageFile(f) // NOTE: ignoring errors
|
||
}
|
||
}
|
||
</code></pre>
|
||
<p>这个版本运行的实在有点太快,实际上,由于它比最早的版本使用的时间要短得多,即使当文件名的slice中只包含有一个元素。这就有点奇怪了,如果程序没有并发执行的话,那为什么一个并发的版本还是要快呢?答案其实是makeThumbnails在它还没有完成工作之前就已经返回了。它启动了所有的goroutine,每一个文件名对应一个,但没有等待它们一直到执行完毕。</p>
|
||
<p>没有什么直接的办法能够等待goroutine完成,但是我们可以改变goroutine里的代码让其能够将完成情况报告给外部的goroutine知晓,使用的方式是向一个共享的channel中发送事件。因为我们已经确切地知道有len(filenames)个内部goroutine,所以外部的goroutine只需要在返回之前对这些事件计数。</p>
|
||
<pre><code class="language-go">// makeThumbnails3 makes thumbnails of the specified files in parallel.
|
||
func makeThumbnails3(filenames []string) {
|
||
ch := make(chan struct{})
|
||
for _, f := range filenames {
|
||
go func(f string) {
|
||
thumbnail.ImageFile(f) // NOTE: ignoring errors
|
||
ch <- struct{}{}
|
||
}(f)
|
||
}
|
||
// Wait for goroutines to complete.
|
||
for range filenames {
|
||
<-ch
|
||
}
|
||
}
|
||
</code></pre>
|
||
<p>注意我们将f的值作为一个显式的变量传给了函数,而不是在循环的闭包中声明:</p>
|
||
<pre><code class="language-go">for _, f := range filenames {
|
||
go func() {
|
||
thumbnail.ImageFile(f) // NOTE: incorrect!
|
||
// ...
|
||
}()
|
||
}
|
||
</code></pre>
|
||
<p>回忆一下之前在5.6.1节中,匿名函数中的循环变量快照问题。上面这个单独的变量f是被所有的匿名函数值所共享,且会被连续的循环迭代所更新的。当新的goroutine开始执行字面函数时,for循环可能已经更新了f并且开始了另一轮的迭代或者(更有可能的)已经结束了整个循环,所以当这些goroutine开始读取f的值时,它们所看到的值已经是slice的最后一个元素了。显式地添加这个参数,我们能够确保使用的f是当go语句执行时的“当前”那个f。</p>
|
||
<p>如果我们想要从每一个worker goroutine往主goroutine中返回值时该怎么办呢?当我们调用thumbnail.ImageFile创建文件失败的时候,它会返回一个错误。下一个版本的makeThumbnails会返回其在做拉伸操作时接收到的第一个错误:</p>
|
||
<pre><code class="language-go">// makeThumbnails4 makes thumbnails for the specified files in parallel.
|
||
// It returns an error if any step failed.
|
||
func makeThumbnails4(filenames []string) error {
|
||
errors := make(chan error)
|
||
|
||
for _, f := range filenames {
|
||
go func(f string) {
|
||
_, err := thumbnail.ImageFile(f)
|
||
errors <- err
|
||
}(f)
|
||
}
|
||
|
||
for range filenames {
|
||
if err := <-errors; err != nil {
|
||
return err // NOTE: incorrect: goroutine leak!
|
||
}
|
||
}
|
||
|
||
return nil
|
||
}
|
||
</code></pre>
|
||
<p>这个程序有一个微妙的bug。当它遇到第一个非nil的error时会直接将error返回到调用方,使得没有一个goroutine去排空errors channel。这样剩下的worker goroutine在向这个channel中发送值时,都会永远地阻塞下去,并且永远都不会退出。这种情况叫做goroutine泄露(§8.4.4),可能会导致整个程序卡住或者跑出out of memory的错误。</p>
|
||
<p>最简单的解决办法就是用一个具有合适大小的buffered channel,这样这些worker goroutine向channel中发送错误时就不会被阻塞。(一个可选的解决办法是创建一个另外的goroutine,当main goroutine返回第一个错误的同时去排空channel。)</p>
|
||
<p>下一个版本的makeThumbnails使用了一个buffered channel来返回生成的图片文件的名字,附带生成时的错误。</p>
|
||
<pre><code class="language-go">// makeThumbnails5 makes thumbnails for the specified files in parallel.
|
||
// It returns the generated file names in an arbitrary order,
|
||
// or an error if any step failed.
|
||
func makeThumbnails5(filenames []string) (thumbfiles []string, err error) {
|
||
type item struct {
|
||
thumbfile string
|
||
err error
|
||
}
|
||
|
||
ch := make(chan item, len(filenames))
|
||
for _, f := range filenames {
|
||
go func(f string) {
|
||
var it item
|
||
it.thumbfile, it.err = thumbnail.ImageFile(f)
|
||
ch <- it
|
||
}(f)
|
||
}
|
||
|
||
for range filenames {
|
||
it := <-ch
|
||
if it.err != nil {
|
||
return nil, it.err
|
||
}
|
||
thumbfiles = append(thumbfiles, it.thumbfile)
|
||
}
|
||
|
||
return thumbfiles, nil
|
||
}
|
||
</code></pre>
|
||
<p>我们最后一个版本的makeThumbnails返回了新文件们的大小总计数(bytes)。和前面的版本都不一样的一点是我们在这个版本里没有把文件名放在slice里,而是通过一个string的channel传过来,所以我们无法对循环的次数进行预测。</p>
|
||
<p>为了知道最后一个goroutine什么时候结束(最后一个结束并不一定是最后一个开始),我们需要一个递增的计数器,在每一个goroutine启动时加一,在goroutine退出时减一。这需要一种特殊的计数器,这个计数器需要在多个goroutine操作时做到安全并且提供在其减为零之前一直等待的一种方法。这种计数类型被称为sync.WaitGroup,下面的代码就用到了这种方法:</p>
|
||
<pre><code class="language-go">// makeThumbnails6 makes thumbnails for each file received from the channel.
|
||
// It returns the number of bytes occupied by the files it creates.
|
||
func makeThumbnails6(filenames <-chan string) int64 {
|
||
sizes := make(chan int64)
|
||
var wg sync.WaitGroup // number of working goroutines
|
||
for f := range filenames {
|
||
wg.Add(1)
|
||
// worker
|
||
go func(f string) {
|
||
defer wg.Done()
|
||
thumb, err := thumbnail.ImageFile(f)
|
||
if err != nil {
|
||
log.Println(err)
|
||
return
|
||
}
|
||
info, _ := os.Stat(thumb) // OK to ignore error
|
||
sizes <- info.Size()
|
||
}(f)
|
||
}
|
||
|
||
// closer
|
||
go func() {
|
||
wg.Wait()
|
||
close(sizes)
|
||
}()
|
||
|
||
var total int64
|
||
for size := range sizes {
|
||
total += size
|
||
}
|
||
return total
|
||
}
|
||
</code></pre>
|
||
<p>注意Add和Done方法的不对称。Add是为计数器加一,必须在worker goroutine开始之前调用,而不是在goroutine中;否则的话我们没办法确定Add是在"closer" goroutine调用Wait之前被调用。并且Add还有一个参数,但Done却没有任何参数;其实它和Add(-1)是等价的。我们使用defer来确保计数器即使是在出错的情况下依然能够正确地被减掉。上面的程序代码结构是当我们使用并发循环,但又不知道迭代次数时很通常而且很地道的写法。</p>
|
||
<p>sizes channel携带了每一个文件的大小到main goroutine,在main goroutine中使用了range loop来计算总和。观察一下我们是怎样创建一个closer goroutine,并让其在所有worker goroutine们结束之后再关闭sizes channel的。两步操作:wait和close,必须是基于sizes的循环的并发。考虑一下另一种方案:如果等待操作被放在了main goroutine中,在循环之前,这样的话就永远都不会结束了,如果在循环之后,那么又变成了不可达的部分,因为没有任何东西去关闭这个channel,这个循环就永远都不会终止。</p>
|
||
<p>图8.5 表明了makethumbnails6函数中事件的序列。纵列表示goroutine。窄线段代表sleep,粗线段代表活动。斜线箭头代表用来同步两个goroutine的事件。时间向下流动。注意main goroutine是如何大部分的时间被唤醒执行其range循环,等待worker发送值或者closer来关闭channel的。</p>
|
||
<p><img src="../images/ch8-05.png" alt=""></p>
|
||
<p><strong>练习 8.4:</strong> 修改reverb2服务器,在每一个连接中使用sync.WaitGroup来计数活跃的echo goroutine。当计数减为零时,关闭TCP连接的写入,像练习8.3中一样。验证一下你的修改版netcat3客户端会一直等待所有的并发“喊叫”完成,即使是在标准输入流已经关闭的情况下。</p>
|
||
<p><strong>练习 8.5:</strong> 使用一个已有的CPU绑定的顺序程序,比如在3.3节中我们写的Mandelbrot程序或者3.2节中的3-D surface计算程序,并将他们的主循环改为并发形式,使用channel来进行通信。在多核计算机上这个程序得到了多少速度上的改进?使用多少个goroutine是最合适的呢?</p>
|
||
|
||
|
||
<hr><table><tr><td><img width="222px" src="https://chai2010.cn/advanced-go-programming-book/css.png"></td><td><img width="222px" src="https://chai2010.cn/advanced-go-programming-book/cch.png"></td></tr></table>
|
||
|
||
|
||
<div id="giscus-container"></div>
|
||
|
||
|
||
|
||
<footer class="page-footer">
|
||
<span>© 2015-2016 | <a href="https://github.com/gopl-zh"> Go语言圣经中文版</a>, 仅学习交流使用</span>
|
||
</footer>
|
||
|
||
</main>
|
||
|
||
<nav class="nav-wrapper" aria-label="Page navigation">
|
||
<!-- Mobile navigation buttons -->
|
||
|
||
<a rel="prev" href="../ch8/ch8-04.html" class="mobile-nav-chapters previous" title="Previous chapter" aria-label="Previous chapter" aria-keyshortcuts="Left">
|
||
<i class="fa fa-angle-left"></i>
|
||
</a>
|
||
|
||
|
||
<!-- ../ch8/ch8-06.html -->
|
||
<a rel="next" href="../ch8/ch8-06.html" class="mobile-nav-chapters next" title="Next chapter" aria-label="Next chapter" aria-keyshortcuts="Right">
|
||
<i class="fa fa-angle-right"></i>
|
||
</a>
|
||
|
||
<div style="clear: both"></div>
|
||
</nav>
|
||
</div>
|
||
</div>
|
||
|
||
<nav class="nav-wide-wrapper" aria-label="Page navigation">
|
||
|
||
<a rel="prev" href="../ch8/ch8-04.html" class="nav-chapters previous" title="Previous chapter" aria-label="Previous chapter" aria-keyshortcuts="Left">
|
||
<i class="fa fa-angle-left"></i>
|
||
</a>
|
||
|
||
|
||
<a rel="next" href="../ch8/ch8-06.html" class="nav-chapters next" title="Next chapter" aria-label="Next chapter" aria-keyshortcuts="Right">
|
||
<i class="fa fa-angle-right"></i>
|
||
</a>
|
||
|
||
</nav>
|
||
|
||
</div>
|
||
|
||
<script type="text/javascript">
|
||
window.playground_copyable = true;
|
||
</script>
|
||
<script src="../static/wabook/mark.min.js" type="text/javascript" charset="utf-8"></script>
|
||
<script src="../static/wabook/clipboard.min.js" type="text/javascript" charset="utf-8"></script>
|
||
<script src="../static/wabook/highlight.js" type="text/javascript" charset="utf-8"></script>
|
||
<script src="../static/wabook/book.js" type="text/javascript" charset="utf-8"></script>
|
||
|
||
<script type="text/javascript" charset="utf-8">
|
||
var pagePath = "ch8/ch8-05.md"
|
||
</script>
|
||
|
||
<!-- Custom JS scripts -->
|
||
|
||
<script src="../static/wabook/giscus.js" type="text/javascript" charset="utf-8"></script>
|
||
|
||
|
||
</body>
|
||
</html>
|