1
00:00:01,020 --> 00:00:03,340
So we're coming along on our tour of the go language.

2
00:00:03,360 --> 00:00:04,730
We know how to use variables.

3
00:00:04,740 --> 00:00:08,080
We know how to make decisions with an if statement or a switch statement.

4
00:00:08,100 --> 00:00:09,330
We've had a look at pointers.

5
00:00:09,340 --> 00:00:14,040
We've had a look at interfaces, custom data types, maps, slices.

6
00:00:14,490 --> 00:00:18,120
And it's time to look at another important concept known as packages.

7
00:00:18,120 --> 00:00:23,040
And if you've worked in any other programming language, you're familiar with use of packages.

8
00:00:23,040 --> 00:00:27,120
Java uses them and calls them packages, imports, things.

9
00:00:27,120 --> 00:00:28,770
And we've been using packages right along.

10
00:00:28,770 --> 00:00:33,720
Whether you're aware of whether you're aware of it or not, I have an empty mango here.

11
00:00:33,720 --> 00:00:39,990
And if I try to print to the log log print line, hello, it actually imports a package for me.

12
00:00:39,990 --> 00:00:45,300
And in Goaland, if I roll over it, it'll tell me what the package is called, log and how to use it,

13
00:00:45,300 --> 00:00:47,220
import log and then describe it.

14
00:00:47,850 --> 00:00:51,800
Well, we can also create our own packages log as part of the standard library.

15
00:00:51,810 --> 00:00:54,210
We don't have to import it or we don't have to define it.

16
00:00:54,210 --> 00:01:00,750
It's already defined for us, but we can actually define our own and we do that in a really simple way.

17
00:01:00,780 --> 00:01:04,380
I'm going to show you how to do this again in a later lecture, but it's good to get our feet wet right

18
00:01:04,380 --> 00:01:04,730
now.

19
00:01:05,130 --> 00:01:11,520
We need to tell our Idy and go that we're using packages or go module's go module's is the way we use

20
00:01:11,520 --> 00:01:12,060
packages.

21
00:01:12,060 --> 00:01:16,740
Now, he used to be you'd have to define a gopalnath environment variable and put things in certain

22
00:01:16,740 --> 00:01:17,900
folders and it was a real pain.

23
00:01:18,180 --> 00:01:19,410
It's much easier now.

24
00:01:19,800 --> 00:01:25,530
So I'm going to open my terminal and type go mode in it and then I'm going to give my the package I'm

25
00:01:25,530 --> 00:01:27,120
working on, which is my program.

26
00:01:27,120 --> 00:01:27,480
Aname.

27
00:01:28,050 --> 00:01:33,390
Conventionally you start you name things by prefixing it with the name of the git repository where it's

28
00:01:33,390 --> 00:01:33,930
going to live.

29
00:01:33,930 --> 00:01:38,790
You can call it whatever you want, but it's useful even if you don't push to GitHub or BitBucket or

30
00:01:38,790 --> 00:01:45,360
any repository to use this naming convention because you might push at some point GitHub dot com.

31
00:01:45,360 --> 00:01:50,220
My username is Chiseller and I'll aim this package, my nice program.

32
00:01:51,570 --> 00:01:52,500
So it created it.

33
00:01:53,520 --> 00:01:59,850
Now, when it does that, it creates a go mod file and the mod file is automatically created by that

34
00:01:59,850 --> 00:02:04,560
command we just typed, and inside of it it tells me what version of go I'm using one point one five.

35
00:02:04,560 --> 00:02:06,450
You might be a later version, but it doesn't matter.

36
00:02:06,780 --> 00:02:09,720
And it names this module, whatever I've just named it.

37
00:02:10,020 --> 00:02:13,290
And there's another thing that I sometimes have to fight with goaland to get it.

38
00:02:13,290 --> 00:02:19,140
Recognizing it, you go to goaland preferences if you're in goal end and search for goal modules so

39
00:02:19,140 --> 00:02:23,250
you can type modules up here and modules and it will show it.

40
00:02:23,700 --> 00:02:24,390
And there it is.

41
00:02:24,600 --> 00:02:31,980
And make sure that you have this checked enable go modules and apply and then click, OK, and there's

42
00:02:31,980 --> 00:02:36,690
probably similar steps to follow in visual studio code, but maybe Visual Studio Code is smart enough

43
00:02:36,690 --> 00:02:41,640
to do it without being kicked three or four times, which apparently goaland wants and requires.

44
00:02:42,270 --> 00:02:48,780
Once I've done that, I can start creating my own packages and I do that by going to my root level of

45
00:02:48,810 --> 00:02:54,480
the directory, creating a new directory, the root level of my project, creating a new directory.

46
00:02:54,480 --> 00:02:59,820
I'm going to call my helpers and it creates a folder and inside of that will create a go file, which

47
00:02:59,820 --> 00:03:01,320
I will call helper Stocco.

48
00:03:02,250 --> 00:03:07,140
And Goaland opened it for me automatically and you'll see that it names this package.

49
00:03:07,170 --> 00:03:11,010
Whatever the name of this directory is, it happens to be called helper's.

50
00:03:11,010 --> 00:03:17,940
And I can do things like define a type type some type which will just be a struct and it'll have two

51
00:03:17,940 --> 00:03:26,040
fields, one for type name, which is a string and one for type number which is in it.

52
00:03:26,040 --> 00:03:29,870
And I'm actually using these, I'm just creating a type that I can use in my main goal.

53
00:03:30,900 --> 00:03:35,600
So back in my main go file, I can actually use that type.

54
00:03:35,610 --> 00:03:37,800
How do I do that via my var?

55
00:03:38,580 --> 00:03:46,200
I'm just naming a variable and it's a type, some type and it automatically shows me that helper's some

56
00:03:46,200 --> 00:03:51,240
type is available and when I choose that it imports it and it imports it.

57
00:03:51,240 --> 00:03:52,830
Using the package name.

58
00:03:52,830 --> 00:03:58,680
I specified what I wrote Gomaa in it and then it names that package.

59
00:03:58,680 --> 00:03:59,550
Which one of you important.

60
00:03:59,650 --> 00:04:02,190
I can create another package by creating another directory.

61
00:04:02,390 --> 00:04:08,440
I haven't done anything with my var but the fact is I've imported it so I can do things like my dot

62
00:04:08,730 --> 00:04:20,310
type name is equal to some name and then print out to the log logged print line my va dot type name.

63
00:04:20,700 --> 00:04:27,990
And if I run this go run mango it will work and there it is printed out.

64
00:04:28,230 --> 00:04:29,670
So that's what packages are.

65
00:04:30,030 --> 00:04:32,760
And the thing is you don't just define types in packages.

66
00:04:33,090 --> 00:04:38,790
I can also in my helper defined functions that I can call by importing the helpers package and then

67
00:04:38,790 --> 00:04:43,200
I have access to everything in that package and that's all there is to it.

68
00:04:43,200 --> 00:04:49,440
So packages are incredibly useful, incredibly powerful, a nice way of getting our code organized into

69
00:04:49,440 --> 00:04:53,720
logical groupings and will be using those regularly as time goes on.

70
00:04:53,730 --> 00:04:57,420
So you're going to go through the same exercise in the next section when we start actually building

71
00:04:57,420 --> 00:04:57,750
things.

72
00:04:58,230 --> 00:05:02,520
But it's important to have at least some concept of what a package is right now.
