Basics of Go


Maximiliano Firtman

Maximiliano Firtman

Independent Consultant
Basics of Go

Check out a free preview of the full Basics of Go course

The "Goroutines" Lesson is part of the full, Basics of Go course featured in this preview video. Here's what you'd learn in this lesson:

Maximiliano discusses goroutines, which are Go's way of utilizing threads, and demonstrates how to open a goroutine using the "go" prefix. This segment also covers delaying thread execution using time.Sleep.


Transcript from the "Goroutines" Lesson

>> Goroutines, we're going to make an intro on this, and later, we will do the large exercises on that. So, what's a goroutine? A goroutine is the Go way of using threads. You know that every time we get into threading, it's kind of a, [SOUND] I am not sure I wanna get into that, okay?

But because Go was created from scratch and was created in the modern way to solve the current problems and other problems that we used to have in the 80s or in the 90s, it's much simpler. How creating a thread works fact. Let me get out of this for a second.

Because we have here the information and some samples, but let's see this in action directly. For that, I'm going to create, let me clean this up first so it doesn't have any errors. I'm going to create another version. It's going to be a simple one, not a complicated one.

So, but just to play with core routines before actually using them for accessing the network. That's the next step. To use the HTTP package. So, in your project, open folder. We're going to play with Goroutines. I mean the name sounds scary, but no worries. It's actually pretty simple.

So I'm going to say here again as usual you know the deal out go mode init frontendmasters.comm/go/goroutines. Now may not go with a package main and a function main. You know the deal, simple. Now, let's say that we create a function here that will say, let's say print message.

That receives a message as text, And it just call format, print line, that text. But I'm going to create a for, okay? And we are going to use the for as kind of a while. Remember, also, I can use an infinite loop. Remember the infinite loop? Like so but we will see that later.

Because if it's an infinite loop, yeah, we have a problem. We know we have a problem, but we can say, okay, let's start with zero 0, let's do this 10 times, okay? And then let's call printMessage That says Go is great. Okay, so nothing really fancy. I'm going to execute this and I should see go is great 10 times.

Yeah, you were expecting that, nothing to worry about. So principle. So what's the deal here? What if we start waiting sometime because this is going really fast. So what if we start waiting some moments before doing something, for example, waiting a second, how to wait for something. So we need to work which we'll look for asleep.

And where do we get that? Where do we get to sleep is threading. So how can we get into something like that? So if we look in the documentation, so I'm not sure if you have experience working with synchronous apps locally. Typically, you have a way to slip the thread in any language.

So you ask the thread to wait. Let's wait one second, and the thread. It is blocked there for one second. It doesn't make sense, that's kind of it. So on Go, it's a time package from the standard library.sleep. The problem is that the sleep receives duration, okay, which is okay, okay.

Duration in which unit? I don't know, I can see the documentation. I can get into the sleep function to see if I get any information. First is a type, it's called duration. Okay, what is the type? Let's get in, init64, okay, so it's not an alias, it doesn't have equals, okay?

But it's expressing nanoseconds. Don't ask me why it's nanoseconds, but that's really precise, really high accuracy. So yeah, how many nanoseconds in a second? It's complicated, right? Anyway, so, fortunately, the time, the same time package has some constants express different units in nanoseconds. For example, hour, a microsecond, a millisecond, okay?

So, also there is a second somewhere. So, I can say, okay, 400 multiplied by that will give me 400 milliseconds. Okay, does it make sense? And if you wanna read the number, you can get in. And that's how you get you can get the minute the nanosecond. Anyway, so now we can let the thread asleep for 400 milliseconds, let's say a little more 800.

So now if I execute this, you can see the slowness of the code, okay? The problem is that, yeah, we're waiting 800 milliseconds but in the meantime we are doing nothing. We are not taking advantage of that time within each print us or something else. What if we wanna print another message like no, we miss classes.

Okay, well, you know what will happen, let's reduce this to 5 for now. You understand what's going on, I guess. That we need to wait for the first output to end to then see the rest. So we need to wait and then do another stuff. This is sequential.

