读Go in action源码chapter1

本文通过一个简单的Go语言程序示例,介绍了如何使用goroutine和通道(channel)进行并发编程。包括如何创建goroutine、使用无缓冲通道传递数据、利用sync.WaitGroup确保所有goroutine完成等关键概念。

最近买了本Go in action学习,以此记录学习日志。

chapter1源码部分:

package main

import (
	"fmt"
	"sync"
)

var wg sync.WaitGroup

func printer(ch chan int) {
	for i := range ch {
		fmt.Printf("Received %d ", i)
	}
	wg.Done()
}

// main is the entry point for the program.
func main() {
	c := make(chan int)
	go printer(c)
	wg.Add(1)

	// Send 10 integers on the channel.
	for i := 1; i <= 10; i++ {
		c <- i
	}
	close(c)
	wg.Wait()
}

以上运行结果为

Received 1 Received 2 Received 3 Received 4 Received 5 Received 6 Received 7 Received 8 Received 9 Received 10 

分析:

一、

第1行:main函数保存在包里面,如果main函数不在main包里,构建工具就不会生成可执行文件。

              包的名字类似命名空间,可间接访问包内声明标识符,所有处于同一个文件夹里的代码文件,必须使用同一个包名,按照惯例,包和文件夹同名。

第3~6行:import导入一段代码,使其可以访问其中的标识符,如类型,函数,常量和接口。

第8行:声明一个sync包里的WaitGroup类型的变量,WaitGroup是一个计数信号量,可以利用它来跟踪goroutine是否完成。

 

二、

再看main函数(程序入口)

第19行:创建一个无缓冲的通道,接收匹配后的结果(使用make的同时声明并初始化该通道变量)

               在Go语言中,channel和map与slice一样,均为引用类型,通道本身实现的是一组带类型的值,这组值用于在goroutine之间传递数据,通道内置同步机制,从而保证安全。

第20行:使用关键字go启动一个goroutine ,一个goroutine是一个独立于其它函数运行的函数(与其他goroutine并行执行,同时也和主程序main并行执行),该goroutine调用printer函数。

第21行:设置需要等待处理,将WaitGroup变量设置为将要启动的goroutine的数量,每个goroutine完成工作后,会递减WaitGroup变量的计数值,当这个值递减为0时,就知道所有工作做完了。

第24~26行:将循环变量i写入通道

第27行:关闭通道。

第28行:等待所有任务完成,WaitGroup的Wait方法,会导致goroutine阻塞,直到WaitGroup内部的计数到达0.

三、

第10行:定义了printer方法,参数为一个int类型通道

第11行:for range会一直阻塞,直到有结果写入通道,一旦mian函数中close(c)关闭了通道,for range就会停止。

第14行:递减WaitGroup计数。

 

吐槽:还是在书上记笔记实在,写博客耗时间,囧。

