We are now at the fifth video of this section arrays slices and maps in the previous video.
We looked at functions in detail in this video.
We will see how to use arrays slices and maps.
We will also learn to use the visibility attributes of a function along with zero initialization arrays
are one of the most widely used types of computer programming.
There are lists of other types that you can access by using their position on a list.
The only downside of an array is that its size cannot be modified slices allow the use of arrays with
variable size the maps type will let us have a dictionary like structure in go.
Let's see how each work.
So let's start with arrays on arrays a numbered sequence of element of a single type.
You can store 100 different unsigned integers in a unique variable 3 strings or 400 bool values.
This size cannot be changed.
You must declare the length of an array on its creation as well as the type.
You can also assign some value on creation.
For example here you have one hundred int values all with zero as value or an array of size 3 with strings
already assigned.
With this line of code go is awesome at the positions.
0 1 2 of the array.
Let's see another example.
Here you have an array of 2 bool values that we initialize later.
0 is set to true and one is set to false.
Now let's look into 0 initialization in our previous example.
We have initialized an array of bool values of size 2.
We wouldn't need to assign array at the position one to false because of the nature of 0 initialization
in the language.
Go will initialize every value in a bool or write a false.
We will look deeper to 0 initialization later in this section.
Let's talk about slices slices are similar to arrays but their size can be altered on runtime.
This is achieved thanks to the underlying structure of a slice.
That is an array.
So like arrays you have to specify the type of the slice and its size.
So use this line to create a slice.
This command has created an underlying array of 10 elements.
If we need to change the size of the slice by for example adding a new number we would append the number
to the slice for example using this line of code the syntax of append is of this ORM array to dependent.
Item 2 in square brackets.
Item 2 append and returns the new slice.
It does not modify the actual slice.
This is also true to delete an item.
For example let's delete the first item of the array.
Yes like in arrays but what about deleting the second item.
We use the same syntax as you see here.
We take all elements from zero index included to the first index not included and each element from
the second index included to the end of the array.
Effectively deleting the value at the second position in the slice index 1.
As we start counting with a zero as you can see we use the undetermined arguments syntax as the second
parameter cool.
Now let's explore about Maps.
Maps are like dictionaries for each word.
We have a definition but we can use any type as a word or definition and they'll never be ordered alphabetically.
We can create maps of string that point to numbers a string that points to interfaces and struts that
point to end and End Function.
You cannot use as key slices the functions and maps.
Finally you create maps by using the keyword make and specifying the key type and the value type.
Let's see how we could do it.
Here's the code for it when passing Jace on content.
You can also use them to get a string interface map.
Let me show you the code for this now the my Jason map variable is a map that will store the contents
of a Jason and that we will need to pass its pointer to the UN Marshal function.
The Jason data variable declares an array of bytes with the typical content of a Jason object.
We're using this as the mock object.
Then we unmask all the contents of the Jason storing the result of the memory location of my Jason map
variable.
After checking that the conversion was okay and the Jason byte array didn't have syntax mistakes we
can access the contents of the map in Jason like syntax now that you know about arrays slices and maps.
Let us understand what is visibility.
So visibility is the attribute of a function or a variable to be visible to different parts of the program.
So a variable can be used only in the function that is declared.
If the entire package or in the entire program.
How can I set the visibility of a variable or function.
Well it can be confusing at the beginning but it cannot be simpler uppercase definitions are public
which means visible in the entire program lowercase are private.
That is not seen at the package level and function definitions which are the variables within functions
of visible just in the scope of the function.
Here's an example of a public function here.
Hello world is a global function a function that is visible across the entire source code and to third
party users of your code.
So if our package is called Hello We could call this function from outside of this package by using
Hello Dot hello world method as you can see we are in the different underscore package we have to import
the package we want to use with the keyword import the root then is the past within your go path slash
source that contains the package we are looking for this path conveniently matches the U.R.L. of a get
hub account or any other concurrent version system or CBS repository lets me edit the path and make
this as first section.
Okay let's end this video by looking deeper into 0 initialization 0 initialization is a source of confusion
sometimes they are default values for many types that are assigned.
Even if you don't provide a value for the definition here are the 0 initialization for various types
which you can see on your screen the 0 value of a structure is defined as the structure that has its
fields initialized as 0 value to 0 initialization is important when programming and go because you won't
be able to return a nil value if you have to return an int type or a struct.
Keep this in mind for example in functions where you have to return a bool value.
Imagine that you want to know if a number is divisible by a different number but you passed 0 as the
divider.
For example have a look at this piece of code the output of this program is false but this is incorrect
a number divided by 0 is an error.
It's not the 10 isn't divisible by 0 but that a number cannot be divided by 0 by definition zero initialization
is making things awkward in this situation.
So how can we solve this error.
Let's consider this code.
Here we we're dividing 10 by zero again.
But now the output of this function is a number that cannot be divided by zero error captured the program
finished gracefully in this video.
We dived into arrays slices and maps in the next video we will be learning about pointers structures
and interfaces.