Press "Enter" to skip to content

Web Apps with Go & Postgres

00:00:00this video is brought to you by Greater

00:00:01Commons greater learning greater

00:00:03opportunities greater life visit us at

00:00:07Greater Commons comm so I want to show

00:00:10you how to do a basic web app using the

00:00:12go programming language and I’m just

00:00:14going to introduce you to the resources

00:00:15and give you all the code that you need

00:00:17and get you up and running and you’ll be

00:00:20able to do create read update delete and

00:00:22you’ll be able to use Postgres to do it

00:00:25and and it will all be with the go

00:00:27programming language but I want you to

00:00:30like this video and I want you to

00:00:33retweet this video and I want you to

00:00:35share it on social networks because here

00:00:38on YouTube these are the videos in my

00:00:40channel and in this video here build web

00:00:45apps go language 135,000 people have

00:00:48watched it and that’s an OK video but I

00:00:51made that like a year ago and so I

00:00:53really need this video here to be what

00:00:56people are watching because this video

00:00:58and the material I have to share with

00:01:00you is way way better and I’ve been

00:01:03teaching go and web dev would go at the

00:01:06University and the college level for a

00:01:08couple years now and I’ve worked with

00:01:10Caleb doxy and Bill Kennedy right just

00:01:13to get the go code perfect and here’s

00:01:17the code base you know the resource

00:01:20here’s here’s the code pathway for

00:01:23learning go web dev and this is the best

00:01:26code pathway in the entire world

00:01:28and I know because this is what I do

00:01:30full time I study go I study go web dev

00:01:34and I teach it at the college of the

00:01:36University and I’ve worked with you know

00:01:38Caleb dachsie like created the

00:01:41foundation of this code base and we’ve

00:01:43built upon it so it’s an amazing code

00:01:45base it’s the best code base in the

00:01:47entire world screaming web dev let’s go

00:01:49so just discovering this like just tweet

00:01:52this video so people can discover this

00:01:53because I want people to get into the

00:01:57gogo language I love it and I want to I

00:02:00want to spread the gospel of go and and

00:02:03so people have a good way to learn it

00:02:05and get into it they’ll be more likely

00:02:08to use it I’m not paid by Google either

00:02:11by the way

00:02:12so down here is Postgres we’re going to

00:02:15like do this post grass and look at this

00:02:17code that’s when we’re going to look at

00:02:19but in post grass and you can go install

00:02:21post grass and Postgres website alright

00:02:26I’ve lost my voice I just got to talk

00:02:29quiet I guess but down here you hit

00:02:32download and if you’re on a Mac you just

00:02:35want to get this one right here that’s

00:02:36light and that’s easy and if you’re on a

00:02:38Windows you could you can watch my

00:02:41training where I show you how to do all

00:02:43this my training you can find it for

00:02:46migrator comments comm look there first

00:02:48we’re building a site all and go and

00:02:51it’s a place where you’ll be able to

00:02:53teach and and and you know deliver

00:02:55educational content very economically to

00:02:58help people learn for the greater good

00:03:00of humanity greater Commons so if it’s

00:03:03not if it hasn’t launched yet the course

00:03:05will be up there when this launches

00:03:06right now you can also find the course

00:03:08on Todd MacLeod view to me so just go

00:03:11google that and you’ll get to the course

00:03:13go web dev and that shows you how to do

00:03:16it on Windows Postgres and everything

00:03:18all right so let’s see it so you get

00:03:22Postgres installed and then you want to

00:03:24create a database a book store so you

00:03:27can see there i have a database

00:03:28bookstore that’s right there that’s my

00:03:30database if we go go into in the

00:03:33bookstore and then take a look at you

00:03:36know it’s got one table books so we can

00:03:38select everything from books right and

00:03:41there what did I mess up select star

00:03:44from books select let’s just try it


00:03:49sometimes my terminal does that see same

00:03:51command and in there out so there are my

00:03:54three records I got three records in

00:03:56there let’s look at the code and yeah

00:04:00it’s working on us on a map Mac I just

00:04:02wander if I to pull it but here here we

00:04:04have the code and this is the exact same

00:04:06code which is right here right exact

00:04:10same code it’s probably I can note but

00:04:14I’m in that so you have access to this

00:04:15code did I already show you the URL

00:04:17there’s the URL

00:04:20so you go into this code and then here

00:04:23all the steps to build it up and this is

