Hello and welcome to the Golang programming language course.
Go line crowd using my school.
We are going to see an example program to learn how to do database crud operations using gold language
and my school database.
We have talked about it before, which is an acronym for Create, Read, Update and let current operations
are basic data manipulation for databases.
In this example, we are going to create an interface as database front end to handle these operations.
We have a student table containing tainted information like ID, name and age.
With this stated, we have to perform current using my school in previous sessions.
We succeed to create a student table in the MySchool database and perform craft operations on it.
So we use the same student table built into the MySQL database in previous sessions within this project
to connect the Go Languages program.
Database driver in a computer system and adopter program is required for making a connection to another
system of different type.
Similar to connecting your printer to a computer by using a printer driver, a database management system
or DBS mess needs a database driver that enables a database connection in other systems.
A database driver is a computer program that implements the protocol for a database connection the driver
walks like, and the doctor each connects a generic interface to a specific driver vendor implementation.
Now, let's go to the escort program to implement this project.
Because in this tutorial, we used my school database.
We need a driver to connect to my school.
So first, we should prepare and install my school driver in our project for this purpose.
Using Terminal at first Install Driver four goes my is dual database package.
So run this command and install my ISCA driver.
Go get.
That's you.
Tom, dot com slash go the ranch school dash driver, slash my rescuer.
And, Ron, this comment.
OK, first, go to Package Explorer and go to a Sazi folder and create the project folder name like
database.
Create a folder log data base
in project folder.
Create a folder named model.
In model folder, we create a file like name the student that go inside it.
Student the school.
We are going to create a structure equivalent to a student table in the MySchool database.
So go to a student file and create a strike name, a student with foods such as foods is stored inside
the manuscript database.
Typekit mother.
And create a structure like a student tied.
Student district
and city fields for it.
I.D. type is in
name, type is strange
and age tie these parents.
And so the comment for this, as throughout as exporter, is to the best of the format, the code and
save the product.
Now Go Project folder and create another folder named like configuration, which is supposed to include
the settings for the connection of the Go program with the MySchool database.
New folder on immigration.
He want to create a database connection city as a Jason fine.
So inside the configuration folder, we create a file called config with the JSON extension, so create
a file config dot js.
The program requires Tollman settings to connect to the database.
First is a driver name and second is data source name.
Driver name is MySchool and data source name is a combination of user name, passport and database name
that we want to connect in.
So we write adjacent fine for settings of database.
Driver named.
Column and said, Venue My Askew, a sitcom, I'll go to next month.
Said another key like data source, meaning column and set value.
My database username is root column and my database passport is where I can.
At some.
Yes, slash and database name is person.
Now we could create the JSON file for connection to my database.
Now go to Project Folder and create another folder named like DevTools.
DBE tools.
And create the file inside ID like name, the connection that go, that file contains methods to interact
with the database, create a new Find DB connection.
That's cool.
Before creating any function, there are two things to do.
First, in Port My Escort Driver, top of database tools.
Package
package
tools.
In Port Open and close, Francis Andre School double quotation mark
guitar, dot com slash go.
Dash this dual dash driver slash my skewered.
And second, define global variable named driver meet and data source name as string type that through
either me type is extreme and that data source name and type is strange.
Now create and initialize their 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 functions can access their values.
So define foreign.
The the initial laser.
Sit down as driver me and this and as Driver Soul's name by the time estranged and create body a statement
driver name equals the end and datasource name equals the as in.
Reformat the code and say the projects now you want to design a method whose task is to connect the
program to the MySchool database and return the object from school package.
For example, we create the function named Connect that is stopped by the lower cheesecake there because
we want to use this function just to this package and we don't use it to other packages.
So we write phone.
Can't.
And return type is the object by the type, it's cruel that D.B..
The asterisk is fuel package Dot TV.
They required Metro to connect to the database is the open function of the estriol package.
So we should import a school package.
Open function opens a database is specified by its database driver name and the driver specific data
source name, usually consisting of at least a database name in connection information.
And it returns the object then.
So first define two variables like DB and or DB comma there column equals in Collingwood and function.
Form a school package and set driver name and data source name as prior to eschew a dart open driver
name and data source name.
Now, check in on our status
and.
Carl Fattal function from Lok Nayak is not that at all.
And sit at all, don't ever function.
And Return created Debbie Optic as returned times return TV, reformat the code and save the project,
OK?
After we can connect to the database, we can perform current operations by writing different functions
in order not to prolong the time of decision.
We will finish it and we will continue the content in the next session.
Goodbye.