Hello and welcome to the goal and programming language course in the previous session.
We have been able to design the database there.
Now it's time to create the rest API layer before implementing the rest layer.
You must first install Gorilla Marks tool to do this.
Go to the terminal and execute the following comment.
Go
get gas.
You get half dot com slash gorilla
slash marks.
OK, the government's package was in a stall, and now you will continue.
So go to Project Folder and create another folder named like Rest Layer, which is supposed to include
the rest files for the connection of the Go program with the rest APIs.
Create a folder rest layer.
This layer consists of two parts, one part is related to rest configuration and the other part is related
to rest functions.
First, we create the file related to the rest functions, so go to Project Folder and go to rest players
and create the go blank fine name like rest functions that go create the file list functions that go.
The package name, package rest claim first, the implement, a function that is supposed to take the
name of the student through the endpoint, then search it in the database.
And if that student exists, returned the information of that student to us.
So define the function.
Then like select the student based name phone.
Select a student based name this function.
Accept two parameters first and object from the a response writer and second, a pointer of each type
of request.
So we write
response as a student
response writer
and set the pointer parameters the prerequisites request.
As Point Rob Typekit request
now want to implement that body of the function?
This function is supposed to return the information of a student based on the name it receives from
that endpoint.
So to access the parameters in the.
We have to do this using Mox, so we write marks.
In calling vast function and sit requisites as parameter for each virus.
And it requires that parameter for its last function, returns the road variables for the current request.
For example, in this case, our endpoint parameter is name that define in risk config alter.
The return value of this function is a map who is key is the name of the parameters in the endpoint,
and its value is the value based on which the key is to be returned.
So defining a variable like that as a return type.
So define a variable launch.
That column equals
know all the keys in the endpoint are inside the vast value of.
We can access their venues by calling each key.
In this example, we have defined only one parameter as name, in our view.
So there is only one name key inside the vast variable, which we can display the value of that can
as follows first, define two variable for return types like name and OK, so write name comma OK.
Column equals calling values, variable values and sit and point parameter as index for it.
In this case, our euro parameter name is name.
OK.
This value of the variable Auchi indicates the presence or absence of a return value if only returns
the true value.
It means that the return value exists and if it returns the false value, it means that the key has
no venue.
Therefore, we checked the value in the key through the variable OK.
And if it does not exist, we should send an error message to the client.
So we write, if not, OK.
Response.
Start calling right, Heather, function in the city's status court right here,
right?
Heather.
For example, a status battery course from the HTP package is to debate what the state rules that request.
Right, Heather, function since its type of response, Heather, with the provided the status quo and
calling it prindle and fonctionne for displaying the arrow to client if A. Don't give preened Alan and
sandy response.
And sets, for example, a student not found.
Now it's time to connect to the database, then call the query to call the student based search by name
and fetch the return value, which is a student.
So we define a variable like the student student column equals DB tools dot and from the DB tools invoking
select the student based name function select.
A student based name function, and that name is arguement for it name.
Now we have to sand the return value of the select method to the client, which is an object from the
student, but you have to convert the object to JSON in sent it to the client as JSON format that do
this through the JSON packet.
So right, Jason
Dauth, calling new encoder function, instead responds as parameter for each new encoder, and that
response is our human for it.
This function returns a new encoder that rice writer and calling input function in such a student as
argument for it.
But ENCODE said the student as argument.
This writes to Jason, including of A-student student object to the extreme, followed by a new line
character.
So we were able to implement the method of selecting a student based on name from the database and sending
it into the client, the rest API in order not to prolong the time of decision.
We will finish it and we will continue the content in the next session.
Goodbye.