D:\tools\python\python.exe D:\历史项目留存2\诺褀2025\python加工浦发模型模拟\py搭建\pythonProject1\爬虫抖音视频\测试01.py 小说下载器 - 自动翻页版 请输入小说起始URL: https://www.dbxsd.com/book/p23063/6291061.html 请输入输出文件名(默认: novel.txt): 2025-08-25 15:04:22,222 - INFO - 开始下载小说,起始URL: https://www.dbxsd.com/book/p23063/6291061.html 2025-08-25 15:04:22,223 - INFO - 基础URL: https://www.dbxsd.com 2025-08-25 15:04:22,223 - INFO - 正在下载第 1 章: https://www.dbxsd.com/book/p23063/6291061.html 2025-08-25 15:04:26,383 - INFO - 检测到编码: utf-8 2025-08-25 15:04:26,388 - INFO - 使用选择器: (‘div’, ‘content’) 找到内容容器 2025-08-25 15:04:26,398 - INFO - 通过分页区域找到下一页: https://www.dbxsd.com/book/p23063/6291061_2.html 2025-08-25 15:04:26,398 - INFO - 成功写入第 1 章内容 (1342 字符) 2025-08-25 15:04:26,398 - INFO - 正在下载第 2 章: https://www.dbxsd.com/book/p23063/6291061_2.html 2025-08-25 15:04:30,218 - INFO - 检测到编码: utf-8 2025-08-25 15:04:30,223 - INFO - 使用选择器: (‘div’, ‘content’) 找到内容容器 2025-08-25 15:04:30,226 - INFO - 通过JavaScript变量找到下一页: https://www.dbxsd.com/book/p23063/<a href= 2025-08-25 15:04:30,227 - INFO - 成功写入第 2 章内容 (65 字符) 2025-08-25 15:04:30,227 - INFO - 正在下载第 3 章: https://www.dbxsd.com/book/p23063/<a href= ================================================== 下载完成! 共 3 章,1407 字节 耗时: 12.07 秒 文件已保存至: D:\历史项目留存2\诺褀2025\python加工浦发模型模拟\py搭建\pythonProject1\爬虫抖音视频\novel.txt 2025-08-25 15:04:34,294 - ERROR - 错误: 无法获取页面,状态码: 404 2025-08-25 15:04:34,294 - WARNING - 检测到重复URL: https://www.dbxsd.com/book/p23063/<a href=,跳过 2025-08-25 15:04:34,294 - INFO - 已保存到: novel.txt 2025-08-25 15:04:34,294 - INFO - 总章节数: 3 2025-08-25 15:04:34,294 - INFO - 总字数: 1407 字节 2025-08-25 15:04:34,294 - INFO - 小说下载完成! Process finished with exit code 0 这个是翻页以后的页面源码 <!DOCTYPE html> <html lang="zh-cn"> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>第339章 嘴巴里的出口(第2/2页)-《我的治愈系游戏》免费在线阅_独步小说网</title> <meta name="keywords" content="独步小说网,我的治愈系游戏,第339章 嘴巴里的出口(第2/2页) 在线阅"/> <meta name="description" content="当前章节:第339章 嘴巴里的出口(第2/2页)"/> <link href="/static/css/bootstrap.min.css" rel="stylesheet"> <link href="/static/css/font-awesome.min.css" rel="stylesheet" > <link href="/static/css/base.css" rel="stylesheet"> <script src="/static/js/action.js"></script> <!--[if lt IE 9]> <script src="/static/js/html5shiv.min.js"></script> <script src="/static/js/respond.min.js"></script> <![endif]--> <script type="text/javascript"> var book = new Object(); book.id="23063"; book.name="我的治愈系游戏"; book.author="我会修空调"; book.url="/book/p23063/"; book.chapterName="第339章 嘴巴里的出口(第2/2页)"; book.chapterUrl=window.location.href; book.chapterId="6291061"; </script> </head> <body> <div class="top"> <div class="container"> <div class="logo-name"><a href="/">独步小说网</a></div> <div class="float-right"><script>play("lan")</script></div> </div> </div> <div class="container"> <div class="search row"> <div class="col-sm-4 col-xs-12"> <form id="search-form" action="/plus/search.php"> <div class="input-group"> <input type="text" class="form-control" placeholder="请输入书名" id="bdcsMain" name="q" /> <div class="input-group-addon" onclick="search();"><span class="fa fa-search"></span></div> </div> </form> </div> <div class="col-sm-2 hidden-xs"> </div> <div class="col-sm-6 hidden-xs"> </div> </div> </div> <div class="container"> <div class="book-type t10"> <ol class="breadcrumb hidden-xs"> <li><a href="//www.dbxsc.com"><i class="fa fa-home"></i> 首页</a></li> <li><a href="/book/p23063/">我的治愈系游戏</a></li> <li class="active">第339章 嘴巴里的出口(第2/2页)</li> </ol> </div> <div class="cont-set clearfix"> <script>bookset();</script> </div> <!-- Content Begin--> <div class="content" id="content"> <h1 class="cont-title">第339章 嘴巴里的出口(第2/2页)</h1> <div class="row"> <div class="col-md-3"></div> <div class="col-md-6 text-center"> <a href="/book/p23063/6291061.html"><button type="button" class="btn btn-info"><i class="fa fa-arrow-circle-left" aria-hidden="true"></i> 上一页</button></a> <a href="/book/p23063/"><button type="button" class="btn btn-info">回目录</button></a> <a href="/book/p23063/6291062.html"><button type="button" class="btn btn-info">下一章 <i class="fa fa-arrow-circle-right" aria-hidden="true"></i></button></a> </div> </div> <div class="page"> <ul class="pagination pagination-sm"> <li><a href='6291061.html'>1</a></li><li class="active"><a>2</a></li> </ul> </div> <script>play("code-1");</script> <div id="cont-body" class="cont-body 6291061"> <script>play("code-2");</script> <p>“这就是出口?”</p> </div> <div class="clear"></div> <div class="note"><script>play("note");</script></div> <script>play("code-3");</script> <div class="page"> <ul class="pagination pagination-sm"> <li><a href='6291061.html'>1</a></li><li class="active"><a>2</a></li> </ul> </div> <script>play("code-4");</script> <div class="row t10"> <div class="col-md-3"></div> <div class="col-md-6 text-center"> <a href="/book/p23063/6291061.html"><button type="button" class="btn btn-info"><i class="fa fa-arrow-circle-left" aria-hidden="true"></i> 上一页</button></a> <a href="/book/p23063/"><button type="button" class="btn btn-info">回目录</button></a> <a href="/book/p23063/6291062.html"><button type="button" class="btn btn-info">下一章 <i class="fa fa-arrow-circle-right" aria-hidden="true"></i></button></a> </div> </div> <script>play("code-5");</script> </div> <!-- Content End--> </div> <!-- Footer Begin --> <div class="footer"> <div class = "container"> <p class = "hidden-xs">如果版权人认为在本站放置您的作品有损您的利益,请发邮件至<script>play("email")</script>,本站确认后将会无条件删除。</p> <p class = "hidden-xs">本站所收录作品、社区话题、书库评论均属其个人行为,不代表本站立场。</p> <p>有能力者,请一定订阅和购买正版书籍支持作者,这样作者才能写出更多更好的书!</p> <p class = "hidden-xs"> <script>play("tongji")</script> </p> </div> </div> <div class="fixed-btn"> <a class="go-top" href="javascript:void(0)" title="返回顶部"> <i class="fa fa-angle-up"></i></a> </div> <!-- Footer End --> <!-- jQuery (necessary for Bootstrap's JavaScript plugins) --> <script src="/static/js/jquery.min.js"></script> <!-- Include all compiled plugins (below), or include individual files as needed --> <script src="/static/js/bootstrap.min.js"></script> <script src="/static/js/comm.js"></script> <script type="text/javascript"> var shortname = ".html"; var npage = 2; var totalpage = 2; var namehand = "6291061"; var displaytype = "st"; var nexturl = "<a href='/book/p23063/6291062.html'><button type='button' class='btn btn-info'>下一章(→)</button></a>"; var preurl = "<a href='/book/p23063/6291060.html'><button type='button' class='btn btn-info'>(←)上一章</button></a>"; var muluurl = "/book/p23063/"; var cid = "6291061"; var sid ="23063"; play("flow"); </script> <script src="/static/js/article.js"></script> <script src="/static/js/up.js"></script> </body> </html>
最新发布
08-26
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值