Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Preview
Start a free Courses trial
to watch this video
A "goroutine" is a simple way to make several function calls simultaneously. The work gets split up among all your CPU cores, and they all work on it at the same time.
- A goroutine is a simple way to make several function calls simultaneously. The work gets split up among your CPU cores, and they all work on it at the same time.
- A Go program starts with a single goroutine, which runs the
main
function
package main
import (
"fmt"
"time"
)
func longTask() {
fmt.Println("Starting long task")
time.Sleep(3 * time.Second)
fmt.Println("Long task finished")
}
func main() {
longTask()
longTask()
longTask()
}
- The first
longTask
call runs, sleeps for 3 seconds, and then returns. Then the secondlongTask
call runs, sleeps for 3 seconds, and so on. Because the calls tolongTask
run one at a time, the program takes just over 9 seconds to complete. - Prepend the
go
keyword to a function call to launch another goroutine, which runs alongside the first.
go longTask()
go longTask()
go longTask()
- Now calls to
longTask
run in separate goroutines. After each goroutine kicks off, it goes back to themain
goroutine and launches the next goroutine. - The problem is that as soon as the
main
goroutine finishes, the program exits. So the other goroutines don't get a chance to do anything. So just as a quick fix, we'll add a call toSleep
to themain
function:
go longTask()
go longTask()
go longTask()
time.Sleep(4 * time.Second)
More info
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign upRelated Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
[MUSIC]
0:00
Say we have a function that averages
three seconds to complete, and
0:04
we need to run it three times.
0:08
If you made just one
function call at a time,
0:10
it would take around nine seconds,
but that's wasting computing power.
0:12
Your CPU probably has
multiple cores on it.
0:16
A simplified way to think of this
is like having several computers
0:19
inside your computer.
0:22
And if you're making just
one function call at a time,
0:24
only one of those
computers is getting used.
0:27
A go routine is a simple way to make
several function calls simultaneously.
0:30
The work gets split up
among those CPU cores.
0:34
And they all work on it at the same time.
0:37
So if your CPU has at least three cores
we can make our three function calls to
0:39
our three second function simultaneously,
0:44
meaning our program completes in
only three seconds instead of nine.
0:46
Go routines can help you even
if your CPU only has one core.
0:50
There are lots of tasks for which your
programs have to just sit around waiting,
0:54
like waiting for network connections.
0:57
Instead of waiting for
1:00
the connection to complete, your program
will be able to start several at once.
1:00
Then deal with each
connection as it's ready.
1:04
Go routines and
concurrency are deep topics, and
1:07
we're only going to skim the surface here.
1:09
Check the teacher's notes for
more info if you'd like to dive deeper.
1:12
Here we have a long task function
that simulates a task that takes
1:15
a long time to complete.
1:19
It does this by calling the Sleep
method from the time package,
1:20
which causes your program to pause for
a specified interval.
1:23
A go program starts with a single go
routine, which runs the main function.
1:27
In that go routine here in this program,
the first longTask call runs, sleeps for
1:31
three seconds and then returns, then
the second longTask call runs, sleeps for
1:36
three seconds and so on.
1:41
Because the calls to
longTask run one at a time,
1:42
the program takes just over
nine seconds to complete.
1:45
You can prepend the go keyword to
a function call to launch another go
1:48
routine, which runs alongside the first.
1:52
So let's prepend go to each of these calls
to longTask to run it as a go routine.
1:55
Then we'll run our program.
2:04
Now the calls to longTask
run in separate go routines.
2:06
After each go routine kicks off,
it goes back to the main go routine and
2:09
launches the next go routine.
2:13
But you can see that our program's
not producing any out put now.
2:15
The problem is that as soon as the main
go routine finishes, the program exits.
2:19
So the other go routines don't
get a chance to do anything.
2:23
So just as a quick fix, we'll add a call
to sleep to the main function as well.
2:26
So we'll say time.Sleep, and
we'll have this sleep for for seconds.
2:31
So 4 * time.Second.
2:37
We'll run this again.
2:42
And now while the main
go routine is sleeping,
2:44
the other go routines are able
to start and finish running.
2:47
Since they all start around the same time,
2:50
they all finish in just
over three seconds.
2:52
And the main go routine finishes
in just over four seconds.
2:54
Of course, in the real world
you won't know exactly how long
2:58
a go routine will take to complete.
3:01
So adding a call to sleep in
the main go routine isn't ideal.
3:03
It might sleep for too long or
it might not sleep for long enough.
3:06
Up next, we're going to look at channels.
3:09
Channels are a solution that let
you wait for a go routine and
3:11
give you a way to communicate
between go routines as well
3:15
You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up