Channels in Go
Through goroutines
and channels
, Go offers constructs for concurrent programming. A goroutine
is a concurrent function execution, while a channel
offers a communication mechanism through which one goroutine
can pass values of a specific type to another goroutine
.
For example, the following code performs three HTTP requests concurrently, reports back the request URL, the request response time, and its HTTP response status code for each request, and also the total time spent executing the program. In this example, the channel
receives a string summarizing the URL, response time, and status code details for each concurrent request, while the main
function prints each string sent to the channel
. The code illustrates that the total time spent executing the program is less than the sum of the times spent waiting for each individual HTTP response, as the HTTP requests are performed concurrently.
|
|
Example output:
$ go run fetch.go
http://mikeball.me 0.26s 200
http://mikeball.info 0.38s 200
http://github.com/mdb 0.53s 200
Total time: 0.53s