00:04:26like you know an hour to of training

00:04:29right there so I’m not going to go

00:04:30through all of it but this is the

00:04:32finished deal let’s just watch it run

00:04:34and then I’ll show you the code walk you

00:04:36through the code so I’m going to switch

00:04:38into that over here on this side I’m

00:04:40already there yeah I’m in 44:22 so I’m

00:04:46going to run that and we’ll come over

00:04:50and take a look at it and so yeah there

00:04:54are those three records we saw and I

00:04:56could insert a book but I don’t want to

00:04:59have to think of an ISBN number and so I

00:05:05don’t know what’s the title of a book

00:05:08West with night a Beryl Markham if you

00:05:15have not read this book I will not be

00:05:19steering you wrong go get this book this

00:05:23book is like one of the best books I’ve

00:05:25ever read in my life it’s so great I

00:05:28created that book showed the record go

00:05:31back to all books it’s in there I can

00:05:33update it if I want

00:05:35that’s like I’m a blues singer or some

00:05:39so change numbers out of the West with

00:05:42the night Beryl Markham let’s say we’re

00:05:44making it on sale so now it’s 1499 go

00:05:49back to all books and Emma Jane Austen

00:05:52who wanted to read that delete that we

00:05:55got crud created read update delete and

00:06:02it’s all restful right we go look at a

00:06:05book books show nice URLs drop the drop

00:06:10to you know some value in there URL

00:06:12parameters so look at that code right

00:06:15here you know I could go into this in

00:06:21quite a bit of depth so I was the first

00:06:24place to start if you don’t know any go

00:06:26web dev it’s a really it’s a kind of an

00:06:30entire process so I

00:06:32think I’m just going to keep this video

00:06:34short because this would be about 40

00:06:37minutes for me to walk you through all

00:06:38this but I think that this is the best

00:06:45example this code for learning how to do

00:06:50go web dev I kind of want to go into it

00:06:54I kind of feel you say man go into it

00:06:57let’s go into it so if you don’t if if

00:07:05this doesn’t make sense to you I’ve got

00:07:07an introduction to the go programming

00:07:08language and I’m just giving me the

00:07:10resources and I’m not trying to sell you

00:07:12something I mean obviously this is also

00:07:14how help support my family got two kids

00:07:17and but if you don’t have the resources

00:07:22the money the course is usually like ten

00:07:23or fifteen dollars if you don’t have the

00:07:26money to pay for that just send me an

00:07:27email on Twitter message me on Twitter

00:07:31and here’s my Twitter deal and at Todd

00:07:37McCloud I guess the shade URL that’s

00:07:38better send me a message on Twitter and

00:07:41just interesting god I totally want to

00:07:44take course but I’m college to it in our

00:07:46live in India or you know I don’t have

00:07:49that money whatever I don’t care what

00:07:50your reason is you don’t have the money

00:07:52that’s fine I’ll give you free access I

00:07:55want you to learn it for the good of all

00:07:56for the greater good the greater cons

00:07:58that’s why we’re building that site so

00:08:02just send me a message and I’ll give you

00:08:03free access but I have an introduction

00:08:05of a go programming language to teach

00:08:07you everything all right so really go

00:08:11look at the net HTTP package so if this

00:08:14doesn’t make sense

00:08:15take that introduction course and here

00:08:17in the net HT package we have type

00:08:19Handler and type handler is an interface

00:08:22okay so an interface allows polymorphism

00:08:26and go so I click on that and I see type

00:08:29handler and anything that has this

00:08:31method implicitly implements the handler

00:08:34interface and that means that if I have

00:08:36some type and it has a method and that

00:08:39method has this signature and that a

00:08:42value of that type also is of a handler


00:08:45and and so that’s how the interfaces

00:08:50work and again go watch the introduction

00:08:52training it doesn’t make sense because

00:08:53I’m going to try to keep this a little

00:08:54brief you got a little bit of like a

00:08:58hangnail I’m not totally weird all right

00:09:02so that’s the handler interface and

00:09:04serve HTTP response writer an employer

00:09:08to request my joke which I tell on class

00:09:11is what is a interface say says hey baby

00:09:14if you’ve got these methods then you’re

00:09:17my type so the handler interface and the

00:09:22reason the handler interface is the

00:09:24entry point for understanding the net

00:09:27HTTP package is because if you look up

00:09:29here handlers are used all over you see

