Build Go Apps That Scale on AWS

Setup Your First Go Program



Build Go Apps That Scale on AWS

Check out a free preview of the full Build Go Apps That Scale on AWS course

The "Setup Your First Go Program" Lesson is part of the full, Build Go Apps That Scale on AWS course featured in this preview video. Here's what you'd learn in this lesson:

Melkey creates a Go program using the "go run init" command. A go.mod file is generated. The entry point to any Go program is main.go. Variables are introduced, and string, bool, and int types are demonstrated.


Transcript from the "Setup Your First Go Program" Lesson

>> All that being said, we're gonna get into more practical example here with writing Go Code. This portion like I said, it's gonna be mainly a beginner focus, right like, from the Hello World, maybe a few concepts and data structures. We're gonna type those all out. But before we start, if you've never spun up a Go Project from scratch, the very first command you're gonna write is Go mod init and then the name of the module.

And I wanna focus on the name of the module just just for a quick portion here. This plays a critical role, especially if you're making something that's gonna be public-facing. Like if you wanna make a package that people depend on, this does play a critical role. And you can always change it, so it's not like, okay, I gotta restart.

No, you can change it. It's not the end of the world, but it does allow your project to have this underlying structure as you move forward with it. And I wanna show an example, and this is a project I made on open source. It's called Go Blueprint, you can check it out, I don't wanna plug it too much, but it spins up a Go Project for you, okay?

That's the TLDR. But when you run go mod init, you will generate what's called a go.mod file. Let's just jump into this for a second. This go.modfile, is gonna include a bunch of different things. It's a kind of wanna say it's your past, package.json, but it's not, I think it's kinda doing a disservice, but it's similar in that sense.

So we have this module name. This is what I wanna focus on. So be new when we spun up this project, I wanna be an open source project. I wanted people to use it, pull it in, check it out. And the convention in Go is when you have this idea of making something open source, you follow this kind of pattern where it's hosted, so

This is either the name of the author, the name of the organization, maybe it follows a suite from a different package, so that's kind of what this middle portion is, and then it's the name of the project. So this is called Go Blueprint. And why this is effective is that this is the identity of the app everywhere.

If anyone was to pull this in, did you go get and then this is what they put in and they'll get the package in their repository, right? So this is why it's important because you can really write anything you want and that Go mod init portion, and the compiler is not gonna stop you to say, hey, wait, I expect to have slashes doesn't exist.

But if you kinda say something that doesn't make sense, it could throw off some potential people that wanna use your package. So that's just some food for thought if you do wantto make something client facing or available to the public. And then you have the version that go that we used to spun it up.

You have your direct dependencies and then you have your indirect dependencies here and these required blocks, all right? So these are things that we specify that we need. So we literally run the Go Get Command on these packages, also very good packages, check them out, and then we have the indirect dependencies here.

In this entire workshop, you will not need to pull any code. There is no like, use this and write all the code for you and we're gonna add in things, no, we're gonna literally build stuff from zero to hero. But if you wanna snapshot the finish code, I have a public repository here frontend masters.

All right, you can check this out. This is the link, dev/frontend masters. And there's a bunch of different branches there, and the readme basically describes kind of the branches. So the main branch has everything you need for the Go portion. Chapter 0 is the chapter 0 stuff, chapter 1, chapter 2, and chapter 3.

So yeah, pull this down. You don't have to. I kinda prefer if you don't. I'm kidding, just have it in the backlog. Have it in the back pocket if you wanna go check it out, if you get lost, whatever. And we're gonna audit some stories. Okay, I gotta start myself.

All right, so here, this is gonna be the branch that we're working on. It's gonna call it live underscore workshop. So we just have a snapshot are done, but we're gonna be doing something here, I'm also gonna deploy this branch, so if we go on a tangent, go explore different topics, it'll be on the live workshop branch as well.

All right. So the first thing, let's go ahead and do a Go mod init, you can see that in this, hopefully you guys can all see, there's nothing here just to read me, all right? So go ahead and do a Go mod init. I'm gonna call it, femBasics.

