Skip to Content
Head First Go
book

Head First Go

by Jay McGavren
April 2019
Beginner
556 pages
14h 21m
English
O'Reilly Media, Inc.
Content preview from Head First Go

Chapter 13. sharing work: Goroutines and Channels

image

Working on one thing at a time isn’t always the fastest way to finish a task. Some big problems can be broken into smaller tasks. Goroutines let your program work on several different tasks at once. Your goroutines can coordinate their work using channels, which let them send data to each other and synchronize so that one goroutine doesn’t get ahead of another. Goroutines let you take full advantage of computers with multiple processors, so that your programs run as fast as possible!

Retrieving web pages

image

This chapter is going to be about finishing work faster by doing several tasks simultaneously. But first, we need a big task that we can break into little parts. So bear with us for a couple pages while we set the scene...

The smaller a web page is, the faster it loads in visitors’ browsers. We need a tool that can measure the sizes of pages, in bytes.

This shouldn’t be too difficult, thanks to Go’s standard library. The program below uses the net/http package to connect to a site and retrieve a web page with just a few function calls.

We pass the URL of the site we want to the http.Get function. It will return an http.Response object, plus any error it encountered.

The http.Response object is a struct with a Body field that ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Head First Git

Head First Git

Raju Gandhi
Learning Go

Learning Go

Jon Bodner
Head First Software Architecture

Head First Software Architecture

Raju Gandhi, Mark Richards, Neal Ford
Head First Design Patterns, 2nd Edition

Head First Design Patterns, 2nd Edition

Eric Freeman, Elisabeth Robson

Publisher Resources

ISBN: 9781491969540Errata Page