00:09:32them in the index of all the different

00:09:34functions and things it’s like it’s used

00:09:37all over handler and their handler and

00:09:40particularly here right if we look for

00:09:43listening served there’s listen and

00:09:45serve and it takes an address in the

00:09:48handler and so that handler right here

00:09:52that we pass in right that’s going to be

00:09:54our server or serve MUX at the false or

00:09:56MUX or MUX whatever you want to call it

00:09:58and so if we pass in nil as I was

00:10:02passing in so let’s just go down here

00:10:04and look at it and we’ll search for nil

00:10:15so there’s no and if you pass a nil you

00:10:19use the default serve MUX okay so the

00:10:22default serve MUX well with the default

00:10:25serve MUX we can use handle and handle

00:10:36func handle and handle fog so handle and

00:10:42handle func registers the handler

00:10:43function for given pattern in the

00:10:45default serve MUX the defaults for MUX

00:10:48is your server so you can say server you

00:10:50can say router you could say serve mug

00:10:52so you say mugs it’s all pretty much the

00:10:54same the documentation for serve MUX

00:10:59explains how patterns are matched let me

00:11:01just show you so the starting point is

00:11:05HP listen and serve port 8080 mill right

00:11:09meaning to fall serve MUX and with the

00:11:11fall serve MUX we can use handle func

00:11:13HTTP handle func handle func wants a

00:11:16pattern and the string and then a

00:11:19function with that signature that is a

00:11:21type in go you can look at the language

00:11:24speculative speculation you can look at

00:11:27the language spec here’s the language

00:11:29spec and in the language spec you can

00:11:33see all the different types including

00:11:37function types first-class citizens

00:11:40right so we’ve got a type here go back

00:11:44to the documentation which is a function

00:11:46with that signature I’m in the pantry by

00:11:50the way because my kids are asleep I

00:11:52don’t want to make too much noise so

00:11:54it’s like my little sound booth so

00:12:00handle func takes the pattern string

00:12:03which is the route and then it takes a

00:12:05func with this signature so there’s my

00:12:07first func with that signature func

00:12:09index response writer pointer to request

00:12:12response write a pointer to request said

00:12:15now I’m wrapping up my whole deal so

00:12:18this route run this code this route run

00:12:22this code this route run this code

00:12:24that’s it

00:12:25these route from this code it’s totally


00:12:29so HB redirect here just to keep it easy

00:12:32keep it restful keep the route to

00:12:34meaningful HP redirect can redirect

00:12:37takes the response writer and by the way

00:12:40this is how servers work client-server

00:12:42architecture right it’s like a request

00:12:45comes in you write a response the

00:12:47request comes into the server to the

00:12:49server writes a response so that’s why

00:12:52this wants that signature because it’s

00:12:54like okay requests came in we got a

00:12:56right response requests came in got a

00:12:58write a response right that’s why you

00:13:01need to function with that signature so

00:13:03redirect takes the response to requests

00:13:06where you want to send and then the

00:13:09status code and that’s just a constant

00:13:11says you know hey it’s 303 the other

00:13:15which means if you come into default you

00:13:18end up getting taken to route books and

00:13:20that’s where you go and that serves this

00:13:23one looks index so if the request method

00:13:26is not yet you’re out of here each to be

00:13:29err can respond and the response writer

00:13:32and then the status text and and status

00:13:35method not allowed and then we return so

00:13:38if you try to post something to this

00:13:40route now we don’t take that method here

00:13:42when we get and then we do a database

00:13:44query and this DB here that comes from

00:13:49go doc I don’t know how much longer I

00:13:53keep going go dog database sequel all

00:13:58right and so here we have the DV stock

00:14:01and you look up at the top package

00:14:06sequel provides a generic interface

00:14:07around sequel databases must be using

00:14:11injunction with a database driver just

00:14:14your drivers car drivers let you drive

00:14:16something let’s go driving and here’s

00:14:21for Poe’s grass so I like this one here

00:14:23we go and you just get that driver get

00:14:30back here so on TV and

00:14:37we can do a query and a queries right

00:14:40here and here’s the query string where

00:14:41you put in your sequel

00:14:43you got any arguments it will take an

00:14:45unlimited 0 to unlimited number of

00:14:47arguments gives you back a pointer to

00:14:49rows so you just get the address back

00:14:52it’s pointer to an address for all the

00:14:54values are stored get a pointer to rows

