Check out a free preview of the full Go for JavaScript Developers course:
The "Packages" 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 introduces the required package name at the top of every Go file, exported and unexported functions, the linting rules for exported functions, and explains how to run exported functions in other Go packages.

Get Unlimited Access Now

Transcript from the "Packages" Lesson

>> So let's talk about packages. So far we've really only looked at package main. We've imported a few. We've imported fmt, math, and reflect. We haven't looked at math yet but it would open up a lot of functions around mathematical operations. And then reflect was the one we used for asking what kind of type a particular variable was.

[00:00:20] We're gonna hop over to PACKAGES.GO. We talk a little bit about package visibility. And then we're gonna create our own custom package. So we can watch that interaction between the main package, which has all of our code so far and then writing a smaller package and pulling that code into it.

[00:00:34] Cool. So right now the only thing in this file simply has a print line for packages. But let's say we wanted to create a package that has kind of like utilitarian functions. If you wanted to capitalize strings or do kind of smaller unit testable tasks. So in our code directory here, I'm going to create a new folder.

[00:00:58] And I'm gonna call this folder utils. And I know that all go files that live in this folder are now gonna be in package utils. We're getting out of package main, we're gonna do a subset of code that lives in package utils. So in utils, I'm gonna create a new file called mah.go And essentially this file will be where all of our math lives.

[00:01:25] So here at the top instead of package main, we're now gonna say package utils. [SOUND] I'm still going to import the package. And then in this file, I'm going to create a couple math functions that just do some basic math for us. First I'm going to create is just func Add.

[00:01:46] It's gonna take some amount of numbers. That must be int, and then at the end of the day, I wanna int, so. Theoretically we'll add them all together, return a final int. I'll set my initial value to 0. And then using that underscore since we're not using the index and v for every individual value of number.

[00:02:12] User range keyword to iterate over all of our numbers. And then, for kicks, I'm gonna create a new function called printNum. Just so we can kinda visualize like, what number are we looking at? What are we doing with this thing? We'll write that function in just a second.

[00:02:34] Let's add it to our total and then at the end of this function, I just wanna return the total of all the things we've added together. So you'll notice that right now I'm getting some errors we need to go write that printNum function, so I'll put that right above our add function.

[00:02:54] And printNum is going to take a single number that's also an integer. And all I really want this guy to do just we can kinda watch what's happening behind the scenes is print out. The current number you're talking about. Is this thing.
>> You also need on 13 plus or equals.

>> Thank you. So on line 13 just modifying that typo where it used to be called an equal sign and changing that to actually add each individual number to our total value here. So one of the areas that we're still seeing that I'm kind of leaving up here intentionally, this green squiggly line here is telling me that something's wrong with this function and my inter.

[00:03:40] If I have override it's telling me that the add function should have a comment or be unexported. And so with packages if you're talking about JavaScript and we want to make any variables or functions accessible to other files. We have to export them, export const etcetera to get those values or functions or variables to be accessible elsewhere.

[00:04:00] In go, anything that has a capital letter is automatically exported. So right now because this thing is actually going to be visible elsewhere once we import the utils package. Go has a convention that anything exploitable needs to be commented so that users know what's happening and what it does in other packages.

[00:04:17] So here I'm just going to say, Add adds together two numbers. Multiple numbers. And you'll see that error ran away. So to quick summarize kinda what's happening here, we created a different package called utils. And nowhere in this package is func main. And that's because this is not the first starting point of our program anymore.

[00:04:45] We're not trying to dive in here and do this every single time our program loads. We just might need this add utility function elsewhere in our application. And so we want to define it in a place where we can reuse it in other places. Cool, so let's put this package to use.

[00:05:03] I'm gonna go back to our packages.go file. And then underneath import here, we're still in package main, but I want access to that utils file that we just created. So underneath thumped, I'm going to import a local package. And go is going to start from that top level directory.

[00:05:22] So that go workspace. And so remember that go has like the folder directory structure where like after go you see source and source. You have all of your GitHub files or local files or the repo or whatever you're trying to do. So this import statement here needs to be the entire file path to the file we just created.

[00:05:43] So starting from the go workspace in source, you have, let's see intro-to-go as the name of the repository and then within that we have the 05 toolkit. And then within that we need to go into code. And then within that we need to go to utils. On save it is deleting it since I'm not using it.

[00:06:10] So let me just run that one more time. And so on line four we've been using the font package just by writing fnt. When you have a long nested list of packages here the name you're gonna use is either the last on the right hand side, so we'll use utils here.

[00:06:39] If we wanted to, we could add kind of an alias on the left hand side. So maybe this is like math. And now we can use math instead of this package name here that can be helpful if like that last package name is particularly long or has a lot of difficult to spell words.

[00:06:55] You can kind of choose either abbreviation or a single letter or a shorter word to use here to make your life a little easier. For now, I'm going to stick with the basic utils on the end. So underneath our print line packages let's add a function here. So I'm gonna say okay, I know at some point I need a total variable in my main function and then I'm not quite sure what's happening.

[00:07:22] But I'm going to create a file a function. Calculate. Calculate important data. So my main function all it knows is that this calculate important data is going to go handle all of the calculations. Do some other stuff, make some magic happen. But that's what my entry point of my application is going to kick off by doing.

[00:07:45] And then underneath that, let's go and print out what we get from that function. And so to make that function above main, I'm gonna create a func calculateImportantData Let me just reimport this one last time. [SOUND] So now I can use my utils package. So, let's say maybe there's some crazy stuff happening in this folder or this file, excuse me.

[00:08:36] And at the end of the day, we want the total value to go grab that add function from our utils package. So, because we have access to it, online file, I can now call utils dot add of the capital A, because it's exported. And then pass it a series of numbers.

[00:08:55] I do want this function to return the total value, which means that to the right of the parentheses here in our function signature, we also need to say that hey, you do need to return an integer Cool, so you'll see when I run the code, we get that current number that's all happening in the utils directory.

[00:09:26] And then we're also getting 15 which is what we're printing from line 15 of our main function.
>> So, I'm just curious about the utils that add. Is that utils coming from the file name, or that you imported utils or is it because it's the package name is utils?

>> It is the package utils. So behind the scenes, if we had multiple files in our utils directory, if we had strings.go and do some other stuff that go all within the package utils. All of the exploitable capitalized functions within that package would be visible by calling any of those methods on utils.

[00:10:02] On the other hand, though, you'll notice that in our math.go, we do have two different functions. We have the capital A add and we have this printNum. And just to demonstrate a little bit about that export madness, I can't. Let's see. So printNum. What do we need here?

[00:10:16] It takes a single integer and print some stuff. So if I were to change this a little bit to say I just wanna print line and say utils dot lowercase, printNum 4, let's actually do a bigger number, 10, run this file. We're gonna say, undefined: utils.printNum, cannot refer to an unexported name.

[00:10:44] So the only functions that are gonna be visible, even if we can see all of the utils package, are anything that has a capital letter.