Check out a free preview of the full Go for JavaScript Developers course:
The "Anatomy of a Go File" Lesson is part of the full, Go for JavaScript Developers course featured in this preview video. Here's what you'd learn in this lesson:

Brenna describes the structure of a Go file, how to run the file from the terminal, and answers questions about package main and the main() function indispensable in every runnable Go file.

Get Unlimited Access Now

Transcript from the "Anatomy of a Go File" Lesson

>> So the next thing I wanna talk about is the Anatomy of a Go File and what you're starting within your application. So I'm going to flip over to my code editor and build a file from scratch. In the Go repository that we're working with I'm gonna create a New file, and I'm gonna call it test.

[00:00:21] Nope, not test, I'm gonna call it practice.go. And so the first thing every Go File needs is a package name. And the package name lives at the top of the file here and for now we're gonna say package main. And every Go program needs at least a package main.

[00:00:45] We're gonna look at creating custom packages down the road. But for now we're gonna start here, and this package is going to encompass whatever files live in the same folder. And allow us access to some of the variables and functions that only are associated with this main package.

[00:00:59] And we can kind of pass that around and encapsulate some of the code in smaller pieces. The next line is gonna be any import statements for any additional libraries you might need. So we've noticed that we've been using the thumped package to print to the console. And I know I'm gonna be using that later on in this particular file.

[00:01:14] So I'm gonna import that at the top. And the input statements allow you to access additional libraries that ship with the Go code. For instance, if you're handling day time or strings or convention of types or input output, and we'll see a lot of those throughout the day.

[00:01:31] And the third part here is the actual main function. So to create a function and go use the word funk F-U-N-K, and every Go program needs both a package main and a main function. And the main function is gonna be the entry point for your program where Go starts executing the code and fires off whatever else happens in your program.

[00:01:49] So for now in main, I'm just gonna circle back to our print line function. An interesting aspect of Go as well is that in any particular package, so right now, we're looking at the main package. If we had additional files in our directory, you can only have one funk main function in that directory.

[00:02:20] So earlier in the code base, we ran this main.go file to get the Hello Front-End Masters. And so right now, I'm working with two different files in the same directory that both have funk main. So to fix this problem for now, I'm gonna comment out one of them because we don't need two right now for this example, and then I'm gonna move back to our practice.Go to run this code.

[00:02:40] I'll use the terminal that lives in VS code to kind of run most of our examples today. And then to actually execute this program, you're gonna use the command GoRun, which we'll talk about a little bit later, and then the name of the file you're running. So I'm just in the repository like the top level directory.

[00:02:56] And so I'm gonna run Go, run, practice.Go. And I should see Hello World print to the console. So what's happening is Go is going to find that file, it's gonna find the main function and it's gonna execute whatever's inside it based on the file we just created. Another side note about the structure of the repository here is that each of these directories do have their own main function within them.

[00:03:17] And as we continue to work through some examples, you might have to kinda comment out some of the main functions depending on where we're at. Just as a side note.
>> When you run the practice Go, it's reading other files in the same module.
>> It is, so in the directory I'm in in the top level directory Go is looking for anything that says package main.

[00:03:35] And within package main you can only have one main function. So once it sees two it's conflicted with what to run and doesn't know where to start executing that initial file.