Unlimited Plugins, WordPress themes, videos & courses! Unlimited asset downloads! From $16.50/m

Next lesson playing in 5 seconds

Cancel
  • Overview
  • Transcript

1.3 Installing Go

Before you can actually write any Go code, you will obviously need to install support for it on your platform. The nice thing about Go is that it has versions for Windows, OS X, and Linux. So whatever type of operating system you are running, Go has you covered. In this lesson, I’ll show you how to get set up.

Related Links

1.3 Installing Go

The first thing that we're gonna do is actually have to download and install support for Go on our machine. Now there are a number of different package installation programs on a number of different platforms out there. And if that's the method that you would like to use to install Go then by all means you can absolutely do that. But I'm gonna keep it fairly straightforward and use the lowest common denominator by heading over to golang.org. Now over on the right side of the screen you're gonna see this Download Go button. I'm simply gonna click on that, it's gonna take me to the Downloads page. Now on this page you're gonna see there is support for a number of different platforms. So if you're following along on Windows or Linux, that's absolutely fine. You can use one of those installers. But for me I'm using Apple Os X. So what you would simply do is click on whichever platform you need. In my case, like I said, it would be Os X, and it would download and install or package for you. Now for the most part, these installation packages are fairly straightforward and are going to install everything that you need to get up and running. Including path environment variable changes and things like that so that you can reference Go from anywhere on the command line. So once you have gone through that installation process, we need to verify that we have Go installed. And we need to make sure what version that we are currently running. Now the most current version as of the time of this recording is 1.7.1. So I would always recommend to use the latest and greatest version when it comes to these types of things. But if you're somewhere close to that, then you should be able to follow along just as easily. Because a lot of the things that I'm going to talk about are going to be very fundamental aspects of the language and the standard library. And it's not really gonna change very much. If it does, then you obviously will need to look up some of the documentation to figure out what changed and maybe upgrade to the latest version. But for the most part you should be able to follow along fairly simply. So in order to verify what version and if we have everything installed correctly, I'm simply going to open up my terminal as you can see here. And I'm simply gonna type in the command go version. Now if you have everything installed at this point, then you should get back a response that says go version and then the version that you are running. And as I mentioned before, I'm running the latest version, which is 1.7.1. So if you have this installed and you see this, then you're ready to go. So the first thing that we should probably do then is talk a little bit about the structure of Go applications and how we can actually start to build very simple applications. And show you the structure of how we're going to be doing this going forward. So the way that we're gonna do that is I'm simply going to open up my text editor, and as I mentioned before I'm using Atom. And we need to start specifying what it is we're trying to do or go over the structure of a very simple Go application. So the first thing that I'm gonna do is I'm actually gonna specify that this is Go, since Atom does have syntax highlighting for the Go programming language. So now at this point I can start to write my application. So let's go through something very simple and very boilerplate. Now the beginning of every Go application needs to start with a package declaration. And a package declaration is one of the ways that Go structures code and is able to almost namespace it, if you want to call it that way. So you can kind of keep things separate in different locations, logically speaking. So that you can reference different functions or things that you might be exporting to make available through libraries or something like that. So the first thing that we need to do is specify a package and we need to give it a name. Now we're writing an executable program right now. Now you can also create libraries, but we're gonna save that for another course. When you're creating an executable application, you are going to specify that your package is main. So this is a requirement of Go, that every executable application begins with a package main so that we know that this is the entry point of our application. Also, a requirement of creating applications or executables within Go is we need to have a main function. And we're gonna go through functions in a lot more detail later on, but for now just kind of follow along. So functions are declared within Go very similarly to how they are in JavaScript with the func keyword. And then we need to specify a name as well as an argument list. So the entry point function for all Go programming applications is going to be main. Now we're not gonna pass in any parameters at this point, so we're just gonna leave open and closed parentheses. And then we're going to do open and closed curly brackets. And now this is gonna be the main skeleton that you're gonna see over and over throughout this course. You're gonna have a package declaration of main and then you're gonna have some sort of main function. And then from there on that's where things start to stray, and we'll go over a lot of different things as we go. Now there are some global pieces of functionality that we can go through and use within the Go programming language in an application like this. Things like print line, there is a print line kind of global method or global function if you want. But what you're gonna see very often is that we are going to be needing to import libraries to use reusable functionality. And I'm gonna show you how to do that just because we're gonna be doing that for the rest of the course. So the way that we're going to import packages is through the import keyword. And then we're gonna use open and close parentheses on separate lines like this. Now one thing you're going to know when you start to build and compile Go applications is that the compiler is very picky. If you don't specify a main package, it's going to complain. And a lot of things you're gonna see throughout this course that might be warnings with other compiled languages are actually errors. And will cause build failures in the Go programming language, which I happen to think is a very good thing. So we're gonna import a very well known package called fmt. So depending on who you're listening to or what you're reading, you might see this referred to as format or you might hear this referred to as [SOUND]. But either way, I'm gonna refer to this as format, and this is a very useful library In order to be able to print things to the screen. So in my main function I simply want to print a hello world string so that we can see that our application is actually running. So I'm going to say fmt.printline. So PrintLn is kind of the basic print line function. So I can enter something in here, some string, and print it out to the screen. So I'm simply gonna say, hello world, just like that. So now I'm gonna save this and I want to save this into some sort of directory. I already have a Go directory here, but you can save it wherever you would like. So I'm gonna save this as helloworld.go. So go is kind of that extension that's going to identify Go programming application files. But you could save them as other things if you would like. But I would definitely stick with the standard of helloworld.go. So from this point now we have our source code, but we need to compile it. Cuz remember, Go is a compiled language. So we need to build our source code. So I'm gonna come down to my command line. You can do this through extensions on your text editor if you have them. Or you can simply go over to a terminal and do the same thing. I'm gonna call go build and I wanna build HelloWorld.go. And I'm gonna hit Enter. Now if everything goes well, you should see nothing show up after that. If there are problems with formatting or syntax, you will get errors down here, which are fairly descriptive, which we'll see later on. So now from this point, as you can see, I have a hello world executable here. And if I do an ls command here, you're gonna see that I have my executable application. And I'm simply going to run that now with helloworld. So if I do that, you're gonna see the string hello world printed out to the screen. And now you have successfully written your first Go application. So we've gone through the process of installing Go and writing our very first application, but this is very simple. And we need to learn a few of the basics of the language before we can start diving into something like building a web server. And ultimately creating some sort of web application. And that's where we're gonna start next.

Back to the top