00:14:57and then there with a pointer to rows

00:15:00you have all these methods available to

00:15:03you so you can do next and scan so you

00:15:07can do a for loop because next returns a

00:15:09bool so you get for loop over the next

00:15:11next next and you can scan and get the

00:15:15values and put it into the destination

00:15:17can we put it into the destination you

00:15:20need that pointer like I said if you

00:15:23don’t understand it could let go coarser

00:15:25so how is that use TB right it goes up

00:15:30here up here I have bar DB and from

00:15:36package sequel right DV I think I might

00:15:40set package TV earlier so that was just

00:15:42me being tired DB query you know from

00:15:47that DB query so pointer to DB x full

00:15:50query because let me just explain ER

00:15:52make sure it’s clear

00:15:54alright so package sequel package sequel

00:15:57package sequel I do open I get a pointer

00:16:02to a DB see that open pointer to DB and

00:16:06open takes a driver name and then like

00:16:09how do you connect to that database and

00:16:11that’s all specified and whatever driver

00:16:13you’re using so up here I create VAR DB

00:16:17from package sequel type DB a pointer to

00:16:21it right and then here I do from package

00:16:25sequel open from package sequel open and

00:16:29then I pass in the driver name Postgres

00:16:33and I pass in the datasource stuff so

00:16:37here it is it’s how you connect Postgres

00:16:39close grass username password then all

00:16:43that stuff move this over come to reason


00:16:49and so now DB when you do open it gives

00:16:53you back a pointer to a DP with a

00:16:55pointer to DB a value of type DB to be

00:16:59specific with my language when you have

00:17:01a pointer to type DB you have all these

00:17:04methods available I can now run query

00:17:07this one right here so I have a pointer

00:17:10to DB here I ping it just to make sure

00:17:13we really connect to the database and

00:17:16what the pointer to DB I can do that

00:17:18query pass in my sequel and when I do

00:17:23that gives me back roses in there check

00:17:26my air right here there are my rows and

00:17:30then I do my four rows next so that

00:17:32returns a bool long as there’s another

00:17:34record it’ll be true true true false no

00:17:37more records were done and then I do

00:17:40rows scan and I give it give it a

00:17:43pointer to where I want that value

00:17:45stored and I match it up tries the

00:17:49entitled look hot and here I have a

00:17:53slice of book a book is a struck with

00:17:57ISBN title author prices struct is just

00:18:00an aggregate composite data structure

00:18:02and it holds all of your uh all your

00:18:06other data whatever other pieces of data

00:18:08you want to put in there stick them in

00:18:11astruc composes together composite a

00:18:15structure to aggregate aggregates

00:18:17together as much data as you want

00:18:21alright so that’s a book here’s a slice

00:18:25a book which is books so I’m appending

00:18:28two books each book and that pass all

00:18:31those books in here this one I have my

00:18:37templates in go so I pass in books which

00:18:42is a slice and then I range over all

00:18:46those books and for each book I print

00:18:50out the ISBN titled author the price

00:18:55very sweet to beautiful language it’s

00:18:58very elegant really clean once you

00:19:01understand it

00:19:02man codes so nice so that’s books and

00:19:08then the rest of it is a lot like that

00:19:10like books show instead of getting all

00:19:13the books and using query less are just

00:19:17fun past we use query row and that just

00:19:21gives us one row where we’re saying hey

00:19:23where is B n is equal to you know

00:19:27whatever the ISBN is just for and that’s

00:19:29the Postgres right there that’s just how

00:19:32you had done it pass in and identify a

00:19:34parameter to a sequel standard and then

00:19:41it’s the same way the other thing which

00:19:42is neat is a let me see here here’s

00:19:47where we create a book so we get form

00:19:50submissions and you can look at form

00:19:53value here so from my request I want the

00:19:55form value you can go in and look at

00:19:58that and that will give it from the body

00:20:01parameters and forms been submitted or

00:20:04from the URL that’s how form value works

00:20:08so we’re getting it from the URL so just

00:20:11give me the form value or a sign in it

00:20:13we create a book write using the

00:20:15composite literal composite literals the

00:20:18type and then curly braces we’re putting

00:20:21nothing in it so there’s nothing in

00:20:23there yet

00:20:23but then we accept ISBN equal to that

00:20:25title that and then for the price that

00:20:28came back as a string so we now have to

00:20:32take that P and parse it as a float so

