Build A Web Server With Golang In 20 Mins (2021) - Beginner Friendly!

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
so in this video we learn how to build a very basic golang web server the prerequisite to this video is that you should have golang installed on your system and you should have a very basic idea of how golang works like packages trucks and interfaces and uh you can watch a video on how to install golang if you don't have it on your system and it doesn't matter if it's open to mac or windows the steps that i'll be covering in this video will be similar for all of them so i i'm on my windows laptop and i've opened up my powershell which is basically a terminal for windows and on mac you'll have terminal on open2 you'll have your bash or your terminal again so you once you have installed golang on your system you'll have a folder called go so that's where you want to be you want to be inside that folder called go and inside this folder you want to be inside a directory called as src inside this src directory uh we'll create a new directory called go server and we'll go inside this directory this is where we'll create our code so i'll open this in my code editor which is vs code so now that i'm in my code editor i'm going to go ahead and create a folder called static where i'll keep my html or static files and my main.go file where all my golan code will reside as you know when we start writing any golan code we have to write package main in the beginning and then we have to import some packages from package main so one uh of the packages that i need is fmt then i need the log package then i need net slash http package which has most of the functions that i need to create a web server and routes right and then as you know for every golang code we have to have funk main which is where most of the control of the operation lies right now i'll switch over to my draw.i o board and show you what exactly we're building so i thought before we start building any of the functions it's better to show you what we're building so we here's our server file and here are the three routes that we'll be building today one is the root route which is just a slash then we have slash hello and then we have slash form when we hit the slash route or the root route we'll just open up index.html file and then we have slash hello which will just call function hello function it will just print hello to the screen and then we have a slash form route which will have a form function associated with it and then that will open up a form.html file for us right so we have to create index.html and form the html files right now and then we have to write these uh routes inside our funk main and then we have to create these functions also inside our main.go file right so now that you have a 10 000 feet view of what we're doing it'll just uh everything will make a lot more sense so now let's uh switch back to our vs code back in our vs code you have seen that we had worked till this point till funk main now it's time to create two files index.html and form dot html in our static folder in index.html we'll write some very basic code we'll say html and then we'll say head and sorry we'll say title here we'll say static website and after the head we'll place a body tag inside the body tag we will have an h2 tag and we will again say static website and that's that's about it in our form we'll have slash doc type html then we have an html tag we'll have a head tag inside the head tag we'll say meta car set is equal to utf-8 right so that's what our meta tag does and then we have our body tag which will have a div it will have a form method is equal to post okay so we want to post the form and then we'll have slash form right and then we have our label uh i'm i'm assuming that you already know uh htmls are not explaining a lot of things right but anyways this is the name field that we want the user to input right so we'll have input name is equal to name and then you have type is equal to text and you can also skip this part if you don't really uh you know want to bother about html and then you have label then you have address then you have your input name is equal to address and type equal to text and value nothing right then you have your input type is equal to your submit button value submit so a submit button which will have submit written over it all right and this completes our form that we want the user to fill right and now we want to start wiring up our main logic which is inside the funk main so inside funk main we'll have a file server which is basically a function that we get inside the hdb package that we just imported so we'll say file server where s is capital so make sure you say that and here uh you have a short form assignment operator which basically declares and defines a variable right so with colon end is equal to and then you have http dot directory dot static dot slash static i meant so we're telling uh golang that we want you to check out the static directory and now golang automatically knows that it has to look at the index.html file because uh as you know php golang node.js all of them look at the index.html file right that's what they're trained to do as servers and now you have uh a handle function inside your http package right and so i want to start handling my root route which is just the slashed out so how do i handle it i say send that to the file server function that i just created which is you know just on top of this and then you have your http dot handle func which handle funk is another um uh what you call function that we get inside the hdb package and now i want to handle my slash form route and you have form handler right so we'll create the form handler function shortly and then you have another handle func where we'll handle our slash hello function right slash hello route that which we'll call the hello handler function so far so good i've already shown you what slash hello and slash form are supposed to slash form will show us the form which is formed.html just the slash will serve as the index.html file which i've shown you how it'll do that and slash hello will just print out hello uh to the screen and we'll have to create a function called hello handler for that and now let's also print out what will be printed out when our server connects so server.port 8080 and right so if you want to put newline you have to use printf which is inside the fmt package that we've just imported now let's handle our server so we uh use the listen and serve function inside the http package so as you can see hd packet is very important and this will create the server so this is really the heart of the software that you're creating right so they can be the error can be there or it could be nil so one of these two values will be assigned to err which is the error so now let's handle the error if it's not equal to nil then we want to do something so we want to log dot fatal error and this is where we used our log package to handle or to you know log the error so so far so good with the main.go now we want to start creating our main functions so first we'll create the hello handler and func handler this usually has two things so one is w and the other is r right so w is http dot response writer so as you know with any api or any route you'll have either a response and a request so request is something that the user sends to the server and response is what the server sends back to the user so we'll keep that value in w which is the response and for the request that the user will send which will be in http.request this star is a pointer so r is basically pointing to the request right so yeah i'm assuming you have basic idea of how pointers work even if you don't uh it's okay i think you can just follow this video right so as soon as we wrote funk uh hello handler you can see the squiggly line below hello hundred went away because now it's it knows what to do when it uh you know encounters this route now let's start writing out our function so url r is as you know the request so when the user sends a request which will be in the form of like let's say slash hello right so what you want to see is if user dot if the r or url dot path is not equal to hello now we're sending the uh the request which is slash hello to the hello handler but let's say by mistake something comes here so now we want to check if uh you know the path is not yellow handler then we'll do something we'll say sdp dot error w slash 404 not found and http dot status not found all right and we'll just return from the function and then if r dot method not is equal to get so we want only uh like when you type slash hello in your browser right that default by default is a get method right and we don't want people to post anything to slash hello we want people only to get and in the sense just print out hello right that's why we're saying that if it's uh not get then again you'll return an error http dot error which will be w again and method is not supported then you have http dot status not found now again as status not found is again uh inside http packet so you can see how important that is and then we'll again return from this function if this is the condition but the ideal condition would be that we want to just print to the screen which is f print f so fmt by the way has a lot of these uh you know functions like printf print ln fprintf right you can check them out and they're all for very different things and so we'll say hello and that's what our hello handle two will do now uh there's this quickly line below form handler so that means we want to write a form handler function so let's start doing that so we say funk form handler w http dot response writer and our asterix http dot request now uh you're going to be using this very often so just i mean it's the same syntax so you can keep it copy paste it somewhere and then just keep copy pasting it that's completely all right and again we'll have the same thing if error parse form so you want to parse a form right we want people to submit something in their form their html file and there will be a post request and they will then that will parse the form header not equal to nil basically if there is error then we want to say print fmt dot f print f w slash parse okay didn't we return so this is just basically printing out the error and here we'll say f print printf [Applause] w comma post request successful which is the ideal scenario which is where you want to be and we'll say name is equal to so whatever we'll fill in the form right you remember the form so whatever we'll fill in the form uh we want the values to be taken into these variables so r dot form value name so we want name from the form to come to uh name and then we want address to be equal to r dot form value address and we'll say fmt dot f printf w comma name is equal to percentage s new line comma name and then fmp dot f f printf is basically printing it on the screen just print f comma so whatever the the user has written so we're just going to basically um print it out out here now uh you must have seen that there was a squiggly line below and a name and then once we used name out here the squiggly line went away because golang tries to tell you that you're you've defined this variable but you're not using it so that's an error in golang and this doesn't happen with javascript and you can have like millions of variables that you never end up using and that's a problem right uh so that's a nice thing about golang that it catches all of that and then you'll say address so now the squiggly line below address is also gone away now this completes our function so let's uh head over to the powershell and check it out if it works so back inside our terminal or powershell we are still inside that folder go dash server where we have written all our code and now we are going to run a command called go build which will create a binary executable file it will be exe file because i'm in windows and now i can run go run main dot go so we have given our main file the name main.go but you could have named it anything else but main.go is the usual accepted standard right so that's why we named it main.go now we're going to run that code and it's going to say starting server at port 8080 because that's what we had written if you remember in our funk main uh it's it's given me it's showing me a pop-up which is asking me for my permission for my firewall so i'm going to allow access you might not see that in your in the screen recording software probably now we'll have to head over to our browser to see what's happening out there so let's go to our browser and here we'll open a new tab we'll say localhost 8080 now you remember we have three uh routes we're handling three different routes right so one is the root route which is this just the slash then we have slash hello and then we have slash form.html right so we have those three routes that we're handling so slash we're handling uh using file server and then we're handling hello using hello hello funk handler the function header handler function then we are handling slash form using the uh form handler function right so in the diagram if you see this right slash hello form and then with slash we want to open up the index.html file where we just written static server and that will be printed to the screen and then we have slash hello where we just want to print out hello to the screen and then we have a slash form where we'll open up a form from the html file and then we'll have we'll fill up the form and then we'll you know post to the server and print out something to the screen again all right so let's uh start off by checking out the slash route and see what happens it says static website right which is exactly what is written inside the index.html file and if we say slash hello uh the hello handler function will be called and you'll just print out hello uh to the screen and then if we have form dot html i'll just enter my details here and submit so remember it'll say posts request successful because that's what we are written in our code right inside the form highlight function and after uh you know you submitted those details it's just going to print out those details to the screen that's all it does so f printf name and f printf address if you remember writing that right so it's just printing out those values to the screen that's what our simple web server in golang does and i'm going to uh you know step this up a notch in the upcoming videos and i'll create more uh complicated videos where we'll have multiple files and we'll have routes and controllers and models and we'll have we'll work databases we'll work with sql will work with sqlite we'll work with golang oh sorry mongodb we'll work with cassandra we'll work with firebase right and uh we'll also work with graphql uh and we won't just be creating rest apis right and i'll also make a video where we have a react front end and then we'll also have authentication and then we'll uh we'll work with gorilla which is a much faster library to create all these apis and droughts and then we'll also work with go fiber which is the probably the fastest um you know api layer uh if you if you there's an article on the internet where somebody compares fiber with express js and fiber is i think 10 times faster so we'll we look at all of those things and i'm just getting started and also we look at microservices in golang as well so this is just the beginning of our work with golang and if you like this video do subscribe to this channel because uh you know i'll keep coming up with uh new uh content almost you know every day probably and thanks a lot for watching and do do share this with your friends if you found it helpful
Info
Channel: Akhil Sharma
Views: 1,495
Rating: undefined out of 5
Keywords: golang, golang tutorial, golang tutorial 2021, web server with golang, learn golang, golang basics, golang for beginners, golang web development, golang lessons, golang learn, go programming language, go prorgarmming, go lang, go tutorial, learn go, go 2021, golang web development 2020, golang web development 2021, latest golang lessons, golang web server, golang web app
Id: ASBUp7stqjo
Channel Id: undefined
Length: 21min 38sec (1298 seconds)
Published: Mon May 24 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.