I don't think that's too crazy. Let's do a touch main.go. Go files are defined by their extension as dot go. And this is it. And already we're getting an error. The compiler is already yelling at us, I love it. I haven't even done anything, just created a file.

Basic Go files have two pivotal components at minimum. The first one is package declaration. Every package in Go, every file has this package declared to it. And I'm specifically using package main here, and there's a reason for that. You can name your packages different things, package whatever, package database, package API, whatever.

And that's gonna basically help us organize our code, it's gonna help construct our code, it's used for imports, but package main is different. Package main is specifically used and it's a keyword. I'm gonna explain why in just a second. And then you have a function here. Functions and Go are defined with the func keyword and then the name of the function, I'm gonna use main, and then parentheses for any arguments and then what you're gonna do in the logic.

And then right after the parentheses, there is a return type. So what is your function gonna return if it returns anything? And then you have some logic here. I purposely named this file func main. And this is what the compiler expects. Every time you create a Go project a, Go application, you are gonna have to have a package main and a func main, no matter what.

You can have other packages can have other repositories. You can slice and dice your code everyone. This is a must and the reason for that is this acts as the entry point to your entire application, okay? If you ever get into a new company, a new repository, and it's a giant monolith, and you have no idea where to start, this is where you start.

And then you work backwards. Cuz this is the entry point, I'm gonna show a very, hopefully, a good example down the line of why this is designed to be the way it is. But for our purpose, just understand right now, this is our main entry point. Question?
>> Do you have any VS Code extensions installed for Go?

>> Yes. Great question. I have one VS Code extension. It's the Go PLS, the Go Please or PLS extension. It just helps with formatting LinkedIn, it comes with a bunch of stuff. That's the only VS Code extension you need. And for my NeoVim homies, get the LSP, get GoPLS.

I do build up both, but I think VS Code is gonna just look a little nicer for this. So anyone use NeoVim here or Vim? Okay, awesome.
>> Yeah, everyone that's in your chat right now because they came from Prime.
>> Literally, yeah, I'm about to get roasted.

Okay, so with that roast underway, we're going ahead and continue cooking our code. We're gonna do an import, imports have this keyword and you can do anything input your packages, the standard library, we're gonna use something called Got the format. And like every beginner project, we're gonna write format dot print line.

Hello World. I can type sweet, exciting stuff. If you're if you wanna run this code, you can do Go run main.go. Hello World. Awesome. That's it. Thank you for coming to the workshop, hope you have a great day. Pretty simple stuff, the Go run main go. It basically runs this main entry point function, and we can see that format print line, print a new line.

Cool. Nothing too crazy there, nothing too exciting. Let's actually jump into still beginner stuff, but maybe some newer stuff. So what I wanna do is, let's create a new folder called variables. Any variables create new file called main.go, okay? And here we're gonna do another func man. Now why am I doing this?

I just said every Go Apple application has this one entry point. I just made another one the same project route. What am I doing? This is by design. Go applications and go project can have multiple entry points, can have multiple executions. Basically you can have multiple microservices defined in like a model list.

And this is for any purpose you wanna execute code individually. Shouldn't execute that's separate from another package, a main package. As long as they are separated by a parent directory, it almost acts as its own individual entry point, if you will, okay? So there's something to keep in mind.

And the reason why I wanna do this is, I think it modularizes a lot of the conflict cover here. So if one day you're looking back or somebody's looking back and like, okay, how do we run the variables? I don't wanna have all these imports mashed into one entry point, let's just keep it separate.

Variables would be its own executable. Go up into loops, go up into maps, and we'll execute them as we go. So how are variable and what are variables? Pretty simple stuff. There's two ways to declare variable and Go, there's your explicit way and you're in third way, so you can do something like VAR my name, right?

And you can put a string, you can define a string. Let's say my name is Melkey. Cool. So we have the name of our variable, the type it is and what we declare it to be. And another gotcha in Go already is my name declared and not used.