It's not really optimized. But we can use Goroutine. So what's a Goroutine? A light-wave thread. It's a thread. What's a thread? Do you know what a thread? Does anyone know what a thread is? How can you define a thread? Execution thread at least.
>> Not really.
>> Not really.

That's a good definition, not really.
>> [LAUGH] A process that's running.
>> A process? Yeah, well, threads are running within processes. So a thread, it's the execution line and in modern operating systems, you can run more than one at the same time. Sometimes it's, they're being executed at the same time but that's kind of a flexible definition.

It depends on the west, it depends on if you're multicore, if they're actually really running at the same time or it's speaking from one or the other. Anyway, it feels like we have different lines of execution running at the same time. Well Goroutines are the way to do that here, but how that works, we just need to add Go, the Go prefix before a function call.

If you say Go and you call a function, it's going to create a new Goroutine. What's a new Goroutine? Kind of a thread. It's how go, is the Go response to threading. Okay, so if I say this one, not the second one, for example, look what will happen.

Just that, I don't need to create the structure a type, I just need to add Go as a prefix to any function code, any function code. So now if I run this, let's see if we can understand what's going on. So, it seems like it's doing both things at the same time, right?

Or kind of, because, I mean it's not completely clear what's going on, but at least we see that it's merging responses from different messages. Why is that? First, the main function is being executed in the main Goroutine. There is always a Go routine, it's called the main Goroutine.

It's the main function. Then from there, you can open more Goroutines just by adding Go, okay? Makes sense? So what happens if I want I can have more messages that say from the Masters. Rocks. And now we can see that everything is being executed at the same time.

Okay, makes sense? So in this case, it's opening three apps at the same time, and from now on, one execution goes in one Goroutine, the other one goes in another Goroutine. They're using the same shared space, which may lead to some problems with memory management. What happens if one of those Goroutines is saving a value on a variable that other Goroutines at the same time trying to read the value, okay?

You can lock and unlock and do things but there is a better way. There is a Go way to that. But before getting into that, go away, what happens if I add a Go to the last one because I didn't do that on purpose? You did that. What happens?

>> Nothing.
>> Nothing, and what nothing means?
>> Nothing printed.
>> Nothing printed, exactly, so let's try and see why. So if I add go there, let's see what happens, so I will clear the screen. I will run this and nothing happens. What's going on? And that leads to another question.

And what's the lifecycle of my app? When my app ends? When my Go app ends? My app ends when the main Goroutine ends. When the main Go in, the routine ends when it reaches the end of the main function. Now I'm opening three more Goroutines. So I have the main goroutine and three more goroutines that they are doing their job.

But the main one has nothing else to do after that. So the whole process end, okay? Does it make sense? So to prove that, let's do this. What is that? An infinite loop. We keep that main thread busy doing nothing? Let's see if that works. At least I can still see seeing the rest of the outputs and now we will be there forever.

How to end up this control C, on every operating system you can interrupt Go so far with Ctrl C. But at least we understood the problem, okay, the problem is that we need to keep the main go routine busy, at least until the rest are ready because when we open a Goroutine is not a different process.

It's working under my process by a different thread, if the main process ends that everything is killed, okay? We will see different solutions to this problem to avoid using these also you can sleep on the main thread, so time sleep. You can sleep but how much time? Yeah, you don't know mean so it's not like the best of the solutions, but you can say a minute.

So now it will wait a minute and something like that it will work. Also but yeah, it's kind of a still not the best of the ideas. So later we will talk about if there is any way that we can create some kind of signup, we know that we know when they Goroutine, it's ending something, and doing something, and so on.

But before getting into that, and by the way, if a function is calling other functions, for example, if this function, if this one is calling a So when you open a Goroutine, everything that is being executed from that line will be executed in that Goroutine. So when you call a, you don't need to add Go again.

If you do that, you are opening another Goroutine, okay, so no need for that. Just to call the function, we actually continue on that Goroutine, and each Goroutine will have its own code. But have in mind that if you're accessing global variables, package variables, they are all the same.

It's not creating copies of the app in memory. It just has to do with execution not with memories.

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now