Build a WIKIPEDIA APP with GOLANG (tutorial)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello everyone today we're going to be making a wikipedia app with golang i haven't seen any other youtube tutorials on how to do this tutorial in particular it's kind of just sitting on go's official website so i decided to make a video um so i'm going to show you guys a little run through on how the program works and then we'll get straight into it so the way that this program works is we can go into the url and type in something like slash view and then the title of an article or a wiki page in this case the title will be test when we go to the page we can check out the title and the contents of the article so we can see the title is test and then there's some random gibberish at the bottom we can go ahead and click on the edit link to go to the edit page which you can use to edit an article so i'm going to change some stuff here and hit save and then you can see it changed cool cool cool now if we try to view an article that doesn't exist like for example i don't know gerald it will instantly take us to the edit page so that we can create that article ourselves as you can see in the url so i'm just going to type in here gerald is a great guy all right and then i'm gonna hit save and then it will instantly take us back to the view page so yeah that's pretty much how the project works so let's get straight into it okay so first of all you're gonna need to get go installed we can just do that from the official golang.org website i've already got it installed so i'm not going to show you but the instructions on how to do it are pretty you know full and through on the official go website anyway and before we get into it and i just want to let you guys know this whole tutorial is completely based off of the official go web applications tutorial that they have on their site so if you want a um sort of a text version of this tutorial you can go ahead and go over to the site and check it out there so without further ado let's get into it okay so first things first let's open up a terminal and then i'm just going to make my go wiki directory and then i'll cd into go wiki okay so in here we'll just open up visual studio code it opened up on my other screen so i'll just bring that over here okay make that a little bit bigger cool cool cool all right so let's make our main dot go file okay so with go you're going to want to define a package so we can say package main okay so let's just make a really quick hello world app okay so we'll type out our main function here fmt dot print ln hello world okay and that should import fmt for us there so if you're using visual studio code you're definitely going to want to install the go extension it gives you a lot of good support the first time you use it it might pop up with a little notification saying that needs to install some extra stuff on top of it go ahead and do that because it'll give you a bunch of code completion like like what i just did automatic imports really handy stuff like that okay okay so now that we've got our hello world application done um let's talk about how we're gonna structure this app right so with a regular page we're going to have um sort of in an article right you're going to have a title and then a body of text which is going to be like you know what you'd actually read the actual article we can go ahead and describe that information using a regular you know a regular struct right so i'll get rid of this import here and let's just go ahead and type that out so let's do type all right page struct cool cool cool so like i said we'll have a title which is going to be a string now because we're going to be working with files um we're going to be using the i o i o util package which uh it accepts like a slice of bytes instead of a string so our body of text will be of type slice and then bytes will be in that slice right okay so we need a way to be able to save uh to save a page right with a title and a body as the input parameters so let's go ahead and make that method okay so we're going to make a method for this struct here so we're going to make a funk all right it's going to be a pointer receiver i think that's what that's called i'm not quite sure but this is going to return an error that's not how you spell error all right there we go so now in here we're first going to need a file name because we're going to be storing this with files right so we're going to have a file name which is going to be equal to p dot title right plus dot txt because we're going to be working with text files so we want the txt extension there all right and then we will return the i o util dot write file and then we'll have the title be our file name the p body is going to be what we write into the file and then we're going to have to give it a code which is going to be 0 6 0 0. now the zero six zero zero just means that this file will have both read and write permission so we'll be able to read and write to it throughout the program okay so now let's make a function that loads a file right that's able to go ahead and just load up a file for us or read the file for us and give us back the information so we can make a function like so so funk i'll call it load page it's going to take in a title because it needs to know what file it's loading okay it will return a pointer to the page struct okay first of all we need to create a file name which is basically just going to be the title with dot txt on the end so it's going to be equal to title plus dot txt okay and then we'll have body we're not going to do error handling right now we will do in a minute all right this will be io util dot read file file name okay cool cool cool so we're going to return a page pointer a page object whatever you want to call it with the title is equal to the title that we just defined and the body okay is going to be equal to the body that we got when we read the file okay um really quickly we'll just run through some error handling here so this also will return an error to us so we can put error there and then we can just say you know if error is not equal to nil then return nil and error okay now since we are returning an error we also need to come up here we need to put this in parentheses all right split it with a comma and then say type of error could also be returned all right and then down here at the end we need to put a comma and then put a nil here because this does not return an error at this point okey doke so from here um let's go ahead and just run this through and see if it works i'll just save this file you will need to import this package as well um this is gonna moan at me because i haven't put a comment above this so i'll just say page describes how we will present an article article there we go and that should fix that so it's going to moan at me because we don't have a main function okay cool so we need a funk main okay so let's go ahead and just make some random information and we'll store it in a variable called p1 okay so we're gonna have a page struct okay one of my i just thought i was doing the wrong thing but i am doing the right thing let's just call this test as a title and the body will be uh remember it has to be a slice of bytes so we'll just do this so and then we can do that and then we can put regular text in here this will automatically convert it into a slice of bytes so this is a test page cool okay so now we need to call the save method on this so p1 dot save okay and then we can just say p2 and we'll just do an underscore will be equal to our load page function whoops i messed that up a little bit i really got to learn how to touch type so we have to give this the title remember so we're going to give it test okay so that should load the file and then we can just print it out so fmt dot print ln and we can just print out p2 now remember p2 is going to receive you know both the title and the body so we need to specifically ask for the body okay uh and this is also gonna give us out a slice of bytes so we need to convert this into a string which we can do like so okay what i got rid of it i accident all right so uh we've got a problem okay let's take a look load page test load page load page this should be capital sorry my mistake you're gonna see a lot of that in this tutorial okay so i'm gonna bring my terminal back up and we can just do go run main.go boom this is a test page now we also made the file here as you can see with the contents of this is a test page which is pretty cool okay so now we can start messing with web stuff okay so i'm just going to comment this out for now probably delete it later i did not mean to do that comment that out for now we'll come back to that a bit okay so to mess with web to mess with the web well first of all come up here i'm sorry if i forget to explain the imports uh if you have the go extension installed it shouldn't be a problem but i'll try to remember to say it so we need the net slash hcp package which is part of the go standard library okay and then here we can say http dot handle func okay so now we have to give it a route that we want to handle so at a specific route what do we want to show people so this is just the regular home route or the just you know a regular forward slash all right and then in here we need to we need to basically give it a callback function okay so we need to give it a function here now um what the parameters of this function usually are going to be w which is of type http.responsewriter and r which is a pointer to an http.request okay this is not going to return anything so there's no type for that so we are going to have in here we're just going to say fmt dot print sorry not not that dot f print f okay this is going to take in a few arguments so first of all it takes w because it needs to actually be able to write a response remember w is of type http.responsewriter this is what you'd use to write a response back in a web application okay and then we'll actually give it some html this is this can be regular text if you want it to be but you know you can put html in here if you want which is what i'm going to do okay okay so um i'll just say hi my name is okay and then in go we can do a placeholder value by just typing in percent s right if we do a placeholder we can come to the end of the string but another comma here all right so let's say we want people to be able to say their name is whatever the url path is right so the way we can access the url path that they're at right now is r dot url dot path okay it's pretty simple right okay so the next thing we need to do is go ahead and let's um let's go ahead and actually host a web server locally of course so to do that we can do http dot listen and serve okay then we need to give it a port number so i'm going to do this on colon port 3000 and then you need to do a comma and pass in nil now the reason we pass in nil is because basically in in the future if you ever want to use any sort of routing or server sort of framework they typically use a different middleware to the go standard stuff that comes with the http package right for example if you've heard of gorilla mux you'd need to pass in another value in here that is not nil because that needs special middleware in order to be able to work but since we're using the go standard library everything works out of the box fine you can also create your own middleware if you want but for the purposes of this tutorial we're not going to be doing that okay so that should work so i'm going to save this see if we got any errors we're good okay so let me just open up my terminal again and i will just go ahead and say go run main.go okay it's as you can see it's kind of just staying there now it's not really moving so if i come here and i go to port 3000 hello my name is slash right if i go to slash i don't know bob hey my name is slash bob let's just get rid of this slash quickly because that's kind of triggering me so the way we could do that and go is you can just do some square brackets like this do a one and then put a colon there so now what we're saying is only include the characters after the first you know after the first index okay so boom save that we're gonna need to restart our server again just to fix that one thing okay come back here refresh and boom my name is bob obviously you know we can still do slash you know whatever and it would still come up but that doesn't really matter okay so after this part we need to move on to something else we need to actually be able to serve some wiki pages okay so we need to be able to create a page where we can view an article of some sort okay so to do that we're going to need something called the view handler and before we get into view handlers i just want to show you one more thing this whole function because it's just a callback function what we can do is we can create a new function here called handler okay this is also going to take in w and r right it's the same as what we have down here but if i take this i literally just copy and paste this whole thing and put it there we can actually just get rid of this whole function and move it somewhere else and then we can just pass in the name of this function this is exactly the same as the other one we had but it looks you know a little bit cleaner in my opinion and if you saved it and ran it it would still work exactly as the other one did i'm not going to do that because there's no real point so i want you to just have this idea when we're working on what we have because it's what i'm going to be doing for the rest of the programming tutorial okay i'm also just really quickly i'm gonna get rid of this all right cool so let's create a view handler so we're gonna create a function called view handler all right this is gonna take in w and r okay and then in here we need the title all right we need the title for the article so we're gonna have a title which is gonna be equal to the r dot url dot path okay now basically the way this works if you remember from the intro is in the url that will actually be like they'll have to type in view they'll have to type in view to be able to view an article so in order to get the title that comes after that part of the url we're just going to need to remove that part from the string that we're going to get when this returns the path of the url so the way we can do that in go is by doing what we did before with the other url the way we got rid of the slash now i don't want to count how many characters are in that string so we can just use the built-in length function and to go to count the length of the string for me okay and then that will just leave us with whatever comes after this ending slash which will be the title of the article that whoever you know was trying to view it wants to view okay okay i almost forgot the colon you need the colon there as well okay cool cool cool so then we can have a p and an underscore we will get back to the error handling shortly i promise we'll have a load page we'll call our load page function and we'll pass in the title this is just going to go ahead and look for the file try and read it try and send it back okay fmt dot f print f okay and then in here whoops in here we have to pass in w again remember and then we just pass in html so i'll pass in we'll pass in two placeholders this time because we want both the title and the body of text so in the h1 i will go ahead and pass in our uh title okay so this this placeholder will be for our title and then i'll just create a div div my typing skills are unparalleled jesus okay and then we'll just do another placeholder now that we've done two placeholders we can do two more commas to like put stuff in there so in here i can say p dot title and then p dot body okey doke so that should work fine uh i believe we didn't have another f print f anyway did we no okay cool uh so that's fine that's cool that's cool okay so that's fine let's save it make sure i didn't do anything wrong okay so in the main function we can go back to http dot handle func okay and then we want to handle the slash view slash route okay this is the route we want to handle with this function we're going to do the view handler we just need the name we don't need parentheses after it okay cool cool cool so we can save that and we can rerun our program okay and we can go back here now if i go to slash view slash test which is the name of the file that we created you know a while ago and hit enter you can see it's actually displaying the file in in the web which is pretty cool right at this point all we need to do is be able to edit and create new files so we're basically one piece of the puzzle down right so that's pretty cool so let's go back to our our visual studio good i'm having a stinker today okey-doke so now we need to work on being able to edit pages right so let's create an edit handler for that okie doke let's create a function let's create a function so funk edit handler handler okeydoke this is going to take in w and r as well okay it's not square brackets we want curly braces so the same thing as um the view handler i'll actually just copy it here we're just gonna change view to edit because we only want to get the title which is after this n slash right okay all right so now we're gonna have p and we're actually gonna do some error handling okay we're actually gonna do it now so i'll explain a little bit in a bit basically how this works okay so title title okay so if error is not null then p is going to be equal to without the colon because we're just reassigning p this time it's going to be equal to ampersand page all right all right i'm trying to look it up here so the title will be title that we have right basically the way this works is it will just send you to a form okay instead of trying to do anything fancy okay so at this point we could do like an fmt dot you know f print f and then we could put a form in there i don't feel like doing that let's make an html template all right because i'm not about to write a whole form like that right so let's go back up to our imports okay and let's do let's get the template slash html sorry that's the wrong way around it's html template template template template template okay cool html template package okay so from here let's go ahead and create an html template so let's call this edit dot html okay so in vs code i can just do exclamation mark tab to get this boilerplate i'm just going to say that i'm going to call this the edit page okay okay let's do this so um let's create an h1 and let's tell the person what they're editing okay so right now they're editing so edit ting and then we'll put the title of the uh struct in here so i'll do dot title okay so i'll tell them exactly what article they're editing or the title of it anyway then we're going to form okay the action is going to be slash save slash and then i believe it will be title or dot title okay yeah it looks right all right we're going to have another thing in here called method and that's going to be post okay in this form we need a text area so we need a text area the name is going to be body we don't need an id i don't really care about columns or rows right now in between the text area um we can't just put dot body because it's a slice of bytes right which is a bit a bit annoying but uh we can get past it so we can just say printf and then we'll just use a placeholder that we learned from before and then we'll just put dot body okay that's how you print out a slice of bytes as a string in a template cool so then we will need an input in boots input and this will be a subnet button basically so the value here will be submit okay or save whatever you want to call it all right cool actually we should probably call this save it's a bit more accurate okay so now we need to go ahead and go back to our edit handler and wire that bad boy up to the template so we can do that like so so what we could do is we can say it's literally just two lines so we can do t and underscore node no error handling for me there's going to be template dot pass files pass files like that okay and we just want to pass the edit.html file okay from here we're going to execute the template so execute this takes in w because it needs to actually write out the response and where we were using the sort of dot title and dot body that needs to actually come from a variable and the variable that we've stored that stuff in is p so if you know because uh in this case we're not assigning it a body right so that should come from the load page if the file doesn't exist it defaults to an empty string so we don't have to worry about that right now so that should be cool that should be cool beans we don't look like we have errors yet so everything looks good okay i'm gonna take a sip of water give me one set oh that's nice so we can just do the same thing for our view handler right it's always best to use templates instead of just sending this random stuff unless you're working with like plain json and you don't really need a template for that okay let's go to view let's create a new file called view.html again if i do exclamation mark and press tab we got this i'll call this page the view page okay in here we don't need anything fancy i'll just make an h1 and i'll put the title in here dot title okay um we also need basically we want to have a link for them to be able to go straight to editing in case they see like a mistake or something so i'll do p i'll do some square brackets just for a dramatic effect and then we'll just put an a tag in here and that's not what i meant at all okay let's make some space here a the href is going to be to slash edit slash i believe we need title dot title whatever this article is and then in here we can just say edit okay cool cool cool and then we just need a div here so div and then uh obviously this is a slice of bytes again so we need to use the method that i showed earlier where we did printf and then we do a placeholder like so percent s and then dot body okay so that's the view template done again we'll wire this bad boy up this is basically exactly the same as the other one except we have to change this to view.html okay cool so that is that now as you can see the same code is showing up in both functions and whenever this happens you know what programmers do they put it in a separate function so let's build that function out here so we have a function i'm going to call it render template okay like so this should have w because a template needs w to be inputted right the execute needs w which is gonna be uh of type http dot response writer we also need uh the template or the name of the template right so i'll just put template name that's really bad practice right so i'll just put ah it should be fine template name which is a string okay and then we will need p because we need to pass that into the template as well and it's a pointer to the page struct okay so that's what we need for that so pretty much let's copy and paste this or cut and paste it in this case okay so pretty much the only thing we need to do okay is get rid of the view here to make this accessible to both functions we can just put template name plus that and then that's pretty much it right uh that was not hard at all we can also remove it from here and then we can just say render template and we need to pass in whoops render template okay we need to pass in w i believe is that right yeah this is the view handler so we need to pass in string view okay and then we need p which is this bad boy here copy paste life of a programmer okay then just change this to edit okay cool cool okay so we're edging closer we're edging closer so now we need to work on being able to save a page right we need to create a new handler for that called the save handler right so we're going to create a new function called save handler handler like that okay this is going to just have w and r like so okay so again we do the same thing as we did before we need to extract the title from the save you can do that using the len function and we just copy and paste and change it to save because we only want a title okay then we need to get the body of text okay so this is a little bit different so we're gonna have the body it's gonna be equal to okay r dot form value okay embody oh i messed that up body so what's happening at this point is if we go back to our edit.html page you can see i said when this form is submitted we want to post it right so the form is just telling it right so it's telling my save url all right here's a form value that you need you're probably going to need this right because we're literally sending you this information body is the name oops i just closed another one down body is the name of the input here right so that's how it works that's how we're getting the information from the edit page to the save page okay then we're going to need to actually store this in a variable so we need p which is going to be equal to page where the title is just the title all right and the body is just the body that we have here okay actually sorry because because um because the actual input with the form is a string we need to turn it back into you know a slice of bytes so we will just do uh you know uh square brackets and then byte and then we'll wrap this in parentheses like so okay and then we can call the p dot save method like that and then we can do http dot redirect okay dot redirect and then redirect takes in w r all right we want to redirect them to be able to view the um the actual page right so we will we'll do slash view and then we want to add the title onto the end of that like so and then we'll do an http dot status found is it not showing up where are you status found http dot status found okay because we you know we found the status i guess i don't know something like that i keep bumping my desk i'm apologizing for that okay so now we need to handle some errors right so with the render template function there's a bunch of stuff that could go wrong right there's a bunch of stuff that could go wrong so let's handle sarah okay let's get this error out of the way so if there is an error if error is not equal to nil in other words right then we need to send an http.error right http.error we may as well just send it through right uh this will take in w which is which is what used to write the response error dot error okay okey dokey and then http dot internal server error okay so at this point we're just saying yo all right we can't render the template there's a problem with the templates so we have an internal server we can't serve you the templates anymore if that's the case we also may as well return from the function there's literally no point carrying on okay this dot execute also whoops what did i just do okay um where were we sorry um we were at the render template function okay so this t dot execute also returns an error so we may as well reassign that we'll reuse the same variable from before which means no colon here okay uh we will go ahead and say if the execution goes wrong okay then we will send an http dot error okay this also you know has to write a response so it takes in w error dot error okay and then this is the same thing http dot internal server error okay we've also got to fix up save handler because we ignored quite a bit here actually okay so with p dot save in case you didn't remember this returns an error okay so we're gonna have to account for that in case that does happen so with a colon we'll set error equal to p dot save and then we'll handle that so if error is not equal to no then send in http dot error w error dot error capital all right okay and then we need to send an http dot internal server error cool cool cool now this is all well and good we're actually edging nearer to the end of this unless you want to make it look pretty well then we're going to keep going for a while okay now at this point we have a little bit of a not a huge problem but there's a little problem in the app right every time we visit these edit and view pages right we are calling the same render template function and what the template function is doing is it's parsing the files every single time you access the page which is incredibly inefficient right a much better way to do this would be to cache the templates right so you're only parsing them once and then execute those templates only when you need them okay oh okay so the way we can do that is we need a global variable that can be accessed by uh those three functions right so in order to do that let's just create a var so global variable we'll use var for that so we'll just call this templates equals template template template.must so template.math is basically a wrapper function basically whatever we put inside here if it goes wrong the entire program will just shut down it will panic right because if we cannot parse the templates there's no point running the program the only sensible thing to do would be to just shut it down at that point so that's why we're using this wrapper function in here we can do template dot parse files okay we need to parse two files this time because we're going to be using this for two functions so we can just put in edit.html and we also need view.html okay now that we have this global variable we can strip away a lot of stuff from the render template function so we go back down here this entire thing can basically just go bye bye so uh this obviously has to change as well because we just completely changed everything so let me just oh i kind of just took a trip somewhere that i didn't know where i went sorry about that all right so in here we'll do templates dot x acute template like so this takes in w we will pass in the template name okay uh plus dot html and we also need p just like before and i believe this is still going to stay the same yeah okay cool let me just save make sure i haven't done anything wrong okay let's take a look what's wrong here templates dot execute template w template name plus dot html and p why is this not hmm oh sorry i forgot my colon here because we are not reassigning error anymore we're actually assigning it for the first time in this function okay so um now we have a serious security flaw in our app right the problem is we want people to enter into the url one of three words edit save or view those are the three keywords we're using but there's nothing stopping anyone from just using a different keyword and completely bypassing that so in order to save our program we're going to have to validify the url path when someone's using our app and if someone's using a different path we might even just want to completely shut down the app right because they could be trying to maliciously take it down obviously they could have probably just done a typo but you know the latter is probably a bit more concerning okay in order to do this we're going to use a regular expression which i'm no good at all right regular expressions are mouthy at the best of times we're going to need a regex package so reg xp is what it's called that should be in quotations i'm sorry about that okay we're just going to make another global variable for this as well because we're pretty much i think we're going to use it across all three handlers so var okay we're going to call this the valid path so one of the three valid paths right so we're going to do a function and this function is called reg xp dot geez i can't type dot must compile this is the same as template.must in the sense that if this does not work so like it says up here if this does not compile so if they type in anything other than view save or edit the whole program will just panic and die right the whole program will just close because they could be trying to you know do something really bad okay so i'm not going to type this out i can't be bothered to do that i don't think you guys can either i'm going to put this in the description as well because there's just no point regular expressions is not the point of this tutorial all right and i don't want to have to type that out okay so now we need to go ahead and create a method or another method sorry a function that will be able to get the title and verify that for us okay and then handle the case where they enter a wrong title in case they have a typo or something like that right so let's do that below the save function let's create a func called get title get title okay this will take in w and r now it will return two types okay it will return both a string string and an error okay so let's just pick a random variable name m is going to be equal to valid path with a capital p okay dot find string sub match okay and then we'll go ahead and pass in the r dot url dot dot path okay cool so this is basically going to go through the url path and then uh it's going to be either edit save or view if it's not one of these the program panics right okay if m equals equals nil okay then do an http dot uh not found it would be nil because you know the the the the muscum pile would not be successful so therefore the page is not found right okay then we'll do w and r into that because that is what that function needs and then we'll do a return we'll just return an empty string for the string because we can't really do much about that and then we'll make a new error so errors dot new okay in valid page title okay and then from here we can come out of this if statement and if all is good we can return m all right uh we're going to need to do a square brackets and put a 2 here okay the reason we put a 2 here is because with the url in order to get the title first the first uh sub expression will be edit or save or view whatever then after that it will be you know test or bob or whatever the title of the article is so that's why we need two here we'll also pass a nil here because there's no error in this case okay cool cool cool all right so now we can go ahead and use this in each of our functions i'll just do a quick save to make sure everything's okay oh sorry this should be called errors and you will also need to add that to your import list so it's literally just called the errors package so go back up there in case you don't have the go extension already installed okay so let's go through each of the our handlers and go ahead and implement that so let me just come up here so we don't need this anymore you can abstract this away and we can just say that our title is equal to with the colon equals so our title and our error because there could be a potential error sorry is this going to be equal to get title w and r okay we obviously you know we should handle that error whenever there's an error variable you know you're going to handle it so if the error is not nil then just return just just go away the the um the error message of http dot not found or whatever we put up there will already be sent you don't need to do anything from this function anymore okay that's all handled with the get title function we can pretty much copy and paste this throughout all the rest of the functions again so let's just boop pop that in there and boop pop that in there okay cool cool cool all right cool stuff right cool stuff cool stuff cool stuff all right so uh now i believe that should be it for now let me just let me just save this and test this right p dot save what's the problem here no variables on left side of go no why is this a problem my friend i don't understand what's going on here no variables no new variables on the left side of go no new variables sorry to go sorry we will be reusing this variable that we have up here so this will just be a colon in the safe handler i believe that goes for everything right yeah sorry about that um actually i think that's fine right because we're actually making new variables as well i think the save handler is the only one you need to worry about for that i believe we haven't handled the view handlers ever so we should be fine okey-doke i don't actually know how much how close we are at this point so we could be really close we could also not be really close so if i refresh this uh let's see have we got an error message that's rough that's rough that's rough okay let's carry on with the tutorial okay first of all um i just did it i just did a boo boo okay so let's just do a few more things first okay before we do this so we're going to introduce function literals and closures okay so the first thing we need to do i also realized that i should probably before we get into that we should probably uh fix up some stuff here as well we're just gonna need to add the other handlers in so this will be for example edit and this will be for example save down here okay all right that's cool all right that's cool that's cool that's cool let's introduce function literals enclosures okay so first things first all of these handlers need to have a title they're going to accept the title which will be a string okay i'll just copy that and paste it throughout here so all of our handlers will need a title now okay we're gonna make a function called make handler so func make handler okay this will take in a function okay which is going to be of type func uh which the types of what it's going to accept are http dot response writer it's also going to accept a pointer to http dot request okay that was a bit my typing skill is off the charts and then they're also gonna have a string because we just added the title okay okay so then here let me just wait i'm i'm actually confusing myself so this is going to return an hdp dot handler funk handler funk okay cool so in here we'll just type return okay funk and then we'll have w in here we will have which what am i doing this will take in w and r i'm losing my mind okay we're getting closer to the end so i'm getting excited all right cool cool cool so here we will extract the page title from the request okay so we will basically just copy and paste this code all right we are going to change it a little bit just a little bit okay so let me just take a quick look and see what's going on nothing is indented this is not okay there we go that looks more like it so let me just take a quick look at what we need to change so we're returning a function with w and r we have m equal to a valid path of fire strings that match if m is equal to nil we will return an http.not found instead of returning this errors we will just return from the function we will not return any errors okay and then down here we are just going to go ahead and type fn w r and m with a little two there okay cool cool cool then down in the main function let's move on down there okay now instead of uh sort of just having a view handler we will have make handler all right wrapping them to make handler wrapping the view handler right okeydoke did i just do i'm so dumb i completely fluffed my chance of doing that efficiently okay cool so we'll have make handler surrounding them right so the next thing we need to do is go ahead and remove the calls to get title okay so remove get title entirely it's not the best way of doing it so remove it remove it from each of the handlers okay and i believe we can actually just get rid of it all together i'm pretty sure we don't even need it at this point okay cool let me just save it make sure there's nothing wrong there is something wrong if error is not equal to no i'm i'm really sorry okay because we got rid of the get title this was one of the things that get tidal was supposed to handle or not supposed to handle i don't really know so we can get rid of it oh okay we don't need in any of the handlers okay we also need to make sure that this is a colon equals because we are not reassigning a variable anymore okey doke alright so at this point that should be it one last thing to do if we cannot start the server we may as well just panic and stop the program so a way we can do that is by using a method called log dot fatal okay and go ahead and wrap that around the uh listen and serve thing you'll probably need to add it up here yeah log add that import there okay cool cool cool we can get rid of these comments as well this is getting really scary because if this doesn't work i have to reshoot this entire tutorial okay so at this point we are going to do a go run main dot go we're getting a problem why am i getting a problem why am i getting a problem why are we getting a problem printf oh sorry i made a typo i just made a typo in one of my templates all right that should be a t not just a print that's just my bad because i can't spell okay so we can just do go run main.go okay okay it's working it's working let's go here let's view the test okay that's working why is this here we probably i just probably left some um fmt.printf stuff here we can get rid of this now we don't need that anymore okay this is incredibly scary wait a minute wait a minute one second so okay very last thing we need to do seriously if we are trying to view a page that doesn't exist i want them to go straight away to the edit page for that right so in order to do that is the error handling for view handler we're finally doing it right so there's going to be an error here and then we'll just do if error is not equal to no right like so then we're gonna do an http dot redirect w r okay and then we'll just do we wanted to redirect them to the edit page right so it'll be slash edit slash and then the title okay and then we'll just type in http.status found okay that should be a dot not a greater than symbol and then we will return from this function http.state is found we need a capital f okay okay so now we can actually run this for real now and test this out if i refresh the page boom if i click edit it will take me to the edit page and then we can go ahead and just type some random gibberish in here so some random uh how do you spell gibberish gibberish something like that okay save it this will redirect us to the view page which is crazy right so if i try and view something that doesn't exist like bob okay it will send me straight to the edit page where i'm editing bob and say bob is a cool guy right and i click save and boom it takes me here i must have made a typo here okay so that's pretty much it guys um from here you can do quite a lot actually some of the you know the biggest things i'd suggest you do is make this look better because it looks pretty bad right now but i mean this is pretty much it this is the functionality i was looking for you could try and add like a home page of some sort of all the different articles you could you could do that by just iterating over all the different text files that we have here but that's pretty much it guys um thank you guys for watching and i'll see you in the next one bye
Info
Channel: JackIsBack
Views: 783
Rating: undefined out of 5
Keywords: Go, go, programming, web dev, golang
Id: LOo3hA20ujU
Channel Id: undefined
Length: 58min 53sec (3533 seconds)
Published: Mon Oct 05 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.