Basics of Go

Packages

Maximiliano Firtman

Maximiliano Firtman

Independent Consultant
Basics of Go

Check out a free preview of the full Basics of Go course

The "Packages" Lesson is part of the full, Basics of Go course featured in this preview video. Here's what you'd learn in this lesson:

Maximiliano covers the topic of defining and importing packages in Go, explaining the scope of functions within these packages. The discussion includes a demonstration using dot syntax to access data from imported packages and how to define public or private visibility.

Preview
Close

Transcript from the "Packages" Lesson

[00:00:00]
>> You already mentioned packages, okay?That the package is a group of files in the same folder, we define the name of the package at the top of the file, and we can import other packages. We haven't seen that yet, but we can import other packages. So for that we use the import keyword, and within quotes, we the name of the package that we're importing, the package can be a built-in package from the standard library.

[00:00:25]
It can be our own package that we will do in a minute, or it can be a dependency that we are downloading from the web, another dependency, okay? It's just the same, it's just a package. And just remember that having at least one go file with package main is mandatory, and that one should have a function main.

[00:00:51]
Before creating another package to understand how packages work, I'm going to create another file here and let's call that functions.go any name, that's just a random name. In fact, you will see that unless we're talking about the go.mod that's the metadata for the module, the rest of the go files, they can use any name, we don't have a convention on that.

[00:01:13]
And I'm going to use the same package, can I use the same package name on different files? Yes, the only condition is that they must be on the same folder. But yeah, I can create another function here, I type JavaScript, no, it's func. And I can say, for example, here, I wanna print some data, and then I'm going to print some data, whatever.

[00:01:41]
Hello, and we will see in a minute how we're going to, we will stop using print in just a few minutes and you will understand why. So if I save this, of course it's complaining, why? Because I'm not using the function, but that's okay, get in your mind that that's okay.

[00:01:58]
Now, I can go to main.go and what if I want to use that print data function, how can I import that? So, I need to import another package using, as I mentioned before, import. Well, actually, it's the same package, because it's the same package, I don't need to import anything, it will just be there, I just execute it.

[00:02:23]
How does it know that it's there? Because in the same package actually, the go compiler will make different phases. When the compiler is compiling the app, it will first go over all the go files and at the end, all the files that are part of the same package, it's like they are merged into one big file, into one large file.

[00:02:45]
So a package is the only thing that the compiler sees the code sees packages, not files. We separate content in files just for our organization, not for the compiler, not for the language. This is like on C#, for example, you can create partial classes where one class is split into several files this is kind of that.

[00:03:10]
A package in this case is like the concept that we see, and then we can split the content of that package in as many files as we want. The only restriction is they must of course contain the same declaration on the top, and they should be in the same folder.

[00:03:28]
In this case, we're using a main package, that's not the package that we are defining, it's a package that already exists on every go software. On every go script, on every go module, we always have a main package, we are just adding functions to that package, and it's not just functions, I can also create variables.

[00:03:49]
So I can create a name here, or I can, look at this, I'm not saying you will like this, but I can create the name of my app. Here is the frontend masters, it's a variable, it's a global variable because it's an in go we don't say global variable, we say it's a package variable.

[00:04:09]
And it's a package variable, that means that I can also use it from here, can I? Yes, because it's the same package, so every function in the package can access other functions or other variables, constants, or data types for the same package. Yeah, maybe you don't like that, okay?

[00:04:33]
But that works. If I execute this, by the way, close this let me do this there we are, if I execute this, it says HelloWorldFrontend Masters, first, can you see that everything is in the same line without spaces? So, if we execute this, so go run. You can see it's working, but we do have a problem, hello world, we don't see separations, okay, why?

[00:05:05]
Because print is sending that to the console without creating patroness faces are new lines. We have another version it's called println, that will add a new line at the end typically we say print line. This is coming from see if you have ever seen see, so we have print and print line.

[00:05:24]
Print line, well maybe for example we can use print here, then, line after world and also after name, maybe we can add a space here, something like that. Now, it looks better, it says, hello world and a different line from the master, right? But here you can see that we can split our package content to insert several files.

[00:05:51]
And it will work I don't need to import anything everything is visible, so everything that was defined in the package is visible for the whole package. We'll start creating new packages in a second, what's that? That's a comment I brought the comment no one asked, what is that?