We cannot compile this. If you use Go, you know what I'm talking about. This is one of those gotcha. Go compiler, if you have unused imports, unused variables, it will not compile, it will not run. Okay, so we have to use this. And you can use it in different ways.

You can just print it, it's fine. We can do a, was bringing our format package again to do format printf. This printf takes something called format specifiers. So it's different than the print line. This is printf, and this is a way to do concatenation in Go. All right?

Cuz you can specify the type that you wanna concatenate with your string. So if I go back, oops. If I go back, we can write here, hello my name is, and then we can do our specifier percent s, right? And type in that string. And percent s is, or modular s is for the string type, okay?

You can have different types like ink, float, Boolean, and each have their own format specifier. And if we wanna run this code, we can't just do Go run main.go, like our last executable command here, because it's gonna run the hello world from the previous main.go. So how do we run this variable, main.go?

Go run the directory, so variables, and then main.go. You can see here. Hello, my name is Melkey, and I'm actually kinda disappointed in myself. I told myself I wouldn't do this. I've practiced this over 15 times, and I still do it. Classic gotcha. If you don't do print line, you will not print a new line.

You have to break the line with the /n, all right? That is how you break a new line. Because if I were to remove this, let's just copy and paste this if I run this. It's pretty ugly. So make sure you add that break character here to create a new line.

I'm probably gonna do this a few more times and every time I'm gonna be upset with myself cuz I'm like I'm afraid forgot it again. I forgot it again. So yeah, there you go. This is fine. This way of declaring variables is fine, it's cool. It's kind of long, I don't know, VAR, name, type, declared.

So let's insert the type. So again my name and we can do, this operator kinda looks like a walrus and just do Melkey and there you go. Same thing. The compiler infers a type to be a string. We can use it, as it satisfies our format specifier here and we can rock and roll if you run this code.

Hello, my name is Melkey. Cool. And this extends to different types too. So my init let's just say is 10. My float, let's say is 10 float. Not that. 10 float, and then let's just specify those, right? So hello, my name is string. My int is modulo t for int, and then my float is modulo f for float and just append those over here.

Gonna do myInt and then myFloat, cool. And I was about to run the code, no one stopped me, but I added the break. So there you go. My name is Melkey, myInt is 10, myFloat is 10 float. Cool, nothing too groundbreaking. We have a way to infer types and we can use them.

Another gotcha that I wanna introduce, a core concept is, let me just kind of show it. So let's say I make a new variable called my friend's name and let's say it's a string, okay? And then let's make another variable of a Boolean, VAR my boo, type Boolean and then my other end, var my other end.

And isn't it? So what why is it compiler yelling at us? Is the Quran I use? Okay, let's just use them. Okay, so format F printf All right? Let's say my other friends name module s, my bool, module t for Boolean and myother int %d, and let's just append these, okay?

So my friends name, my bool, myother int. Before I print this, again, no one called it, didn't break the line. So let's break this line. What what is gonna happen here? We didn't declare these variables. So nothing is yelling at me. The code can compile. If I run this, my other friends named blank, my bool false and my other int zero.

I didn't declare false or 0, they're here declared. And what's happening here is go as a concept of zero value. Because we can declare or initialize the type here and it will have an underlying data structure, but we don't assign it a value. And this is okay with Go, because it goes down to the 0 value.

So for a string, it's an empty string. For Boolean it's false for a integer, it's 0, or a float, it's 0 float. We'll get into more nuanced examples, but it's worth mentioning that if you initialize your variable, but don't assign it to anything, you run the risk of having your code run its logic on the 0 value.

Which isn't bad because you can do my friend's name Prime, Brian Prime and, and there you go. My friend's name is Prime, my bool is follows my other to 0. In Go, different data structures have different zero values. These ones do not equate to nil, but other data structures do.

We're gonna go into some of those examples. And that's another really good gotcha.

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