Go by Example - Timeouts
Go by Example
Go is an open source programming language designed for building simple, fast, and reliable software.
Go by Example is a hands-on introduction to Go using annotated example programs. Check out the first example or browse the full list below.
Go by Example: Timeouts
超时对于需要联接外部资源或需要限定执行时间的程序非常重要,在golang中因为存在channel
和select
使得超时实现起来非常简单。
1 | package main |
1 | tashuo:golang ta_shuo$ go run timeout.go |
使用select结构的超时模式需要借助管道进行通信,这通常是个好主意因为其他一些golang重要的功能也是基于管道和select。