So in this section of the course will be interacting with the database, which means that we need to
connect both our front and back ends to the database.
So what I'm going to do is go to my internal folder at the root level of my project, and I'll create
a new directory there, which are called Driver, and inside of that can create a new file, which I'll
call Driver Dutko.
And this will be where we connect to the database.
So let's give it a package declaration driver.
And in that, I'm going to have one function and I'll call that function open DB and I'm going to export
that function by giving it a capital letter as it's at the start of its name.
And I will take one argument, which will be DSN.
That's the database connection string and it's a string and it will return to things.
A pointer to sequel DB and I hit Enter to let Visual Studio Code import that for me and potentially
in error.
OK, now this database connection function is going to connect to my school or actually Maria DB, so
we need to import the necessary code for that.
So I'll put a terminal window and I will type go get GitHub dot com go dash sequel dash driver slash
my screw up and that will go and get the necessary driver for us.
So that means we need to actually modify our import statement.
So I'll surround the whole thing in parentheses like this.
And at the very bottom of the of those parentheses just before the closing one, I'll import what I
need with an underscore the blank identifier that says I'm not calling this directly, but I'm going
to use it.
GitHub dot com slash go dash sequel dash driver slash my school.
OK, so now we can actually connect to the database and it's really simple and you can go read the documentation
right at the URL there from line six.
But I'm sure you've done this before.
DBI and error are assigned the value of from the school package.
We're going to open and we're going to open a minuscule connection and we're going to pass that our
connection straight down and we'll check for an error.
If error is not equal to nil, then we'll just return nil because we have no connection pool and the
error.
And then just to be absolutely safe, updating the database error equals DB ping.
And if error is not equal to nil, then I'll print the error out format of print line error and I'll
return nil and the error.
And finally, if we've managed to ping it, then we can return our database connection pool and no error.
OK, so now we have a driver package so close by terminal.
And let me first of all, hook up Mingo.
So that's inside our command web and there it is, Mingo.
So I'm going to read the connection string right from a flag of the we're going to pass to this.
And if you want to use an environment variable, that's fine, too.
But I'm going to read it right from the command line for now.
So I'll say flag.
Scott Stringer, I'm going to read it into our CFT DB, populate our config variable and the flag will
be called down and I'll give it a default value so I can work in development without any difficulty.
And my connection string looks like this.
My username, which I called Trevor Colen my password and I chose a secret which isn't very secure,
but I'm in development and I don't care at TCP.
That's how I'm going to connect and I'm connecting to localhost Colen three three oh six.
Then close my parentheses and I've already created a database and I'll call my Kullman widgets because
we're selling widgets, whatever those happen to be.
And then because I'm working in Go with my school or Marea DB which is compatible with my school, I'll
put the question mark and and hand at the flag pass time with a capital T equals true and then I'll
put an ampersand.
RTLS equals false.
So I'm not using SSL and the connection help text is just dsf.
OK, so that actually will read in the DSM our connection string.
And of course yours will be different than mine, but it will follow this basic format that we have
here in line fifty seven in my code.
Now after that happens, say right after I declare my logger's here on line sixty five and sixty six
in my code, let's actually try to connect to the database.
So I'll call my variable con and I'm going to check for an error.
And that's a sign the value of from the driver package dot.
Open divi.
There it is and I'm going to hand it cfg dot divi dsn and I'll check for her.
If error is not equal to nil, then error log dot print one error.
Actually, that's just stop at this point.
We'll make that fatal instead is that I really don't want to go any further if I don't have my necessary
connection to the database.
OK, and then we'll defer conduct close because we don't want to leave our resources open and I want
that connection pool to remain open until the application stops.
So at this point, I should be able to open my terminal, clear the screen and try running this.
So I'll just run it like this make start front and see if it works.
And I have a typo and it's an easy one.
So line eighty five right here, that has to go away.
OK, let's clear the screen and try that again.
Make start front.
And we connected to our database perfect, so now we have a means of connecting to our database and
of course, that means we have to do the same logic in our API Dongo file.
And that's pretty straightforward.
Let's just stop this so mixed up.
Everything is stopped and I'll open my API and let me copy this line first, because I know I'm going
to use it.
So we'll open API Dargo
and write in the main function.
I'll put it right here paste and get rid of the blank line and go back to main Dongo and just copy the
same logic right here.
And modify as necessary, so back in April, go, we'll put it in the same place, paced, and I need
to import that driver, so I'll just go copy it from the import statement in mean, go right here and
back in.
Go.
I'll put it right here.
So that makes that area go away.
And there should be one more error right down here.
So at this point I can clear the screen and type and make start and I should start both the front and
back end.
And it did perfect.
So we have a connection to the database now.
We have nothing in the database and we're going to have to create a few tables and we'll get started
on that in the next lesson.