Lesson Description

The "Installing & Running Go" Lesson is part of the full, Complete Go for Professional Developers course featured in this preview video. Here's what you'd learn in this lesson:

Melkey walks through installing Go and configuring both VS Code and Neovim. Installing a Go extension or LSP provides helpful language support and will catch compiler errors ahead of time.

Preview
Close

Transcript from the "Installing & Running Go" Lesson

[00:00:00]
>> Melkey: This section is gonna be introducing the data structures of Go and how we can spin a project up. What does Go mod init mean? What's the entry point of function, arrays, slices, we'll go through all of that in this section, and then we'll start to build off of that knowledge in implementing it with a project.

[00:00:18]
So there's this website called go.dev. This is the official Go website. And you go into /doc/install, and you click this big blue button that says, Download. The version of Go that's currently available is version 124.1. Now, if you don't have this exact version, it's actually okay. Go is very boring, in the sense that the majority of features that individuals use are actually the same features that have been in development for five plus years now.

[00:00:53]
Of course, there's new features like, Go 121 and 123 introduced like iterators and generics, I think we're introducing Go 118. And these are helpful tools as well, of course. But typically, having a go version that's patched version or whatever, out of date, it's not gonna be the end of the world.

[00:01:12]
So depending on what operating system you are on, there's Linux, Mac, and Windows. Go ahead and click the button, follow the instructions. It should be pretty, relatively straightforward. I'm on Mac, so I followed the Mac instructions, and I have it installed. So we can open up a terminal, all right?

[00:01:28]
So here, I have this empty terminal, well, there's this one folder, but we can ignore it. Just remove it, okay? So here we can check the Go version, like so. So I'm running on 123.2. This is completely fine. And the first thing I'm going to do is make a directory.

[00:01:45]
So let's just do mkdir, make directory, and let's call it beginner_go, all right, stuff like that. Let's cd into it. And so here, now this is truly an empty director. There's nothing in here at all. When you start a Go project, like I said, there's no boiler plate.

[00:02:01]
So we're not gonna go to GitHub, repo, get cloud, none of that. We're doing this from the ground up, sticks and stones, all right? What you wanna do is do a go mod init, and then the following, after the init is the name of the module, the name of your project, essentially, okay?

[00:02:17]
Now, before I type this one in, I wanna explain what that is in just a little bit. So I'm going to open up one of the projects I mentioned earlier, Go blueprint. This is obviously a public facing project, right? It's on GitHub, everyone can see it, everyone can use it, right?

[00:02:33]
Now, the Go mod init, the module name for this follows a very particular format. Now, if I go into this go.mod file, I'll explain what that is in a second, you can see the module name is right here, right? It's module, and then the name of it. So the pattern for something that you intend to be public-facing used by other engineers, other products, other software, or whatever, is the name of where this project is hosted, the auth or org name.

[00:03:06]
And then the name of the project itself. So following that pattern, this is github.com, because it's hosted on github.com melkeydev, I was the sole author at the time of creating this. And then Go blueprint, the name of the actual project itself. And this module, this kind of concatenation is how this project is identified in kinda the Go ecosystem.

[00:03:30]
Even though its name is Go blueprint, right, and like, okay, it's probably one Go blueprint, maybe another, or whatever, the full kind of idea of it is, the whole string here. And why am I emphasizing this? This module name is how the code is known for other people to use it and put it into their project if they want, but it's also how it is identifiable to itself within the scope of the project.

[00:03:57]
Go doesn't have the concept of relative paths to plug in when you wanna import something from a utils package into something, all right? It has this notion of basically pulling in from packages. And these packages are identifiable. And when we create a Go script, there's this package identifier, which could be like package utils, package routing, etc.

[00:04:21]
And how you can pull that into any location within your project is by referencing the full module name, and then where that package exists. I'm gonna show this in more depth, but I wanna again, emphasize what the module name is. So before you do a go mod init back here, maybe sometimes think about it.

[00:04:42]
You can always change it, it's not like a zero sum game that you've made it, you can't go back. You can obviously do that. But if you know something is gonna be used by other people, you can follow that pattern. It's kind of, the accepted pattern here. So for us, in this portion, we do, go mod init, we just call it a BeginnerGo.

[00:05:02]
So, beginner, and then GO, all right? And then you can see, LS, we have this, go.ModFile. Next, we're gonna go ahead and create touch main.Go, all right? Now you should have this, go.Mod, and your main.Go file here. So here at this point, we've installed go, we have these two files.

[00:05:23]
I'm going to briefly show you what the setup looks like for VS Code, but for the following course and the remaining of course, I'm going to be using new of them, which I'll also show you how you can set that up. So if you are on VS Code, and I do apologize for anyone who maybe is on a different editor, couldn't cover all of them, all right?

[00:05:41]
For VS Code, it's fairly straightforward. When you open up your project, again, we're going to ignore all the compiler issues we see for just a second, you'll be prompted to install a extension. And you can see, if you go, there's this extension here by the Go team at Google.

[00:05:57]
If you just type in, Go into the Extension tab, if it doesn't pop up, you can go ahead and install it, and this is all you really need to run Go, truly, in VS Code, right? It's gonna format stuff that's gonna give you the LSP, it uses gopls.

[00:06:12]
You can see down here, the Go language server for a bunch of our tooling recommendation and hints. If you are on New Vim, or any kind of vim-configured editor. Obviously, this is a bit more involved, because what you have to do in that instance is set up the LSP yourself.

[00:06:41]
>> Melkey: Okay, so here,
>> Melkey: I have some core things for my plugins. You can see my LSP, right? Again, these are my settings, my configuration. It varies very, very vastly for however you set your services, right? I'm using Mason, so I have the ability to use Mason like so, and it pulls up all of these LSPs that I can install.

[00:07:09]
You can see, right here I have gopls installed. Which means whenever I open up a Go file, it's going to bind it to my language server protocol. And then we have gopls available, just like we saw in VS Code, okay? So again, it's gonna vary case by case on how you set up, but typically, for someone with VS Code with an extension marketplace or someone with something like new Vim, whatever.

[00:07:36]
That's typically the route to go about it.

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