Complete Go for Professional Developers

Writing Your First main.go

Melkey
Twitch
Complete Go for Professional Developers

Lesson Description

The "Writing Your First main.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 creates a main.go file and demonstrates how to run the application and build an executable binary. Every Go application begins with a main function. The "fmt" package is imported from the standard library, and the Println function is called to print a message to the console.

Preview
Close

Transcript from the "Writing Your First main.go" Lesson

[00:00:00]
>> Melkey: Okay, let's go ahead and open up some code. So you can see if you open up main.go, you're already greeted by the compiler, it's already mad at you. You haven't written anything and the compiler is yelling at you. Go's compiler is very opinionated, it's gonna yell at you a lot.

[00:00:17]
There's a bunch of different rules that the compiler wants you to follow so I can be happy, that Go can run. And we'll see more of these as we kind of write more code. I'm not gonna list them right here, we'll just see them as they come. But one highlight that I want to emphasize here is, every Go project starts with the same function.

[00:00:39]
If you go to any project on GitHub, your company, whatever, you look at a repository, it could be like multiple different files, thousands upon thousands of lines. If you wanna see where everything gets executed, it's a function called func main, okay? And it's going to exist in a package called main.

[00:00:57]
So, because it's our main.go file, we'll create our first package called package main. Just like this, oops, okay? And package main, when I talked about the Go module, I said that you can define your package to be whatever it is. Package utils, package routing, package middleware, you have control of what to call your package.

[00:01:20]
Accept the package main. The compiler is going to look for a package declared as main because we wanna compile into an executable binary. That's our goal, it's a compiled language. So, there's a strict rule set that the compiler needs to know to execute and create this binary in the first place.

[00:01:41]
So we have this package main. We've addressed one core rule that the compiler wants. Next is the function. So, I'm gonna quickly go over how to write a function. I'm gonna go in deep in detail of it in just a little bit. So it's func main, like so, okay?

[00:01:59]
So, func is the keyword for go to declared function, main is the name of the function, parentheses's the arguments within. There's a little gap right here where it returns the type of where this function returns, and then curly braces to execute the logic of the function. Here, func main has to be defined like this.

[00:02:19]
The signature of func main and every package main and every Go project follows this pattern, right? This is not negotiable. You can make other functions called func not really main. That could be whatever you want as a signature, but as far as the compiler, it needs to have a package main with a func main here, exactly written like this.

[00:02:38]
So, the first thing I want us to do is just make a hello world. To do that, I'm gonna import the format package from the standard library. To import, we're gonna use the keyword import. I'm gonna open it up with parentheses. And then here, to import a package, we'll do quotation and then FMT.

[00:02:59]
You can see that the compiler's already yelling at us, format import and not used. The compiler, if you have unused imports, will not compile, another strict rule by the compiler. To print hello world, we can do format.print line, and if you have everything except correctly, you should see the hint of the function here.

[00:03:19]
We'll do print line and we'll just say hello from the masters, and we'll save this, all right? Nothing too crazy, we can open up another terminal, or whatever it is you're comfortable with, okay? And to run this file, we can do the command Go, run main.go. Let me see, we have hello, front end masters.

[00:03:41]
Nothing too groundbreaking, hello world, of course we're gonna have it, right? It is what it is, really. And we've explained kind of the logistics of the compiler. One thing to note, when we import packages, packages can import other things from other packages, right? Of course, you can have cyclical imports, that's kind of a no-brainer.

[00:04:05]
Your package main can import other packages from your application, no problem. It cannot export, so other packages cannot import anything from your package main. Your package main brings everything in, structures your project, and it executes whatever is in the func main here, all right? So remember, it's not like a two-way door, only in for the funk main, but not out.

[00:04:29]
Other packages, there's less restriction. It's just a package main follows this particular rule set, okay? One more thing on the compiling aspect, I wanna kinda cover this ASAP before moving forward with anything. Right now we're running this, we just do go run main.go again and it just runs whatever we have in our func main.

[00:04:48]
If you want this to be compiled into a binary, which is something you would typically see in like a pipeline, right? A project pipeline that it gets deployed, you get your Go code, it lints it, checks it out, compiles it, deploys it, sweet. The command to do that is, and again, this is a very simple version of the command without specifying operating systems or anything like that.

[00:05:09]
But we do go build-o, then the name of the executable file, we'll just call it main, and then where it's from, so main.go. If you run this, you can see now we have this new file main, which we then can run like, so you can see hello, front end masters.

[00:05:26]
So yeah, if this is someone's first interaction with making a binary file, one thing I really want to highlight is if we go back and just changes hello to something like, I changed this, right? And we save this Go back and we use Go run main.go, we'll see the reflection here.

[00:05:43]
If I just use the existing binary, we'll get hello front end masters because the binary file will act as a snap shot of the code at the time we use the built command. So if we want to rerun it, we would Go built, again we run the same command.

[00:05:58]
And now if you execute that binary file, we'll have I change this. So just something to be aware of, right? When you're building, if you're kinda scratching your head like, wait, wait, why is my change still not working? If maybe you skip the step in bundling your app or zipping it up, deploying it to whatever it is you deploy on, that could be a thing.

[00:06:15]
So, just a little heads up.

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