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

The "Go Setup" 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 walks through selecting the appropriate Go extension for Visual Studio Code, checking Go installation, and creating a basic stand-alone Go script. The discussion also covers how to run the Go script from the terminal.

Preview
Close

Transcript from the "Go Setup" Lesson

[00:00:00]
>> Going back here, I'm in Visual Studio Code, copy the empty. First, the plugin area is this one and you search for go. Typically the first one that you will find is the official one, and you will see it's the official one because it's coming from the Go team at Google.

[00:00:15]
That's the official one. You just install that. The first time you create a Go file, you that by the way, it's a file with the extension go.go. So the first time you open one, it will ask you to download those additional tools, such as the linter. Typically it works, it's straightforward.

[00:00:34]
It's fast also, it's not like a lot of things that has to be downloaded. Also you can download, if you want, Go Nightly. This is like experimental. So in case you wanna try like the non-stable version of the plugin that comes with more features. We are not going to do that for the course, but if you are brave enough, you can try that one.

[00:00:58]
And there are more plugins from the community for specific things. But I think that with the standard one, we're good, okay, we're ready. How do we know if Go is installed and working properly in our computer? You can open a terminal, new terminal, or use a shortcut in case you are used to VS Code, and you type go and you should see something.

[00:01:22]
An output from the library that will explain everything that you can do with the CLI. So Go is a tool for managing Go source code. If you try go space version, it will give you the version that you have. I mean, 120, okay. So which version are we going to use in case you have an older version of your computer?

[00:01:44]
At least I think you should have 118. Most of the code will work with probably 110, okay. Anyway, you can download the latest version. It's not going to harm your computer. Okay so that means that you have that and we are ready to write the new file just to play with this initially.

[00:02:06]
So I'm going to create a new file save it somewhere my desktop for example first dot.go. And when you use a .go now you will see the logo there in VS code. That means that the plugin is working. If you see the logo there also at the bottom of VS code, look at the bottom left of your screen.

[00:02:30]
You will see the version of code that VS code is recognizing. In my case, it says 1.20. Okay, so that's recognizing the go runtime running your computer. By the way, I didn't open a folder or create a folder yet. We'll get into that in a minute. I want to first show you how, like, stand alone scripts work like if you had a dog.go file somewhere, if you can run in before grading actually oppression.

[00:03:01]
So 99.9% of the time you will create a project that by the way is not named as project. It's called a module or workspace. We'll get into that but go at least the runtime let us just run standalone scripts to see how a script that is doing something, but you can run it as a Python script, as a Perl script that you have in your computer.

[00:03:29]
But maybe you're thinking, hey, but you said that we have to compile it, yeah, we can compile it and run it, or we can run it directly that is actually making a compilation on the fly in memory. No worries give me a second. First, I have an empty file, and it's giving me an error.

[00:03:47]
How do I know? Because it's red. When you see red in the file name, it's because it's not following Go rules. Where are the rules? Well, every file must be within a package. What's a package? For now, let's say that the package is a folder, okay? And there is one package that doesn't need to be a folder, only one.

[00:04:13]
And you must have at least that package in your Go file and that's package main. Now, you can see that after I wrote package main, I don't see red anymore. So now, I'm still on an empty Go file, but now it's valid. In fact, what we have on Go is an entry point.

[00:04:36]
For those of you that are used to creating no js applications or even front end applications, it's difficult to understand what that end what the entry point is. I mean, no Jas, yeah. Maybe you have like a server.js file like, it's like the entry point. When you deploy that somehow you need to tell the cloud.

[00:04:57]
Where you are deploying that, which is your file, your entry point. If you are doing front-end, you don't have an entry point. I mean, you have a JavaScript file and you have code. And what's the entry point? Just executing the code, the first line of that code. But because here we are doing apps, we actually need to have an entry point.

[00:05:19]
The entry point for now is to create a function with the name main. So for every Go solution that we are creating, we will have a file that starts with package main at the top, and it has a function that they mean. Give you a spoiler on how to create a function.

[00:05:37]
We haven't seen that yet, but it's func. The keyword is func, space, increase that a little bit main. We use parentheses, and then we use a code block. Code block calibrates, that's all. That's a valid Go file now. It has an entry point. So the file is first let go.

[00:06:00]
The package is main. The function is also called me. So main and main are mandatory the name of the file. It's up to you. And for now just to see if this is working, we are going to print a message so I'm going to call the function with an imprint will explain functions and everything in a second, I will use double quotes to send a message.

[00:06:22]
Maybe some of you are thinking, do we have semicolons? Do I need to add a semicolon? Give me a minute, we will get there. But for now, I can save the file and you don't see a semicolon, you don't see an error, so it seems like we don't need one.

[00:06:34]
That's the file. How do I run the file? How do I execute the file first? This is not for those of you that are well developers. We don't put this into an HTTM L, or we don't need a container. We need to run it as a console software that we have in our computer.

[00:06:56]
So I need to open the terminal. The problem is that on the terminal I need to get into the right folder because I didn't open a folder. That's the next step actually. My terminal is actually in any place. So I need to get in. I think I saved that into my desktop.

[00:07:14]
So if I'm, I'm seeing the file is because I'm in the right folder. If you just go space, run space, the name of the file, and I'm sorry, we need to also use the extension. So first.go, so first alone is not going to work. It will say no, I can't find that app.

[00:07:39]
But if you say first.go it will say hello from go they said we our character there that we'll see what that is later but that's our first go script running did they compile it no not really I just running. In this case, this is a special condition that is not common.

[00:08:03]
The compiler will compile that on the fly in the temporary directory, and it will execute it and then delete the temporary directory. So it's actually compiling it before executing it. Okay, this is just for development purposes. When you deploy go application, you don't deploy the source code, you deploy the binary that you need to compile, okay?

[00:08:26]
And we will see that later, but well, you can ask help. Just write in, go, and you will find here all the possibilities that you have, all the burps that you can use, and there is a build command. So then you can use go build. And if you say like that it says, I need to know what your bill.

[00:08:44]
Okay, and then you pass more data. So we have files.go that we can run. And we need at least a package main and a function with the name main. That's the first step.

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