This is the eighth video of this section libraries in the previous video.
We learned how to perform testing on the functions and do test driven development.
In this video we will have a look at the GO GET tool and the encoding package.
Until now most of our examples were applications.
An application is defined by its main function and package but with go you can also create pure libraries
in libraries.
The package need not be called Main nor do you need the main function as libraries aren't applications.
You cannot build a binary file with them and you need the main package that is going to use them.
For example let's create an arithmetic library to perform common operations on integers sums subtractions
multiplications and divisions will not get into too many details about the implementation to focus on
the particularities of those libraries.
So let us create the package in the Nano.
First we need a name for our library.
We set this name by giving a name to the entire package.
So this is located within libraries and inside this I have named it as arithmetic not go.
This means that every file in this folder must have this package name too and the entire group of files
composed the library called arithmetic too.
In this case because it only contains one package.
This way we won't need to refer to the file names for this library and to provide the library name and
path will be enough to import and use it.
We have defined a sum function that takes as many arguments as you need and that will return an integer
that during the scope of the function is going to be called rez.
This allows us to initialized to 0 the value we're returning.
We defined a package not the main package but a library one and called it.
Arithmetic as this is a library package.
We can't run it from the command line directly so we'll have to create the main function for it or a
unit test file.
For simplicity we'll create a main function that runs some of the operations now.
But let's finish the library first.
So let's add another function that is subtraction function to our library.
The subtraction code will return 0 if the number of arguments is less than zero.
And the subtraction of all its arguments if it has two arguments or more.
Now let's add multiplication the multiply function works in a similar fashion.
It returns zero when arguments are less than 2 and the multiplication of all its arguments when it has
two or more.
Finally the division code changes a bit because it will return an error if you ask it to divide by zero.
So let's write the function for division.
So now we have our library finished but we need a main function to use it as libraries could not be
converted to executor all files directly.
Now let's create a main function.
Here's how our main function looks and the path should be similar to this.
We are performing an operation over every function that we have defined.
Take a closer look at the import clause.
It is taking the library we have written from its folder within.
Go path that matches its you are L and get hub dot com.
Then to use every one of the functions that is defined within a library.
You have to name the package name that the library has before each method let's recall some naming conventions
about libraries.
Each file in the same folder must contain the same package name files don't need to be named in any
special way.
A folder represents a package name within a library the folder name will be used on import parts and
it doesn't need to reflect the package name although it's recommended for the parent package.
A library is one or many packages representing a tree that you import by the parent of all packages
folder you call things within the library by their package name now let's step ahead and look into the.
Go get to go get is a tool to get third party projects from CBS repositories instead of using the get
clone command.
You can use go get to receive a series of added benefits.
Let's write an example using core OS is ATC D project which is a famous distributed key value store
core OS as ATC D is hosted on GitHub at get hub dot com slash core OS slash ATC D dot get to download
this project source code using the go getters tool we must type in the command terminal it's resulting
import path that it will have an outgoing path.
So here's the command which I just mentioned.
Note that we have just typed the most relevant information so that go get figures out the rest.
You'll get some input depending on the state of the project but after a while it will disappear.
But what did happen go has created a folder in go path slash source slash GitHub dot com slash core
OS.
It has closed the project in that location.
So now the source code of ATC D is available at.
Go path slash source slash get hub dot com slash core OS slash ATC D.
So now we are within the directory.
Call goes cloned any repository that ATC D could need.
It has tried to install the project if it is not a library.
This means it has generated a binary file of ATC D and has put it in go path bin folder by simply typing
the go get project command.
You'll get all that material from a project in your system then in your go apps you can just use any
library by importing the path within the source.
So let me show you how it will be for the ATC D project.
It's very important that you get familiar with the use of the go get to and stop using get clone when
you want to project from a get repository.
This will save you some headaches when trying to import a project that isn't contained within your go
path.
Let's move on to encoding package.
Have you realized that we have imported the packaging coding Jason.
Why is it prefixed with the word encoding.
If you take a look at go's source code to the source slashing coding folder you'll find many interesting
packages for encoding or decoding such as x AML hex binary or even CSP.
Now something a bit more complicated.
Have a look at this piece of code which I just added
let's run this.
You can see the output.
Conveniently it also works pretty well with structures.
But what if I want to not to use uppercase in the Jason data.
You can define the output input name of the Jason in the structure declaration.
Let me show you how to do this.
Okay let's execute this code.
We have not only lowercase the names of the keys but we have even changed the name of the word key to
string enough of marshalling we will receive Jason data as an array of bytes.
But the process is very similar with some changes.
Let's edit the main function.
The big difference here is that you have to allocate the space for the structure first with a zero value
and pass the reference to the method on Marshall so that it tries to fill it when you use on Marshall
the first parameter is the array of bytes that contains the Jason information while the second parameter
is the reference.
That's why we are using an ampersand to the structure we want to fill.
Finally let's use a generic map string interface method to hold the content of a Jason.
Let's add the lines of code for this at the end.
That said let's run this code.
And here's the output what's happened to the result.
This is why we described the object as dangerous.
You can point to a nil location when using this mode.
If you call a non-existent key in the Jason not only this like in the example it could also interpret
a value as a float 64 when it is simply a byte wasting a lot of memory.
So remember to just use map string interface when you need dirty quick access to Jason data that is
fairly simple and you have under control the type of scenarios described previously wonderful in this
video.
We have learned to create and use libraries in the next video.
We will dive into the go tools.