Go: Setting Up a Code Editor
The Go language, like a lot of other programming languages, uses an editor to allow us to write our code and to interact with our programs as they're developing. And the basic schematic for this is we have us, as the developer, and then we're interacting with some sort of an editor, a code editor, which is basically a very powerful text editor. Think about a program that you might write a document in, write a report, or write a letter in. Well, instead of writing a letter, we're just writing source code, so that's basically what an editor is. And then on the other side of this, we have the Go language itself, so the editor has the responsibility of interfacing us with the Go language. So it allows us to write our code that interacts with Go, but it's also going to do a lot of things. It's going to allow us to interact with that Go Toolchain without having to actually go to a terminal every single time. In this blog we are setting Visual Studio Code. This is the most popular editor for Go code and has proven itself to be a very robust and reliable tool for creating Go programs, regardless of their size. Now, in between Visual Studio Code and the Go language, we have a couple of other things that we need to install. The first is something called an extension.
What is extension?
Visual Studio Code on its own doesn't understand Go code. It doesn't know how to work with Go, so we have to extend the Visual Studio Code editor with something called an extension that allows it to work with Go a little bit better.
Now, the funny thing is the extension itself doesn't even really understand how to work with Go. The extension, however, takes advantage of a collection of libraries, and it knows how to interact with those libraries, and those libraries each provide individual bits of functionality. So we might see that there's one library that's going to provide auto completion, so when we start writing our code, our editor is going to try and help us try and understand what we're writing, and it's going to provide us a series of options to automatically complete that code for us. Well, the extension doesn't know how to do that, and VS Code doesn't know how to do that, but there's a library that provides that intelligence that the extension and VS Code take advantage of. Fortunately, after you get it set up, most of this is behind the scenes, and you never have to think about it again.
Now, we understood this. We need to setup three things. We need to install Visual Studio Code, we need to install that extension, and then we need to install those libraries.
Install Visual Studio Code
Go to code.visualstudio.com and now to download it, we have this blue Download button right there, and it's going to try and select your operating system for you. It's going to try and detect what operating system you're on. As you can see, it's correctly identified that I'm running on Windows. If it does guess wrong, then you can click this down arrow and get access to manually select the installer that you need. And then each one of these is an installable package. So for Windows, it's going to give me an MSI. For macOS, it's going to give me a pkg, and then depending on which version of Linux I'm on, if I'm on Red Hat or Debian, you pick the right package and your package manager is going to install that application for you.
Install Go extension
Now, go to extension menu on left hand side. This is the Extensions manager that we have available in VS Code, so this is how we're going to add extensions to the editor. Now you can see I've got a couple of extensions available for installation, but I want to install the Go extension. So in order to find one, we can see text box above the available extension list that says Search Extensions in Marketplace. So I'm going to search for Go, and the first option that comes up should be this one here (selected one in list) that's sponsored by the Go Team at Google. So this is one of the neat things about the Go extension for Visual Studio Code is this is one of the few extensions that the Go Team itself actually manages. So anytime new features are added to the Go language, the most likely place for you to get access to those new features is Visual Studio Code. Now to install this extension, all I have to do is simply click on Install button(I have already installed so, now, text change to uninstall), and it will go through the installation process.
Install Go libraries
Go to the View menu on top and select first option Command Palette. If you select that, this is going to give you a list of all of the commands that Visual Studio Code already knows how to run. So Visual Studio Code is more than just a text editor, it offers us these capabilities to do all sorts of things with our programs, as you can see in below image. the command that I want to run right here is
Go: Install/Update Tools.
Now I've run that recently, so you see it's at the top of my list. You might need to type that in, and remember to use that colon, so you'll type
Go: Install/Update Tools.
When you run that, you're going to be presented with a menu that looks something like this.
This is going to be all of the libraries that Visual Studio Code would like to install on your system. The easiest way to get all of them installed, and they would encourage you to install them all, is select top checkbox and that's going to select all the libraries, and then select OK. And then an output window is going to open up in the bottom of your screen, and you're going to see as Visual Studio Code goes through the process of installing those libraries one at a time. So let's just give it a second to finish. And when it's done, you hopefully get the message All tools successfully installed. Now, we have our development environment set up. You are ready to Go.
Reference
Pluralsight- Go Fundamentals