Press "Enter" to skip to content

Web Apps with Go & Postgres


this video is brought to you by Greater
Commons greater learning greater
opportunities greater life visit us at
Greater Commons comm so I want to show
you how to do a basic web app using the
go programming language and I’m just
going to introduce you to the resources
and give you all the code that you need
and get you up and running and you’ll be
able to do create read update delete and
you’ll be able to use Postgres to do it
and and it will all be with the go
programming language but I want you to
like this video and I want you to
retweet this video and I want you to
share it on social networks because here
on YouTube these are the videos in my
channel and in this video here build web
apps go language 135,000 people have
watched it and that’s an OK video but I
made that like a year ago and so I
really need this video here to be what
people are watching because this video
and the material I have to share with
you is way way better and I’ve been
teaching go and web dev would go at the
University and the college level for a
couple years now and I’ve worked with
Caleb doxy and Bill Kennedy right just
to get the go code perfect and here’s
the code base you know the resource
here’s here’s the code pathway for
learning go web dev and this is the best
code pathway in the entire world
and I know because this is what I do
full time I study go I study go web dev
and I teach it at the college of the
University and I’ve worked with you know
Caleb dachsie like created the
foundation of this code base and we’ve
built upon it so it’s an amazing code
base it’s the best code base in the
entire world screaming web dev let’s go
so just discovering this like just tweet
this video so people can discover this
because I want people to get into the
gogo language I love it and I want to I
want to spread the gospel of go and and
so people have a good way to learn it
and get into it they’ll be more likely
to use it I’m not paid by Google either
by the way
so down here is Postgres we’re going to
like do this post grass and look at this
code that’s when we’re going to look at
but in post grass and you can go install
post grass and Postgres website alright
I’ve lost my voice I just got to talk
quiet I guess but down here you hit
download and if you’re on a Mac you just
want to get this one right here that’s
light and that’s easy and if you’re on a
Windows you could you can watch my
training where I show you how to do all
this my training you can find it for
migrator comments comm look there first
we’re building a site all and go and
it’s a place where you’ll be able to
teach and and and you know deliver
educational content very economically to
help people learn for the greater good
of humanity greater Commons so if it’s
not if it hasn’t launched yet the course
will be up there when this launches
right now you can also find the course
on Todd MacLeod view to me so just go
google that and you’ll get to the course
go web dev and that shows you how to do
it on Windows Postgres and everything
all right so let’s see it so you get
Postgres installed and then you want to
create a database a book store so you
can see there i have a database
bookstore that’s right there that’s my
database if we go go into in the
bookstore and then take a look at you
know it’s got one table books so we can
select everything from books right and
there what did I mess up select star
from books select let’s just try it
again
sometimes my terminal does that see same
command and in there out so there are my
three records I got three records in
there let’s look at the code and yeah
it’s working on us on a map Mac I just
wander if I to pull it but here here we
have the code and this is the exact same
code which is right here right exact
same code it’s probably I can note but
I’m in that so you have access to this
code did I already show you the URL
there’s the URL
so you go into this code and then here
all the steps to build it up and this is
like you know an hour to of training
right there so I’m not going to go
through all of it but this is the
finished deal let’s just watch it run
and then I’ll show you the code walk you
through the code so I’m going to switch
into that over here on this side I’m
already there yeah I’m in 44:22 so I’m
going to run that and we’ll come over
and take a look at it and so yeah there
are those three records we saw and I
could insert a book but I don’t want to
have to think of an ISBN number and so I
don’t know what’s the title of a book
West with night a Beryl Markham if you
have not read this book I will not be
steering you wrong go get this book this
book is like one of the best books I’ve
ever read in my life it’s so great I
created that book showed the record go
back to all books it’s in there I can
update it if I want
that’s like I’m a blues singer or some
so change numbers out of the West with
the night Beryl Markham let’s say we’re
making it on sale so now it’s 1499 go
back to all books and Emma Jane Austen
who wanted to read that delete that we
got crud created read update delete and
it’s all restful right we go look at a
book books show nice URLs drop the drop
to you know some value in there URL
parameters so look at that code right
here you know I could go into this in
quite a bit of depth so I was the first
place to start if you don’t know any go
web dev it’s a really it’s a kind of an
entire process so I
think I’m just going to keep this video
short because this would be about 40
minutes for me to walk you through all
this but I think that this is the best
example this code for learning how to do
go web dev I kind of want to go into it
I kind of feel you say man go into it
let’s go into it so if you don’t if if
this doesn’t make sense to you I’ve got
an introduction to the go programming
language and I’m just giving me the
resources and I’m not trying to sell you
something I mean obviously this is also
how help support my family got two kids
and but if you don’t have the resources
the money the course is usually like ten
or fifteen dollars if you don’t have the
money to pay for that just send me an
email on Twitter message me on Twitter
and here’s my Twitter deal and at Todd
McCloud I guess the shade URL that’s
better send me a message on Twitter and
just interesting god I totally want to
take course but I’m college to it in our
live in India or you know I don’t have
that money whatever I don’t care what
your reason is you don’t have the money
that’s fine I’ll give you free access I
want you to learn it for the good of all
for the greater good the greater cons
that’s why we’re building that site so
just send me a message and I’ll give you
free access but I have an introduction
of a go programming language to teach
you everything all right so really go
look at the net HTTP package so if this
doesn’t make sense
take that introduction course and here
in the net HT package we have type
Handler and type handler is an interface
okay so an interface allows polymorphism
and go so I click on that and I see type
handler and anything that has this
method implicitly implements the handler
interface and that means that if I have
some type and it has a method and that
method has this signature and that a
value of that type also is of a handler
type
and and so that’s how the interfaces
work and again go watch the introduction
training it doesn’t make sense because
I’m going to try to keep this a little
brief you got a little bit of like a
hangnail I’m not totally weird all right
so that’s the handler interface and
serve HTTP response writer an employer
to request my joke which I tell on class
is what is a interface say says hey baby
if you’ve got these methods then you’re
my type so the handler interface and the
reason the handler interface is the
entry point for understanding the net
HTTP package is because if you look up
here handlers are used all over you see
them in the index of all the different
functions and things it’s like it’s used
all over handler and their handler and
particularly here right if we look for
listening served there’s listen and
serve and it takes an address in the
handler and so that handler right here
that we pass in right that’s going to be
our server or serve MUX at the false or
MUX or MUX whatever you want to call it
and so if we pass in nil as I was
passing in so let’s just go down here
and look at it and we’ll search for nil
so there’s no and if you pass a nil you
use the default serve MUX okay so the
default serve MUX well with the default
serve MUX we can use handle and handle
func handle and handle fog so handle and
handle func registers the handler
function for given pattern in the
default serve MUX the defaults for MUX
is your server so you can say server you
can say router you could say serve mug
so you say mugs it’s all pretty much the
same the documentation for serve MUX
explains how patterns are matched let me
just show you so the starting point is
HP listen and serve port 8080 mill right
meaning to fall serve MUX and with the
fall serve MUX we can use handle func
HTTP handle func handle func wants a
pattern and the string and then a
function with that signature that is a
type in go you can look at the language
speculative speculation you can look at
the language spec here’s the language
spec and in the language spec you can
see all the different types including
function types first-class citizens
right so we’ve got a type here go back
to the documentation which is a function
with that signature I’m in the pantry by
the way because my kids are asleep I
don’t want to make too much noise so
it’s like my little sound booth so
handle func takes the pattern string
which is the route and then it takes a
func with this signature so there’s my
first func with that signature func
index response writer pointer to request
response write a pointer to request said
now I’m wrapping up my whole deal so
this route run this code this route run
this code this route run this code
that’s it
these route from this code it’s totally
cool
so HB redirect here just to keep it easy
keep it restful keep the route to
meaningful HP redirect can redirect
takes the response writer and by the way
this is how servers work client-server
architecture right it’s like a request
comes in you write a response the
request comes into the server to the
server writes a response so that’s why
this wants that signature because it’s
like okay requests came in we got a
right response requests came in got a
write a response right that’s why you
need to function with that signature so
redirect takes the response to requests
where you want to send and then the
status code and that’s just a constant
says you know hey it’s 303 the other
which means if you come into default you
end up getting taken to route books and
that’s where you go and that serves this
one looks index so if the request method
is not yet you’re out of here each to be
err can respond and the response writer
and then the status text and and status
method not allowed and then we return so
if you try to post something to this
route now we don’t take that method here
when we get and then we do a database
query and this DB here that comes from
go doc I don’t know how much longer I
keep going go dog database sequel all
right and so here we have the DV stock
and you look up at the top package
sequel provides a generic interface
around sequel databases must be using
injunction with a database driver just
your drivers car drivers let you drive
something let’s go driving and here’s
for Poe’s grass so I like this one here
we go and you just get that driver get
back here so on TV and
we can do a query and a queries right
here and here’s the query string where
you put in your sequel
you got any arguments it will take an
unlimited 0 to unlimited number of
arguments gives you back a pointer to
rows so you just get the address back
it’s pointer to an address for all the
values are stored get a pointer to rows
and then there with a pointer to rows
you have all these methods available to
you so you can do next and scan so you
can do a for loop because next returns a
bool so you get for loop over the next
next next and you can scan and get the
values and put it into the destination
can we put it into the destination you
need that pointer like I said if you
don’t understand it could let go coarser
so how is that use TB right it goes up
here up here I have bar DB and from
package sequel right DV I think I might
set package TV earlier so that was just
me being tired DB query you know from
that DB query so pointer to DB x full
query because let me just explain ER
make sure it’s clear
alright so package sequel package sequel
package sequel I do open I get a pointer
to a DB see that open pointer to DB and
open takes a driver name and then like
how do you connect to that database and
that’s all specified and whatever driver
you’re using so up here I create VAR DB
from package sequel type DB a pointer to
it right and then here I do from package
sequel open from package sequel open and
then I pass in the driver name Postgres
and I pass in the datasource stuff so
here it is it’s how you connect Postgres
close grass username password then all
that stuff move this over come to reason
that
and so now DB when you do open it gives
you back a pointer to a DP with a
pointer to DB a value of type DB to be
specific with my language when you have
a pointer to type DB you have all these
methods available I can now run query
this one right here so I have a pointer
to DB here I ping it just to make sure
we really connect to the database and
what the pointer to DB I can do that
query pass in my sequel and when I do
that gives me back roses in there check
my air right here there are my rows and
then I do my four rows next so that
returns a bool long as there’s another
record it’ll be true true true false no
more records were done and then I do
rows scan and I give it give it a
pointer to where I want that value
stored and I match it up tries the
entitled look hot and here I have a
slice of book a book is a struck with
ISBN title author prices struct is just
an aggregate composite data structure
and it holds all of your uh all your
other data whatever other pieces of data
you want to put in there stick them in
astruc composes together composite a
structure to aggregate aggregates
together as much data as you want
alright so that’s a book here’s a slice
a book which is books so I’m appending
two books each book and that pass all
those books in here this one I have my
templates in go so I pass in books which
is a slice and then I range over all
those books and for each book I print
out the ISBN titled author the price
very sweet to beautiful language it’s
very elegant really clean once you
understand it
man codes so nice so that’s books and
then the rest of it is a lot like that
like books show instead of getting all
the books and using query less are just
fun past we use query row and that just
gives us one row where we’re saying hey
where is B n is equal to you know
whatever the ISBN is just for and that’s
the Postgres right there that’s just how
you had done it pass in and identify a
parameter to a sequel standard and then
it’s the same way the other thing which
is neat is a let me see here here’s
where we create a book so we get form
submissions and you can look at form
value here so from my request I want the
form value you can go in and look at
that and that will give it from the body
parameters and forms been submitted or
from the URL that’s how form value works
so we’re getting it from the URL so just
give me the form value or a sign in it
we create a book write using the
composite literal composite literals the
type and then curly braces we’re putting
nothing in it so there’s nothing in
there yet
but then we accept ISBN equal to that
title that and then for the price that
came back as a string so we now have to
take that P and parse it as a float so
string convert parse is float we want
parse is a float 32 right and I give
this back a float
sorry parse it as that’s kind of weird
float 32 parses float 3 I’m going to
check that code and think that’s good
for a second here parsefloat converts
the string as to a floating-point number
with precision specified by bit size 32
for float 32 or 64 explode 64 we’re bit
size of 32 the result still has type
float 64 but it will be convertible to
float 32 without changing its value so
that was an area my Park can change just
that part that’s what
you float32 I think it said still
float64 there’s ol still has tight
float64 so that’s not an era apart
change that back so that’s still tight
float64 which seems weird it’s 32 now we
do DB exec so DB exec is where is it
it’s right here so exact execute is how
you execute another query right you want
to do some of you or excuse some other
sequel you don’t just want to do a query
a query row but you want to insert or
update use XQ and it gives you back a
result and air so result just tells you
like how many rows were affected or what
the results of your executed sequel
statement was and and so you do that
DB execute and then insert into bugs you
know those those those fields in the
database and then these values and it
inserts record and then updates very
similar so you just do here we go update
update form oak updates form this one
was what create processbook update form
here’s book update process and so update
process is another DB execute update
books set is be into this can it updates
it and then delete is just TVXQ delete
from books where this is acquired p.m.
it’s pretty elegant pretty beautiful you
are operating at the level of complete
understanding when you use this you have
an abstracted away your responsibility
as a engineer and a programmer to know
what you’re doing
and that doesn’t mean we’re all going to
build our own CPUs and and write
everything in zeros ones we still have
strapped away into a language but you
understand methods you understand status
codes right we’ve got methods here we’ve
got status codes
you know you’re looking at the forum
values coming back you know you’re
working in a static language you’re
doing your right conversions you’re
checking your types you’re doing all of
your restful routing it’s really the
best way to go you’re not relying on
somebody else to build some framework
where you don’t really understand the
fundamentals and the language you’re
working with but are instead up at some
abstracted level working in the way
somebody else prescribed restricting you
not giving you flexibility slowing down
your performance you don’t want that you
want to learn how to do it real do it
right so that’s just a little bit I just
have to get this out here man because
this stuff is like many years in process
to you know get a code pathway and and
this solution right here is like the
best T sweetest and you know the
clearest just this one little example
right here this culmination of the
Postgres section is actually a
culmination of so much of years of work
of many people so if you found this to
be beneficial and if it gave you insight
please like this video and tweet it and
share with others even though my voice
is all whacked and and I want to I want
to get that message out there to people
that you know look at how you should be
doing go programming web dev we gotta
get this video replaced so thanks for
watching and I hope this was helpful and
if you have any questions or you know
anything you can find me on Twitter and
I watch that several times a week and so
I love connect with people around the
world and I think it’s fantastic this
amazing world we live in now and so
don’t hesitate to reach out if you want
to access to my courses for free just
let me know be more than happy to help
if you don’t have the financial means
pay for it it’s my honor to return the
favor so many people helped me in life
with my education to my honor to give it
back all right you all have a good day
Please follow and like us:

Be First to Comment

Leave a Reply