1 00:00:00,750 --> 00:00:05,120 A Go program is split into several libraries called packages. 2 00:00:05,120 --> 00:00:08,070 Each file begins with a package declaration. 3 00:00:08,070 --> 00:00:12,090 Packages are often use to set up a library for other programs to use. 4 00:00:12,090 --> 00:00:14,790 But there's a special package that provides a starting program for 5 00:00:14,790 --> 00:00:17,470 Go programs to run, the main package. 6 00:00:17,470 --> 00:00:21,210 We see this here at the start of our hello Go sample program. 7 00:00:21,210 --> 00:00:25,370 Following the package declaration, each go file includes an import statement, 8 00:00:25,370 --> 00:00:30,730 which states the names of several other packages that it needs to import. 9 00:00:30,730 --> 00:00:35,070 Once you've imported a package, you can use functions, constants, variables, and 10 00:00:35,070 --> 00:00:38,340 other things of declares within your current file. 11 00:00:38,340 --> 00:00:41,860 Then following the import statement comes the actual package contents. 12 00:00:41,860 --> 00:00:44,100 This consists of variables, functions, and 13 00:00:44,100 --> 00:00:47,200 various other things you want your package to include. 14 00:00:47,200 --> 00:00:49,770 Anything that you declare up here at package level 15 00:00:49,770 --> 00:00:53,120 will be accessible anywhere else within this package. 16 00:00:53,120 --> 00:00:56,360 Again, if you're writing a Go program that's going to be run from a command 17 00:00:56,360 --> 00:01:00,750 line, there's a special starting point, the main function Basically when a go 18 00:01:00,750 --> 00:01:05,560 program starts running, it looks for a function named main and invokes that. 19 00:01:05,560 --> 00:01:07,950 To refer to the names of variables, constants or 20 00:01:07,950 --> 00:01:11,810 functions that we've imported from another package we need to qualify the name. 21 00:01:11,810 --> 00:01:16,590 So for example this Println function was imported from the format package. 22 00:01:16,590 --> 00:01:21,330 To use it, we need to qualify it by placing the format package name before it, 23 00:01:21,330 --> 00:01:24,730 so we say fmt followed by a dot And 24 00:01:24,730 --> 00:01:27,650 then the name of the function that we want to call from the other package. 25 00:01:28,710 --> 00:01:31,210 If we were to write a function elsewhere in this package though. 26 00:01:31,210 --> 00:01:33,600 Let's say a function named otherFunction. 27 00:01:36,763 --> 00:01:40,485 And we wanted to call this from here within our main function we wouldn't need 28 00:01:40,485 --> 00:01:43,300 to qualify that name since it's here in the same package. 29 00:01:43,300 --> 00:01:44,570 We can just say otherFunction. 30 00:01:47,620 --> 00:01:51,870 Let's try creating another Go program that uses a couple of different packages. 31 00:01:51,870 --> 00:01:54,866 Since this is a program that is meant to be run directly from the command line 32 00:01:54,866 --> 00:01:56,831 we're going to put it in the main package again. 33 00:01:56,831 --> 00:02:02,850 And now I'm going to use a slightly different form of the import statement so 34 00:02:02,850 --> 00:02:06,390 that we can import two different packages. 35 00:02:06,390 --> 00:02:11,050 You'll notice that I put parentheses here at the end of the import line and, 36 00:02:11,050 --> 00:02:15,070 I'm going to include the couple different lines here with the names of a couple 37 00:02:15,070 --> 00:02:17,270 different packages I want import. 38 00:02:17,270 --> 00:02:20,470 So as before, we're going to import the format package so 39 00:02:20,470 --> 00:02:22,540 that we can use its print line function. 40 00:02:23,540 --> 00:02:27,630 And then in addition to that, I'm going to import the math package so 41 00:02:27,630 --> 00:02:30,000 that we can use a couple functions from that. 42 00:02:30,000 --> 00:02:34,020 Now up here, outside of any functions we're working at the package level. 43 00:02:34,020 --> 00:02:35,880 I'm going to declare a variable here. 44 00:02:35,880 --> 00:02:39,540 We'll talk more about variables in a little bit, but 45 00:02:39,540 --> 00:02:43,020 any variable that I declare here is going to be visible within 46 00:02:43,020 --> 00:02:46,380 any function in the package, here in the main path. 47 00:02:46,380 --> 00:02:51,380 So I'm going to declare a variable named myNumber and I'll just assign it 48 00:02:51,380 --> 00:02:57,760 the initial value of 1.23 Now let's create a main function again. 49 00:02:57,760 --> 00:03:03,750 Remember this will be kicked off as soon as our program is run. 50 00:03:03,750 --> 00:03:06,220 I'll create a main function and 51 00:03:06,220 --> 00:03:10,000 let's call a couple of functions from the math packet. 52 00:03:10,000 --> 00:03:12,870 I'm going to call the ceiling function first. 53 00:03:12,870 --> 00:03:17,850 Math, remember we need to qualify the function name with the package name 54 00:03:17,850 --> 00:03:19,710 since we're getting this from another package. 55 00:03:21,170 --> 00:03:26,930 And then I'll call the ceiling function, ceil, that's an abbreviation. 56 00:03:26,930 --> 00:03:30,870 And, as I mentioned, the my number variable was declared a package level, so 57 00:03:30,870 --> 00:03:34,510 we can access it from here within the main function. 58 00:03:34,510 --> 00:03:36,050 So, I'm going to type my number. 59 00:03:39,110 --> 00:03:42,990 And that'll round my number up to the next whole number. 60 00:03:42,990 --> 00:03:45,000 I'm then going to take the result of that and 61 00:03:45,000 --> 00:03:48,350 I'm going to assign that to another variable called roundedUp. 62 00:03:49,640 --> 00:03:53,490 Now since this variable was defined within a function, 63 00:03:53,490 --> 00:03:58,020 it's going to be accessible only here within the main function. 64 00:03:58,020 --> 00:04:01,080 We'll talk more about variable scope in a little bit. 65 00:04:01,080 --> 00:04:03,860 Then lets call another function from the math package. 66 00:04:03,860 --> 00:04:08,590 I'll call math dot floor, which rounds the number down to the nearest whole number. 67 00:04:10,600 --> 00:04:13,920 And we'll assign the results back to another variable named rounded down. 68 00:04:17,590 --> 00:04:20,030 Then we will just print both of those numbers out. 69 00:04:20,030 --> 00:04:25,050 So we'll call the format packages, print line, function, 70 00:04:25,050 --> 00:04:29,350 and we'll pass it, round it up, and round it down. 71 00:04:34,290 --> 00:04:34,960 Save that. 72 00:04:34,960 --> 00:04:37,430 Let's try running it real quick. 73 00:04:37,430 --> 00:04:41,560 Go run temp.go, which I've named my file. 74 00:04:42,850 --> 00:04:43,970 And it prints out 2 1. 75 00:04:43,970 --> 00:04:48,515 The first value here is the result of rounding 76 00:04:48,515 --> 00:04:52,708 1.23 up to the nearest whole number. 77 00:04:52,708 --> 00:04:57,028 And the second value here is the result of rounding it down to the nearest whole 78 00:04:57,028 --> 00:04:58,430 number. 79 00:04:58,430 --> 00:05:00,050 Unlike in some other languages, 80 00:05:00,050 --> 00:05:04,320 it's actually compile error to import a package and then not use it. 81 00:05:04,320 --> 00:05:08,790 In other languages, large programs tended to accumulate many unused library imports 82 00:05:08,790 --> 00:05:10,520 which slowed compile times. 83 00:05:10,520 --> 00:05:13,800 So Go doesn't allow that in order to help your development team 84 00:05:13,800 --> 00:05:16,410 keep your apps compilation fast. 85 00:05:16,410 --> 00:05:20,110 So let's try importing another package into the program here. 86 00:05:20,110 --> 00:05:21,660 And then we won't actually use it. 87 00:05:21,660 --> 00:05:26,790 So up here in the import statement I'm going to add the time package. 88 00:05:26,790 --> 00:05:28,900 Now let's try running it, down here on the console. 89 00:05:30,010 --> 00:05:32,460 And we'll actually see a compile error this time. 90 00:05:32,460 --> 00:05:35,390 Imported and not used, time. 91 00:05:35,390 --> 00:05:39,180 So as I say, make sure to only import packages that you're going to actually use 92 00:05:39,180 --> 00:05:40,000 in your program. 93 00:05:40,000 --> 00:05:43,670 If we remove that again and rerun our program it should run this time. 94 00:05:45,100 --> 00:05:48,130 To define a new package you simply create one or 95 00:05:48,130 --> 00:05:51,010 more .go files within a single directory. 96 00:05:51,010 --> 00:05:54,200 Each file needs to begin with the package declaration, 97 00:05:54,200 --> 00:05:58,580 which includes the name of the package a package name should be all lower case. 98 00:05:58,580 --> 00:06:01,190 And by convention it should be a single word if possible. 99 00:06:02,220 --> 00:06:06,240 Then within the package body you declare one or more constants, variables, or 100 00:06:06,240 --> 00:06:10,140 functions that you want your package to include. 101 00:06:10,140 --> 00:06:14,810 Constants variables or functions that are named with a capital letter are exported 102 00:06:14,810 --> 00:06:18,800 from your package and that means they'll be visible to other packages. 103 00:06:18,800 --> 00:06:23,770 By contrast, if a constant variable or function is named with a lower case, 104 00:06:23,770 --> 00:06:26,950 if it begins with a lower case letter, it'll be unexported. 105 00:06:26,950 --> 00:06:29,320 And that means that you can't use it from other packages. 106 00:06:30,460 --> 00:06:33,540 So these two constants up here, their names begin with capital letters, 107 00:06:33,540 --> 00:06:35,950 that means they'll be visible from other packages. 108 00:06:35,950 --> 00:06:38,880 And this one down here, that's not ready for public use, so 109 00:06:38,880 --> 00:06:40,610 it's named with a lower case letter. 110 00:06:40,610 --> 00:06:42,240 And that means it'll be unexported.