Let's start at the beginning and write our first Go program. We'll also learn some tips and trick for the GoLand IDE
Welcome back. So we're back in Goland and we're back inside the program that we created last video. You may have called it something else, it's absolutely fine.
So the first thing you'll notice is, at least for the video, my ID is probably a little bit small, maybe you're struggling to read the text. So I'm just going to zoom in for now so you can just press help and look for zoom and you can press zoom ID and I'm going to zoom in to like a hundred and seventy five percent.
So I don't usually operate like this, but I think it's much better for videos. Something else that I'll call out while I'm here is there is actually something called presentation mode. And this is really useful for if you are doing conference talks or anything like that. So you can bring up like a bigger screen without all the distractions on it. So it's really great for presenting code. You might use this in meetings at work too.
So I'm just going to exit this by clicking the settings icon, clicking view mode and exit presentation mode, but we'll keep the 175 percent zoom for now. So as I mentioned in this video we're going to make our first Go program. So what I'm going to do is I'm going to go over to the left hand bar here awesome project and I'm going to right click and put new and go file.
I'm going to call this main.go. Every single Go project starts with a main.go file so anything that's executable it must have a main.go file so this is really nice for if you end up looking at someone else's software and you're trying to figure out how it starts what's happening always look for the main.go file it's where everything begins.
So you can see that the package is suggested is awesome project the reason it's done that is because that's my project name here but there's another rule for go so another rule for go is that all main functions must be in a package called main so I'm going to change this to be main and I'm going to define my func main so you come from other languages like C or Java you might be familiar with this sort of pattern it looks very similar to those.
So whenever we start our Go program it's always going to look for the main function in the main.go file which is going to be in the main package. So stick to these rules and you'll have a really easy time navigating other people's Go programs as well as your own. So let's do the standard introduction to a programming language program and let's print hello world to the console.
So I'm going to start typing FMT, which is the format package from the Go Standard Library. And as you'll notice, Goland helps me out and gives me a bunch of options and tries to help me with IntelliSense as to what I might be wanting to do next. And as soon as I start typing print, it shows me all the different options for print that I may use. So I'm just going to press down and press enter.
And the local AI is even helping me figure out exactly what I wanted to be typing. So all I'm going to do is press tab now and it's completed my program for me. So we have our first go program. You'll notice that once I've done that it's imported FMT so all this is saying is from the go standard library import the format package and we're going to be using a function from it.
You can even click into this by pressing the command key and pressing click left click and it takes you into the format package here so all of go is written in go so you can go and read all the source code and figure out exactly what's going on in all these functions. So for example you can open any of them and you can read through these and it might even help you learn go by reading how the go team have written some of these standard library packages. All of it's very well commented as well so it's a really great way to dig in a little bit more.
So I'm just going to close that and collapse this sidebar again and if you were writing this without an IDE the next thing you'd want to do is run it so what you could do is you could open your terminal and you could put go run main.go and you can see that hello world was printed to the console.
So this is great but we want to take more advantage of Goland here and not have to keep opening the terminal to run programs so what we can do instead is we can click on this green play button over here and I can just press run go build awesome project. You'll see there's some other options here which is debug and run with coverage we're going to talk about both of those in this course because they're both very useful but for now I'm just going to run this.
And you can see it printed hello world to the console. The last thing I want to cover off before we finish this video is go format. So if you've come from other programming languages you may have had debates with your co-workers about style right like how should I organize my code how should it look maybe some people like to do this for functions as you can see straight away Goland's giving me a warning because if you do this in Go you actually need to put a comma here.
But let's put this back a second so I'm just pressing command z to go backwards there and you can see that I've got some nice indents here that I want to keep but let's say I was not doing a good job with my programming and I left it like this. So Go has a built-in formatter which is very opinionated about how you should organize Go so to run that you can run go fmt in the console or you can press command option shift and l and that sounds like a lot of buttons but trust me you get used to pressing it after a while.
So I'm going to press that now and it comes up with this reformat screen and it gives me some options I can optimize inputs I can code cleanup and I'm going to run across the whole file so I'm just going to hit enter and you'll see it moves my format.println back to where it was previously so I get into the habit of just hitting this shortcut all the time.
So whenever I'm writing code, every 10, 20, 30 lines or so, I'll just hit that and it'll format it. If you follow this, you'll notice that your Go will be really well organized and you won't have any crazy diffs when you make pull requests because everybody will be using the same style guide. So I really recommend getting used to running Go format when necessary.
That's it for our first video. You've now got your first Go program, which is awesome. We're going to step this up quite a bit in the next section, going to be building an API from scratch. So I look forward to seeing you there.