WEBVTT 00:00.380 --> 00:07.370 We are now at the fifth video of this section arrays slices and maps in the previous video. 00:07.410 --> 00:10.740 We looked at functions in detail in this video. 00:10.740 --> 00:14.130 We will see how to use arrays slices and maps. 00:14.130 --> 00:20.910 We will also learn to use the visibility attributes of a function along with zero initialization arrays 00:20.910 --> 00:24.730 are one of the most widely used types of computer programming. 00:24.750 --> 00:30.150 There are lists of other types that you can access by using their position on a list. 00:30.150 --> 00:36.900 The only downside of an array is that its size cannot be modified slices allow the use of arrays with 00:36.900 --> 00:42.630 variable size the maps type will let us have a dictionary like structure in go. 00:42.950 --> 00:45.330 Let's see how each work. 00:45.330 --> 00:51.410 So let's start with arrays on arrays a numbered sequence of element of a single type. 00:51.570 --> 00:59.940 You can store 100 different unsigned integers in a unique variable 3 strings or 400 bool values. 01:00.040 --> 01:01.890 This size cannot be changed. 01:02.010 --> 01:06.670 You must declare the length of an array on its creation as well as the type. 01:06.690 --> 01:10.110 You can also assign some value on creation. 01:10.200 --> 01:19.020 For example here you have one hundred int values all with zero as value or an array of size 3 with strings 01:19.110 --> 01:20.440 already assigned. 01:20.550 --> 01:24.680 With this line of code go is awesome at the positions. 01:24.690 --> 01:27.350 0 1 2 of the array. 01:27.630 --> 01:29.330 Let's see another example. 01:29.520 --> 01:34.280 Here you have an array of 2 bool values that we initialize later. 01:34.290 --> 01:38.730 0 is set to true and one is set to false. 01:38.730 --> 01:43.590 Now let's look into 0 initialization in our previous example. 01:43.590 --> 01:47.710 We have initialized an array of bool values of size 2. 01:47.850 --> 01:54.060 We wouldn't need to assign array at the position one to false because of the nature of 0 initialization 01:54.090 --> 01:55.430 in the language. 01:55.530 --> 01:59.880 Go will initialize every value in a bool or write a false. 02:00.060 --> 02:04.680 We will look deeper to 0 initialization later in this section. 02:04.750 --> 02:12.160 Let's talk about slices slices are similar to arrays but their size can be altered on runtime. 02:12.180 --> 02:15.570 This is achieved thanks to the underlying structure of a slice. 02:15.630 --> 02:16.880 That is an array. 02:17.040 --> 02:21.980 So like arrays you have to specify the type of the slice and its size. 02:22.020 --> 02:25.530 So use this line to create a slice. 02:25.560 --> 02:29.580 This command has created an underlying array of 10 elements. 02:29.610 --> 02:35.370 If we need to change the size of the slice by for example adding a new number we would append the number 02:35.370 --> 02:44.740 to the slice for example using this line of code the syntax of append is of this ORM array to dependent. 02:44.800 --> 02:47.080 Item 2 in square brackets. 02:47.110 --> 02:50.860 Item 2 append and returns the new slice. 02:50.860 --> 02:53.680 It does not modify the actual slice. 02:53.710 --> 02:56.320 This is also true to delete an item. 02:56.470 --> 03:00.710 For example let's delete the first item of the array. 03:00.790 --> 03:05.660 Yes like in arrays but what about deleting the second item. 03:05.740 --> 03:09.080 We use the same syntax as you see here. 03:09.100 --> 03:17.260 We take all elements from zero index included to the first index not included and each element from 03:17.260 --> 03:20.780 the second index included to the end of the array. 03:21.010 --> 03:25.680 Effectively deleting the value at the second position in the slice index 1. 03:25.720 --> 03:33.160 As we start counting with a zero as you can see we use the undetermined arguments syntax as the second 03:33.160 --> 03:36.810 parameter cool. 03:36.830 --> 03:39.950 Now let's explore about Maps. 03:39.950 --> 03:43.070 Maps are like dictionaries for each word. 03:43.130 --> 03:50.910 We have a definition but we can use any type as a word or definition and they'll never be ordered alphabetically. 03:50.930 --> 03:57.710 We can create maps of string that point to numbers a string that points to interfaces and struts that 03:57.710 --> 04:00.730 point to end and End Function. 04:00.780 --> 04:06.190 You cannot use as key slices the functions and maps. 04:06.330 --> 04:12.960 Finally you create maps by using the keyword make and specifying the key type and the value type. 04:13.000 --> 04:15.270 Let's see how we could do it. 04:15.270 --> 04:19.290 Here's the code for it when passing Jace on content. 04:19.320 --> 04:22.560 You can also use them to get a string interface map. 04:22.560 --> 04:29.380 Let me show you the code for this now the my Jason map variable is a map that will store the contents 04:29.380 --> 04:35.080 of a Jason and that we will need to pass its pointer to the UN Marshal function. 04:35.080 --> 04:41.680 The Jason data variable declares an array of bytes with the typical content of a Jason object. 04:41.680 --> 04:44.160 We're using this as the mock object. 04:44.350 --> 04:50.830 Then we unmask all the contents of the Jason storing the result of the memory location of my Jason map 04:50.860 --> 04:51.820 variable. 04:52.180 --> 04:57.970 After checking that the conversion was okay and the Jason byte array didn't have syntax mistakes we 04:57.970 --> 05:06.330 can access the contents of the map in Jason like syntax now that you know about arrays slices and maps. 05:06.330 --> 05:09.890 Let us understand what is visibility. 05:09.900 --> 05:16.040 So visibility is the attribute of a function or a variable to be visible to different parts of the program. 05:16.500 --> 05:20.160 So a variable can be used only in the function that is declared. 05:20.160 --> 05:24.160 If the entire package or in the entire program. 05:24.300 --> 05:27.780 How can I set the visibility of a variable or function. 05:27.960 --> 05:34.260 Well it can be confusing at the beginning but it cannot be simpler uppercase definitions are public 05:34.530 --> 05:38.670 which means visible in the entire program lowercase are private. 05:38.700 --> 05:44.520 That is not seen at the package level and function definitions which are the variables within functions 05:44.760 --> 05:47.730 of visible just in the scope of the function. 05:47.730 --> 05:51.660 Here's an example of a public function here. 05:51.670 --> 05:57.910 Hello world is a global function a function that is visible across the entire source code and to third 05:57.910 --> 05:59.950 party users of your code. 05:59.950 --> 06:06.490 So if our package is called Hello We could call this function from outside of this package by using 06:06.550 --> 06:15.670 Hello Dot hello world method as you can see we are in the different underscore package we have to import 06:15.670 --> 06:22.780 the package we want to use with the keyword import the root then is the past within your go path slash 06:22.780 --> 06:28.850 source that contains the package we are looking for this path conveniently matches the U.R.L. of a get 06:28.870 --> 06:36.310 hub account or any other concurrent version system or CBS repository lets me edit the path and make 06:36.310 --> 06:38.680 this as first section. 06:38.820 --> 06:47.530 Okay let's end this video by looking deeper into 0 initialization 0 initialization is a source of confusion 06:47.560 --> 06:52.180 sometimes they are default values for many types that are assigned. 06:52.180 --> 06:58.180 Even if you don't provide a value for the definition here are the 0 initialization for various types 06:58.540 --> 07:05.080 which you can see on your screen the 0 value of a structure is defined as the structure that has its 07:05.080 --> 07:13.150 fields initialized as 0 value to 0 initialization is important when programming and go because you won't 07:13.150 --> 07:18.690 be able to return a nil value if you have to return an int type or a struct. 07:18.760 --> 07:23.560 Keep this in mind for example in functions where you have to return a bool value. 07:23.650 --> 07:28.810 Imagine that you want to know if a number is divisible by a different number but you passed 0 as the 07:28.810 --> 07:29.870 divider. 07:29.890 --> 07:36.520 For example have a look at this piece of code the output of this program is false but this is incorrect 07:36.850 --> 07:39.910 a number divided by 0 is an error. 07:40.100 --> 07:48.910 It's not the 10 isn't divisible by 0 but that a number cannot be divided by 0 by definition zero initialization 07:48.910 --> 07:51.820 is making things awkward in this situation. 07:51.820 --> 07:54.310 So how can we solve this error. 07:54.310 --> 07:56.110 Let's consider this code. 07:56.500 --> 07:59.260 Here we we're dividing 10 by zero again. 07:59.260 --> 08:06.610 But now the output of this function is a number that cannot be divided by zero error captured the program 08:06.610 --> 08:09.700 finished gracefully in this video. 08:09.700 --> 08:17.050 We dived into arrays slices and maps in the next video we will be learning about pointers structures 08:17.260 --> 08:18.490 and interfaces.