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

The "Packages Q&A" 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 demonstrates helpful functionality in VS Code for importing packages and performing linting in Go development. The discussion also covers a student's question about exporting data from packages other than the main package.

Preview
Close

Transcript from the "Packages Q&A" Lesson

[00:00:00]
>> Okay, so far, we have, for now, we're still using a package. We've seen how we can share everything that we said in your package between different files, which is expectable. And for now, we have just functions and variables pointing, and we have already imported our first packet.

[00:00:21]
And by the way, I mentioned that typically, you've done this manually, because if you start typing here print, okay, like that, or fmt and you press Enter or Return, it adds the import for you. So you don't typically do it manually, okay, you just go and start typing and it will add the import for you.

[00:00:42]
And if you don't add the import also, but you call the function, you have an error, of course, because it doesn't know in this file how that works. And by the way, imports are not per package, they are per file, okay? Because functions.go are already importing fmt, okay?

[00:01:02]
And it's in the same package as main, but that you still need to import that here as well. Does it make sense? So imports actually works per file. You will see an error and this error has the light bulb. And if you click there, you have a quick fix that it will add the import for you.

[00:01:23]
Okay, make sense? And by the way, if you look here, sometimes, you see an error, you solve the error, you add a quick fix, and I'm still seeing the error. Also, you will still see the error here. This is important, so save this in your brain, you have to save.

[00:01:44]
If you don't save the file, the linter is not taking the changes. So I need to save the file to actually remove those errors. That takes time as well, because when you start typing, you see the error, you solve the error, and the error is still there, the same error, why?

[00:02:03]
You didn't save. If you are brave enough, you can enable Auto Save, okay? That's just centennial for me, so it's not gonna work for me. But if you want, in that case, every once in a while, it's saving, but sometimes it saves when you're not finished, so then you get errors as well that you are not expecting, so yeah.

[00:02:23]
Anyway, if you wanna try that, it's there, okay? Do you have any questions so far? No, so far, it's pretty simple, yeah.
>> So when you export things, since we're all in the same folder and everything's in the same package-
>> As of now.
>> For now,
>> For now, yeah.

[00:02:46]
>> So when you're exporting, that's when you're trying to put stuff into other folders, that's the point?
>> I mean, let me do that quickly. Now, we will start a new project and we will do that from scratch, but just quickly. If I create a new folder here, and I say, I don't know, data, it just a folder.

[00:03:03]
I create a Go file within the data. I can call this, i don't know, collection or, let's say, constants.go. This is a file, constants.go, any name, within the data folder, so it's going to read the package data, not the package main, the name of the folder. And now, I can create a constant for, for example, my maxSpeed=60.

[00:03:32]
And now, I can go to main.go and try to access that. How to access? If I wanna print maxSpeed, okay, it's not gonna work, because it's in a different package now. What do I need to do? To import the package, even if it's in my same module, in my same project, okay?

[00:03:53]
Now, it's in a different package, so I need to import the package. Is it data? No, but it's called data. When you look here, it's data. Because it's my own package, I need to add here the prefix of my project ID, so it's going to be like that [SOUND], right there.

[00:04:20]
Look at this, if I save now, I'm going to save, it disappears. You say, [SOUND] why? Because I'm not using it. You say, but, yeah, I'm trying to use it. Well, the thing is that I should say data., and why data? Because it's taking the last part of the URL as the name of the object to reference things there.

[00:04:45]
If you wanna change that, you can put another name here, okay, something like that, like an alias, but for now, it's okay. But it's giving me an error, do you know why? So I'm trying to print the maxSpeed that it's actually constant, 60, in data, but it's not working, it's giving me a problem.

[00:05:08]
Do you know why? Does anyone know why? What's the problem? Look at the error.
>> Not a string.
>> No, that's not a problem.
>> Cuz it's lower case m. [SOUND] maxSpeed is not exported, why? It's lowercase m, visibility. So I need to change that. Is there any way to export?

[00:05:31]
No, if you wanna export a constant, a function, or a data type, or a variable, it must be title case. And now, I can't even see it. All right, does it make sense? Again, maybe that's not the right question, because if I say, does it make sense, some of you will say, no, it doesn't make sense, but that's how Go works.

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