[00:06:09]
That's a comment, and for comments we use the same fee-based comments. That's a line comment, that's just double mean though this. And also you can select like a lot of code like that and you can use the shortcut you in your IDE for that will forward slash. And also you can use these kinds of comments as well as with JavaScript as we'd see with PHP, we are using the same kind of comments.

[00:06:41]
Yeah, you had a question?
>> I just wondered if it could get confusing, if you're sharing functions and variables across multiple files in one big folder. It seems like it could get kind of messy, but I saw that in my visual studio code, I can just use those tools in there to go to the definition of a function.

[00:07:04]
>> Yeah, you can always, first if you use control click or command click over function, you can go directly to that file on VS code, so it depends on what you are doing, you can get there. If you add enough information in your comments for your functions, your variables, you can use the tools that you're used to in your IDE to see messages and help.

[00:07:30]
So but as you mentioned, it's kind of messy, okay? Creating variables like around, that's why defining everything in the package main might not be the best of the ideas, so that's why we can create our own packages, we'll do that in a second. So we can create our own packages for small pieces and then group in that package variables, functions and types that we know what type is yet, okay?

[00:08:03]
Makes sense? Also that we've seen print and print line, however, we will stop using them soon, this is only useful for development purposes, we shouldn't deploy this, why? Not because deploying to the console is bad because it's not bad, that's not the case, but print as a function is not guaranteed that it's going to work and In every platform.

[00:08:32]
It's not guarantee that it's going to work in every operating system, so that's why it's not safe, it's just a quick way to debug something, okay? But we do have a safe way to print and that way, instead of VM print, it's in a different package. So I'm going to do that first manually to show you how that works, but then we will see that when we get used to that, we don't do this manually.

[00:09:02]
We are going to import another package, and the name of the package is fmt, it's format that they call it fmt. Some people say thumped we import thumped, okay? Anyway and you say it's complaining, why it's complaining? Because I imported the package and I'm not using it, so anxiety.

[00:09:29]
So you can see here, it's imported and not used, that's why it's complaining, so it's okay, it's fine. So now instead of using print, we're going to use, this is how it works, when you import a package, everything that that package is exporting, it's going to be available under the name of the package.

[00:09:51]
I'm going to say this wrong from a go point of view, but it's kind of an object that has all the things that were exported from the package, but it's not really an object, we don't have objects here. So, you say fmt, and we use .syntax, to access all the variables, or the constants, all the functions and all the data types that were defined as package that were exported from that package.

[00:10:21]
And we do have one that says print, and also we have a print line, so we just change print the print line with fmt.print, print line. And if you pay attention that print Hudson's more like different, small but very important difference it's using title case, title case is when you start with capital letters, and that's kind of mandatory.

[00:10:51]
That's one of the, probably the first thing that you may not like initially, so how do we know if a function is going to be exported or not? The way to define if a variable or a function is public, so anyone from the outside can use that is based on the initial character of the name.

[00:11:17]
If it's uppercase, it's public, if it's lowercase, it's private to the package. So for example, in our print data, if we look at this function, if we wanna export that, so other packages can use that, I must move it to capital P, if I don't do that it's private.

[00:11:40]
For those of you using OOP in Java, or C# or PHP, this is kind of using the private keyword or the public keyword kind of. We don't have those keywords here, we have a convention. If the global function starts with capital letters, it's public, it's export, if not, it's private to the package, we will use this in our code in a minute, okay?

[00:12:11]
You will see this in action. Of course, now I have an hour because I need to use capital B. So we are changing completely, how, naming guidelines work because probably you're used to, we start, property names and function names with our case and class names with, well, no.

[00:12:29]
Here you can mix because the name, how you start, the name defines visibility. Okay, make sense?
>> Would you say the purpose of that is for ease of programming?
>> Yeah, that's easy of code, yeah, even if you don't feel that, that's easier said first, that from the from the three goals.

[00:12:51]
That's not changing compilation speed that's not changing execution speed that's changing. Let's stop using a lot of keywords, adding modifiers to the functions that we keep it simple and also when you're real, When you read the code and you see if you read lowercase, you need to get used.

[00:13:10]
You need to train your brain into that when you read something that is lowercase, you know it's private immediately.

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