惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

酷 壳 – CoolShell
酷 壳 – CoolShell
雷峰网
雷峰网
H
Hackread – Cybersecurity News, Data Breaches, AI and More
Spread Privacy
Spread Privacy
H
Hacker News: Front Page
PCI Perspectives
PCI Perspectives
Webroot Blog
Webroot Blog
罗磊的独立博客
H
Heimdal Security Blog
TaoSecurity Blog
TaoSecurity Blog
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
大猫的无限游戏
大猫的无限游戏
月光博客
月光博客
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
Google Online Security Blog
Google Online Security Blog
Last Week in AI
Last Week in AI
美团技术团队
Help Net Security
Help Net Security
The Hacker News
The Hacker News
C
Cisco Blogs
T
The Blog of Author Tim Ferriss
J
Java Code Geeks
The Register - Security
The Register - Security
IT之家
IT之家
WordPress大学
WordPress大学
Jina AI
Jina AI
Recent Commits to openclaw:main
Recent Commits to openclaw:main
H
Help Net Security
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
T
Threat Research - Cisco Blogs
P
Proofpoint News Feed
NISL@THU
NISL@THU
爱范儿
爱范儿
The GitHub Blog
The GitHub Blog
Scott Helme
Scott Helme
V
Vulnerabilities – Threatpost
B
Blog
T
Tenable Blog
博客园 - 三生石上(FineUI控件)
T
The Exploit Database - CXSecurity.com
S
Security Affairs
小众软件
小众软件
Hacker News: Ask HN
Hacker News: Ask HN
Security Latest
Security Latest
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
W
WeLiveSecurity
A
Arctic Wolf
L
LINUX DO - 热门话题
Google DeepMind News
Google DeepMind News
M
MIT News - Artificial intelligence

博客园 - 甩掉裤衩凭风吹

Go学习例子(六) Go学习例子(四) Go学习例子(三) Go学习例子(二) Go学习例子(一) 一、Python基础语法 Python爬虫实战一之爬取糗事百科段子 Python爬虫系列:五、正则表达式 Python爬虫系列:四、Cookie的使用 python爬虫系列:三、URLError异常处理 python系列:二、Urllib库的高级用法 python系列:一、Urllib库的基本使用 二十七、mysql如何确保数据不丢失?有几点值得我们借鉴 二十六、聊聊mysql如何实现分布式锁 二十五、sql中where条件在数据库中提取与应用浅析 二十四、如何正确的使用索引? 二十三、mysql索引管理详解 二十二、mysql索引原理详解 二十一、什么是索引?
Go学习例子(五)
甩掉裤衩凭风吹 · 2021-12-01 · via 博客园 - 甩掉裤衩凭风吹

21、Embedding

Go 支持嵌入结构和接口,以表达更无缝的类型组合

 

$ go run embedding.go
co={num: 1, str: some name}
also num: 1
describe: base with num=1
describer: base with num=1

22、Errors

在 Go 中,通过显式、单独的返回值传达错误是惯用的。这与Java和Ruby等语言中使用的异常以及C中有时使用的重载单个结果/错误值形成鲜明对比。Go的方法可以很容易地看到哪些函数返回错误,并使用与任何其他非错误任务相同的语言结构来处理它们。

errors.New使用给定的错误消息构造一个基本值。

$ go run errors.go
f1 worked: 10
f1 failed: can't work with 42
f2 worked: 10
f2 failed: 42 - can't work with it
42
can't work with it

23、Goroutines

Goroutine是一个轻量级的执行线程。

您还可以为匿名函数调用启动 goroutine。

现在,我们的两个函数调用在单独的 goroutines 中异步运行。等待它们完成(对于更可靠的方法,请使用WaitGroup)。

当我们运行这个程序时,我们首先看到阻塞调用的输出,然后是两个goroutine的输出。goroutines 的输出可能是交错的,因为 goroutine 由 Go 运行时同时运行。

 

$ go run goroutines.go
direct : 0
direct : 1
direct : 2
goroutine : 0
going
goroutine : 1
goroutine : 2
done

24、Channels

通道是连接并发沟槽的管道。您可以将值从一个 goroutine 发送到通道中,然后将这些值接收到另一个 goroutine 中。

使用 创建新频道。通道按其传达的值键入。make(chan val-type)

使用语法将值发送到通道中。在这里,我们从一个新的goroutine发送到我们上面制作的频道。channel <-"ping"messages

语法从通道接收值。在这里,我们将收到上面发送的消息并将其打印出来。<-channel"ping"

当我们运行程序时,消息通过我们的通道成功地从一个goroutine传递到另一个goroutine。"ping"

默认情况下,发送和接收块,直到发送方和接收方都准备就绪。此属性允许我们在程序结束时等待消息,而无需使用任何其他同步。

$ go run channels.go 
ping

25、Channel Buffering

默认情况下,通道是无缓冲的,这意味着它们只有在有相应的接收()准备接收发送值时才会接受发送()。缓冲通道接受有限数量的值,而没有相应的接收器来接收这些值。chan <-<- chan

在这里,我们一个最多缓冲2个值的字符串通道。make

由于此通道是缓冲的,因此我们可以将这些值发送到通道中,而无需相应的并发接收。

稍后,我们可以像往常一样接收这两个值。

$ go run channel-buffering.go 
buffered
channel