WEBVTT 00:00.360 --> 00:05.850 This is the eighth video of this section libraries in the previous video. 00:05.910 --> 00:11.250 We learned how to perform testing on the functions and do test driven development. 00:11.250 --> 00:17.070 In this video we will have a look at the GO GET tool and the encoding package. 00:17.070 --> 00:20.550 Until now most of our examples were applications. 00:20.610 --> 00:27.720 An application is defined by its main function and package but with go you can also create pure libraries 00:28.140 --> 00:29.120 in libraries. 00:29.130 --> 00:36.150 The package need not be called Main nor do you need the main function as libraries aren't applications. 00:36.150 --> 00:42.040 You cannot build a binary file with them and you need the main package that is going to use them. 00:42.060 --> 00:49.290 For example let's create an arithmetic library to perform common operations on integers sums subtractions 00:49.470 --> 00:56.280 multiplications and divisions will not get into too many details about the implementation to focus on 00:56.280 --> 00:59.310 the particularities of those libraries. 00:59.340 --> 01:02.790 So let us create the package in the Nano. 01:02.790 --> 01:05.510 First we need a name for our library. 01:05.520 --> 01:08.740 We set this name by giving a name to the entire package. 01:08.760 --> 01:15.750 So this is located within libraries and inside this I have named it as arithmetic not go. 01:15.750 --> 01:21.570 This means that every file in this folder must have this package name too and the entire group of files 01:21.570 --> 01:24.950 composed the library called arithmetic too. 01:25.020 --> 01:28.590 In this case because it only contains one package. 01:28.590 --> 01:33.510 This way we won't need to refer to the file names for this library and to provide the library name and 01:33.540 --> 01:36.790 path will be enough to import and use it. 01:36.810 --> 01:42.270 We have defined a sum function that takes as many arguments as you need and that will return an integer 01:42.270 --> 01:46.740 that during the scope of the function is going to be called rez. 01:46.740 --> 01:50.370 This allows us to initialized to 0 the value we're returning. 01:50.580 --> 01:55.500 We defined a package not the main package but a library one and called it. 01:55.620 --> 01:59.450 Arithmetic as this is a library package. 01:59.450 --> 02:04.370 We can't run it from the command line directly so we'll have to create the main function for it or a 02:04.370 --> 02:06.080 unit test file. 02:06.230 --> 02:10.820 For simplicity we'll create a main function that runs some of the operations now. 02:10.900 --> 02:13.250 But let's finish the library first. 02:13.430 --> 02:19.280 So let's add another function that is subtraction function to our library. 02:19.280 --> 02:24.090 The subtraction code will return 0 if the number of arguments is less than zero. 02:24.200 --> 02:28.890 And the subtraction of all its arguments if it has two arguments or more. 02:28.970 --> 02:34.490 Now let's add multiplication the multiply function works in a similar fashion. 02:34.580 --> 02:40.760 It returns zero when arguments are less than 2 and the multiplication of all its arguments when it has 02:40.760 --> 02:42.240 two or more. 02:42.290 --> 02:49.360 Finally the division code changes a bit because it will return an error if you ask it to divide by zero. 02:49.370 --> 02:52.720 So let's write the function for division. 02:52.770 --> 02:58.740 So now we have our library finished but we need a main function to use it as libraries could not be 02:58.740 --> 03:01.530 converted to executor all files directly. 03:01.530 --> 03:04.500 Now let's create a main function. 03:04.560 --> 03:09.430 Here's how our main function looks and the path should be similar to this. 03:09.530 --> 03:13.890 We are performing an operation over every function that we have defined. 03:13.910 --> 03:16.740 Take a closer look at the import clause. 03:16.910 --> 03:19.930 It is taking the library we have written from its folder within. 03:19.940 --> 03:24.540 Go path that matches its you are L and get hub dot com. 03:24.540 --> 03:28.720 Then to use every one of the functions that is defined within a library. 03:28.730 --> 03:36.230 You have to name the package name that the library has before each method let's recall some naming conventions 03:36.230 --> 03:37.920 about libraries. 03:38.180 --> 03:44.390 Each file in the same folder must contain the same package name files don't need to be named in any 03:44.390 --> 03:45.490 special way. 03:45.830 --> 03:51.920 A folder represents a package name within a library the folder name will be used on import parts and 03:52.040 --> 03:57.500 it doesn't need to reflect the package name although it's recommended for the parent package. 03:57.500 --> 04:04.220 A library is one or many packages representing a tree that you import by the parent of all packages 04:04.220 --> 04:11.150 folder you call things within the library by their package name now let's step ahead and look into the. 04:11.180 --> 04:18.930 Go get to go get is a tool to get third party projects from CBS repositories instead of using the get 04:18.950 --> 04:20.090 clone command. 04:20.090 --> 04:24.470 You can use go get to receive a series of added benefits. 04:24.470 --> 04:32.420 Let's write an example using core OS is ATC D project which is a famous distributed key value store 04:32.910 --> 04:43.370 core OS as ATC D is hosted on GitHub at get hub dot com slash core OS slash ATC D dot get to download 04:43.370 --> 04:49.280 this project source code using the go getters tool we must type in the command terminal it's resulting 04:49.340 --> 04:52.430 import path that it will have an outgoing path. 04:52.520 --> 04:55.520 So here's the command which I just mentioned. 04:55.520 --> 05:00.680 Note that we have just typed the most relevant information so that go get figures out the rest. 05:00.860 --> 05:06.080 You'll get some input depending on the state of the project but after a while it will disappear. 05:06.740 --> 05:15.020 But what did happen go has created a folder in go path slash source slash GitHub dot com slash core 05:15.170 --> 05:16.450 OS. 05:16.460 --> 05:18.830 It has closed the project in that location. 05:18.890 --> 05:22.550 So now the source code of ATC D is available at. 05:22.550 --> 05:29.870 Go path slash source slash get hub dot com slash core OS slash ATC D. 05:30.620 --> 05:32.890 So now we are within the directory. 05:33.000 --> 05:38.420 Call goes cloned any repository that ATC D could need. 05:38.630 --> 05:41.920 It has tried to install the project if it is not a library. 05:41.960 --> 05:50.360 This means it has generated a binary file of ATC D and has put it in go path bin folder by simply typing 05:50.360 --> 05:52.110 the go get project command. 05:52.220 --> 05:58.460 You'll get all that material from a project in your system then in your go apps you can just use any 05:58.460 --> 06:01.080 library by importing the path within the source. 06:01.550 --> 06:05.740 So let me show you how it will be for the ATC D project. 06:06.410 --> 06:12.560 It's very important that you get familiar with the use of the go get to and stop using get clone when 06:12.560 --> 06:14.900 you want to project from a get repository. 06:14.900 --> 06:19.550 This will save you some headaches when trying to import a project that isn't contained within your go 06:19.550 --> 06:20.980 path. 06:20.990 --> 06:23.600 Let's move on to encoding package. 06:23.810 --> 06:27.780 Have you realized that we have imported the packaging coding Jason. 06:28.130 --> 06:30.680 Why is it prefixed with the word encoding. 06:30.710 --> 06:36.350 If you take a look at go's source code to the source slashing coding folder you'll find many interesting 06:36.350 --> 06:43.600 packages for encoding or decoding such as x AML hex binary or even CSP. 06:43.880 --> 06:46.490 Now something a bit more complicated. 06:46.580 --> 06:49.220 Have a look at this piece of code which I just added 06:52.500 --> 06:54.040 let's run this. 06:54.120 --> 06:55.700 You can see the output. 06:55.980 --> 06:59.210 Conveniently it also works pretty well with structures. 06:59.370 --> 07:03.600 But what if I want to not to use uppercase in the Jason data. 07:03.600 --> 07:08.740 You can define the output input name of the Jason in the structure declaration. 07:08.880 --> 07:10.130 Let me show you how to do this. 07:10.800 --> 07:14.110 Okay let's execute this code. 07:14.190 --> 07:20.220 We have not only lowercase the names of the keys but we have even changed the name of the word key to 07:20.220 --> 07:27.930 string enough of marshalling we will receive Jason data as an array of bytes. 07:27.930 --> 07:30.670 But the process is very similar with some changes. 07:30.660 --> 07:33.280 Let's edit the main function. 07:33.330 --> 07:38.820 The big difference here is that you have to allocate the space for the structure first with a zero value 07:39.000 --> 07:45.570 and pass the reference to the method on Marshall so that it tries to fill it when you use on Marshall 07:45.780 --> 07:51.510 the first parameter is the array of bytes that contains the Jason information while the second parameter 07:51.510 --> 07:52.800 is the reference. 07:52.800 --> 07:57.490 That's why we are using an ampersand to the structure we want to fill. 07:57.490 --> 08:03.930 Finally let's use a generic map string interface method to hold the content of a Jason. 08:03.930 --> 08:07.080 Let's add the lines of code for this at the end. 08:07.150 --> 08:09.740 That said let's run this code. 08:09.870 --> 08:14.140 And here's the output what's happened to the result. 08:14.140 --> 08:17.620 This is why we described the object as dangerous. 08:17.620 --> 08:20.460 You can point to a nil location when using this mode. 08:20.470 --> 08:26.860 If you call a non-existent key in the Jason not only this like in the example it could also interpret 08:26.860 --> 08:32.300 a value as a float 64 when it is simply a byte wasting a lot of memory. 08:32.500 --> 08:39.640 So remember to just use map string interface when you need dirty quick access to Jason data that is 08:39.640 --> 08:46.360 fairly simple and you have under control the type of scenarios described previously wonderful in this 08:46.360 --> 08:46.930 video. 08:46.930 --> 08:51.070 We have learned to create and use libraries in the next video. 08:51.070 --> 08:53.260 We will dive into the go tools.