00:20:36string convert parse is float we want

00:20:39parse is a float 32 right and I give

00:20:42this back a float

00:20:44sorry parse it as that’s kind of weird

00:20:46float 32 parses float 3 I’m going to

00:20:49check that code and think that’s good

00:20:51for a second here parsefloat converts

00:20:55the string as to a floating-point number

00:20:57with precision specified by bit size 32

00:21:00for float 32 or 64 explode 64 we’re bit

00:21:03size of 32 the result still has type

00:21:06float 64 but it will be convertible to

00:21:08float 32 without changing its value so

00:21:11that was an area my Park can change just

00:21:13that part that’s what

00:21:15you float32 I think it said still

00:21:25float64 there’s ol still has tight

00:21:30float64 so that’s not an era apart

00:21:32change that back so that’s still tight

00:21:35float64 which seems weird it’s 32 now we

00:21:39do DB exec so DB exec is where is it

00:21:46it’s right here so exact execute is how

00:21:51you execute another query right you want

00:21:54to do some of you or excuse some other

00:21:55sequel you don’t just want to do a query

00:21:57a query row but you want to insert or

00:22:00update use XQ and it gives you back a

00:22:02result and air so result just tells you

00:22:05like how many rows were affected or what

00:22:07the results of your executed sequel

00:22:09statement was and and so you do that

00:22:14DB execute and then insert into bugs you

00:22:17know those those those fields in the

00:22:20database and then these values and it

00:22:22inserts record and then updates very

00:22:25similar so you just do here we go update

00:22:29update form oak updates form this one

00:22:33was what create processbook update form

00:22:35here’s book update process and so update

00:22:39process is another DB execute update

00:22:42books set is be into this can it updates

00:22:46it and then delete is just TVXQ delete

00:22:50from books where this is acquired p.m.

00:22:52it’s pretty elegant pretty beautiful you

00:22:55are operating at the level of complete

00:23:00understanding when you use this you have

00:23:02an abstracted away your responsibility

00:23:05as a engineer and a programmer to know

00:23:08what you’re doing

00:23:09and that doesn’t mean we’re all going to

00:23:12build our own CPUs and and write

00:23:15everything in zeros ones we still have

00:23:17strapped away into a language but you

00:23:20understand methods you understand status

00:23:24codes right we’ve got methods here we’ve

00:23:27got status codes

00:23:29you know you’re looking at the forum

00:23:30values coming back you know you’re

00:23:33working in a static language you’re

00:23:35doing your right conversions you’re

00:23:36checking your types you’re doing all of

00:23:39your restful routing it’s really the

00:23:43best way to go you’re not relying on

00:23:44somebody else to build some framework

00:23:47where you don’t really understand the

00:23:50fundamentals and the language you’re

00:23:52working with but are instead up at some

00:23:55abstracted level working in the way

00:23:57somebody else prescribed restricting you

00:24:00not giving you flexibility slowing down

00:24:02your performance you don’t want that you

00:24:04want to learn how to do it real do it

00:24:07right so that’s just a little bit I just

00:24:11have to get this out here man because

00:24:13this stuff is like many years in process

00:24:17to you know get a code pathway and and

00:24:22this solution right here is like the

00:24:25best T sweetest and you know the

00:24:29clearest just this one little example

00:24:32right here this culmination of the

00:24:34Postgres section is actually a

00:24:37culmination of so much of years of work

00:24:40of many people so if you found this to

00:24:43be beneficial and if it gave you insight

00:24:45please like this video and tweet it and

00:24:48share with others even though my voice

00:24:49is all whacked and and I want to I want

00:24:53to get that message out there to people

00:24:55that you know look at how you should be

00:24:57doing go programming web dev we gotta

00:24:59get this video replaced so thanks for

00:25:03watching and I hope this was helpful and

00:25:06if you have any questions or you know

00:25:08anything you can find me on Twitter and

00:25:11I watch that several times a week and so

00:25:13I love connect with people around the

00:25:15world and I think it’s fantastic this

00:25:18amazing world we live in now and so

00:25:20don’t hesitate to reach out if you want

00:25:23to access to my courses for free just

00:25:25let me know be more than happy to help

00:25:26if you don’t have the financial means

00:25:28pay for it it’s my honor to return the

00:25:31favor so many people helped me in life

00:25:33with my education to my honor to give it

00:25:36back all right you all have a good day