Hello and welcome to the Golang programming language course.
In the previous session, we talked about rest APIs concept, and in this session we want to talk about
the other concept.
Now, it's time to do a project in decision to practice this section.
We want to establish a connection between the client and the server and the database through the rest
APIs and go link.
It is important to note that the client cannot communicate directly with the database, but the client
is connected to the server and the server is directly connected to the database.
The client sends its request to the server and the server analyzes Dan and sends the appreciated request
to the database and receives the response from the database and sends its response to the client.
This program consists of two main layers a database layer and the rest layer.
In this example, we have a person database that contains a student table, which contains table information
like Aidid name and age that we have to perform crowd operation using my school database.
Therefore, we will use the student table in the person database that we created in previous courses
to connect to the GO program.
Now let's go to the school program to implement this project.
First, go to Package Explorer and go to the source folder and create a project folder named like Rest.
Because in this tutorial, we use school database.
We need a driver to connect to my school.
So first, we should prepare and install my school drive in our project.
So run the MySchool Driver installation comment and turn first.
Go to Project Folder.
See the list.
Go get dash you key top.
That call is let's go.
That's Samuel Dash Driver.
This les my cue.
OK.
The driver has in the.
It's time go to create database live, so go to Project Folder and create another folder name like database.
Later this.
We want to create the database connection settings as a Jason Fine, so go to the database folder and
create another folder named like configuration.
And the reason.
And inside the configuration folder, we create the find called config with the JSON extension, which
is supposed to include the settings for the connection of the Go program with the MySchool database.
We got Jace.
The program requires two main settings to connect to the database.
First is driver name and second is data source name.
Driver name is my secure and data source name is a combination of username, password and database name
that we want to connect it.
The pattern of the data source name is database username, column database passport.
Assign this database name, so overwrite adjacent file for settings of database calibrations.
Driver name
column In this case, my driver name is Mica said on and go to next line and data source name data.
Source two Main column My database username is root
and my database passport.
Here is Oracle.
At Sign Class and database name person,
now we could create a JSON file for connection to my skill database savings.
It's time go to create a model, so in database folder, create another folder name like model.
Model in model folder, we create a file like named a student or go inside it, and we are going to
create a structure equivalent to a student, tailored my Oscar database.
So create a fine line student that go.
So go to a student, find and create this, try and name a student with fields such as fields are stored
inside the MySchool database.
So right package
model
type student strong.
Set a common for this as strong as exporter.
Student.
And define false I.D. prices in name type is strange.
And each time is it's OK now it's time to implement database methods in project folder, go to database
layer and create another folder named like DV tools and create a file inside it like named DB Connection
that go.
That file contains methods to interact with the database, create a folder like DV tools and create
a file like DV connection.
DV connection that glue.
Before creating any connection, there are two things to do.
First, import MySchool driver, top of database tools package, so the right package.
Database
import.
Underlying guitar dot com.
Let's let's go.
Dash secure dash driver discussion my skewered
second define global variable named driver name and data source name as Airstream type, that driver
made history and their data source named as best.
Now create and initialize a function to be able to initialize these two variables from other packages.
This method receives two values of driver name and data source name as input parameters and uses them
to initialize the global variable of package until that other function can access their values.
So right, func db initialize.
RD and as driver name and D as an as driver soul's name by the time is string.
Initialize the global variable, but this variable driver name equals DNA, and in the next line, data
source name equals the S10.
Now we want to design a method whose task is to connect the program to the MySQL database and return
a database object from IS to a package.
For example, we create the function named Connect that is stored by the lowercase character because
we want to use this function just to this package, and we don't use it to either package.
So we write
phone
next.
And return type is the object by the time it's El DB DB point of secure that
theory.
The required metal to connect to the database is the open function of the school package.
So we should import the school package up function opens a database is specified by its database driver
name and a driver specific data source name usually consisting of apps list a database name, connection
information and it returns VB object and it will so first define two variables like Debbie and Arrow.
Calling equals in calling open function from a school package and set driver name and data source name
and parameters.
School that open and set driver name and data source may.
Now, check your status if
error not equals mean, lock the fatal
error darts.
Terror.
And return Creedy object as return type, return de.
OK.
In order not to prolong the time of decision, we will finish it and we will continue the content in
the next session.
Goodbye.