Build a Rest API with GoLang

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello everybody and welcome back to the channel in this video what we're gonna do is utilize the go programming language in order to build a rest api now this is not going to be a beginner friendly tutorial mainly because i am going to expect that you know the basics of the go programming language so as long as you know the basics of that you should be able to take this tutorial and learn exactly how to build a rest api with go now it would also be nice if you are familiar with what exactly a rest api is however if you're not that's a-okay because i'm going to do a quick explanation right now so in the web world there's really two different components that exist we have the client and we have the server so anytime that you go on a website you go on a specific url what ends up happening is the client is going to make a request to the server to get some data the server is then going to process that request and then return that data to the client once that's done the client is going to render that data and there you go you can see all of your data so for example let's say i went ahead and went to a website where i'm storing all of my to-do's so let's say i want to view all of my to-do's so i go to the website so what ends up happening is the client is going to make some sort of get request with a path of the server url slash to do's to this server the server is going to look at this look at the end point and start executing logic to get all of our to-do's and now once that's done what ends up happening is it returns back an array of our to-do's and it returns back our data in json format so this right here is json format and this is how clients and server interact with one another and communicate with one another so another example let's say we want to add and create a to do well what we can do is we can hit this exact same url but we can change this to a post request this right here is the verb now what we want to do is we want to add a to do so we the client needs to provide the server with information about the to do itself so right over here it provides this json blob where we have a key that is item and then the property that is read book as you can see the communication is going both ways through json and then the server can do something it can add that to do into the database and then it can actually send back that json blob of the to do that it added so that is pretty much all it is that you need to know in terms of rest framework one thing that is super super important to understand is these verbs right over here as well as how the client and the server ultimately communicate with one another and that is through json all right so now that we got that started let's actually go ahead and start working on our rest api in this section what we're going to do is go ahead and set up our go application in order to build our rest api so what i went ahead and did is i created this to do hyphen go directory and inside of here i have this empty main dot go file actually it's not empty the only thing that it has is this package main on line one so you go ahead and do the same now again i'm opening this on vs code you can use whatever text editor you choose however vs code is by far the most popular and that is what i am going to be choosing so in order to create a rest api we're going to be utilizing a library called gin like so so what we need to do is we need to somehow get this dependency inside of our main dot go application now in order to do this what we need to do is set up dependency tracking inside of our go app so let's go ahead and first do that so what we're going to need to do is open up our integrated terminal like so and then right in here we need to execute a very simple command go mod and then over here we're going to say init and then over here we're going to say example and then we're going to do example slash whatever directory we have right over here so i'm going to say to do hyphen go now this is only going to work if you have go installed on your local machine again i do expect you to have it installed being that you're taking this tutorial so once this is executed you should have this go.mod file and now what we can do is we can download gin onto our local go application in order to in order to do this we're going to do go get and then what we need to do is we need to specify where we want to get this package from this is going to be a github package so we're going to say github dot and then over here we're gonna say gin chin gonic and then slash gin like so so go ahead double check that you got everything right let's go ahead and download that and now we should have if you go over here you can see here we have a bunch of different things going on we also have this additional go dot sum file so that's pretty much all it is that we need to do to get started so now what we can actually start doing is creating our api the first thing that we need to do in order to create this api is to first define how our to-do data structure is going to look and of course in golang we're going to do that with a struct so over here we're going to say type to do then we're going to say struct and then over here we're going to start defining the properties as well as the type of the values so the first thing that we need to do is to have an id in order to uniquely identify each to do item so we're going to say id and then over here we're going to say that this is going to be of type string all right we're also going to have another property called item and this also will be of type string and then we're also going to have a completed status and this is going to be of type boolean okay so now that we have created that what i'm also going to do is i'm going to create a to-do's array so let's go over here we're going to say to do's and that's going to be equal to the type of to do array and over here we are going to have a few different arrays let's have an id of one over here we're gonna say item of clean room and then over here we're also gonna have a completed status of false like so so now let's just go ahead and copy this three times or maybe just twice let's just change the id to two change the id to three and then over here what we're going to do is we are going to also let's say here um read book and then over here let's say record video because that's what i do often now one thing that i want to note is that this structure right over here is a completely different data structure than json so why is that important well let's remember that the client and the server communicate with one another through json whether it is the client sending json to the server or the server sending json back to the client so what we need to do is when the server decides to send data back to the client we need to somehow get this data structure and convert it to json now when the client is sending data to the server in the format of json what we need to do is somehow get this json and conver convert it into a data structure that go will understand now this is actually a relatively easy thing to do all we have to do is go to the struct and then right over here we can use these string literals and then we can say json colon and then over here we can give this the property name inside of these quotes and that's going to be id like so and that's really all it is that we need to do so we're going to do json colon and we're gonna do title and then over here we're gonna say json colon and then we're gonna do completed like so so that is really all it is that we need to do now that we got that out of the way let's go ahead and create our server and surprisingly this is going to be very very simple so the first thing that we need to do is we need to import a few packages so the first package that we are going to import is one that is native to uh golang which is going to be net http and then we're also going to import github dot com slash gin gonic slash gin so we can get gin working inside of our app so now what we're gonna do is we're going to create the main function that is going to run by default and then right over here in order to create our server what we can do is we can assign a new variable called router and that is going to be equal to gen dot default and that right there is going to create our server so right here this router is our server now in order to run our server all it is that we need to do is do a router dot run and then over here we're going to specify the path in this case it's going to be local host and let's just make it 90 90 just to be a little bit unique so now our application once we run it of course should be running on port 90 90. before we run our server let's go ahead and create our very first endpoint now in order to create a endpoint using this library all we have to do is go right over here and then say router and then over here we're going to say the http verb this is going to be a get request we're going to say router.get the first thing that we need to supply as a parameter is going to be the path that we're going to append to this url over here i'm going to say slash to do's like so because we want to get multiple to do's for our very first endpoint the second parameter is going to be a function that we're going to define inside of our main.go file and that function is going to return that json data and eventually this is going to be returned to the client so let's go over here and let's define another function and i'm going to call this function get to do's so i'm going to say get to dues and then over here it's going to take in one parameter and that we're going to call this parameter c or we can even call it context which i think would make a little bit more sense so we're going to call this context and this is going to be of type gin dot context like so this context is going to contain a bunch of information about the incoming http request so inside of the http request we could have a bunch of information we can have data inside of the request body inside of the request header etc if we ever want to extract that data all it is that we need to do is get that from the context okay so that's all it is that we really need to do now what we can actually do with the context is to actually transform that data that is in this data structure into json and this is why we need this right over here because this is going to be serving as a template so what we can do here is we really just want to return this array of to-do's but we need to convert it into json so all it is that we have to do is context and then over here we're going to say dot intended json because this is going to convert this right over here this data structure into dejohn so over here the first thing that we're going to supply it with is the status of our incoming request so we're going to say over here http and this is going to be coming from a library that we are going to import i thought we already imported it i guess not net slash http so we're going to go ahead and import that so we're going to say http and then over here we're going to say dot status we're going to say status ok for this and then the second parameter is going to be the json or is going to be this data structure right over here so we're going to say to do's and now of course what's going to happen is it's going to convert this data structure into json utilizing this and this should be okay like so so that's really all it is that we need to do and that's pretty much it so right over here all we have to say is to find that function that we want to call that's going to be get to do's let's go ahead and save that and now let's go ahead and run this application and try to hit this endpoint in order to run our go application what we're going to do is we're going to open up our terminal our integrated vs code terminal and we're going to execute go run main dot go and what this should do is run our go application on this port so now what we can do is we can actually try to hit this endpoint so localhost colon990 slash to do's in order to get this data right over here in order to do that we can either do it inside of the browser or use a really cool tool called postman so i actually highly recommend that you go ahead and install postman it's a relatively easy download i'll have the link in the description below and then right over here create a new folder i created a new folder called golang and then inside of here i added a new request and then over here i put the url as well as the path and then i specify that this is going to be a get request if i were to send off this request as you can see we are getting back our data our array of objects inside of json format so we can see that this is working a okay let us now create another endpoint that is going to allow us to add a to do so over here i'm going to say function add to do and over here we're going to similarly utilize the context and now what it is that we need to do is create a new to do so let's go ahead and create a new variable that's going to be called new to do and we're going to give it the type of to do and the first thing that we need to do is we need to get the data from the client so remember in a post request is going to hit a specific endpoint and the data for that specific to do is going to be in the json format and it's actually going to be inside of the request body so how are we going to first extract that data from the request body and then convert it to this data structure that is familiar to golang well this is actually not that difficult all it is that we really need to do is utilize the context and instead of utilizing intended json we're going to utilize something called bind json so over here what i'm going to do is i am going to create a very new variable and i'm going to call this error and over here we're going to say that that is going to be equal to i'm going to say context dot bind json so bind json like so and then right in here we're gonna pass in this new to do so over here we're gonna say and new to do new to do like so and then what we're gonna do is we're gonna do a colon and then over here we're going to say if the error is equal to nil then what we want to do is we want to return so what is happening right over here well what's happening here is we're doing the context and then we're going to be passing in the bind json so contacts.bind.json and essentially what that means is it is going to take whatever json inside of our request body and it's gonna bind it to this new to do that has this to do type now this potentially might return an error if our json doesn't have this format so it doesn't have an id that's a string a title that's a string as well as a completed that is a boolean so if that is the case what's going to happen is it's going to throw an error it's going to return an error and over here we're actually catching that error inside of this error variable right over here then what we're doing is we're checking okay if the error is not nil because if everything is a-okay well it's just going to go ahead and bind that new to do and not throw an error so if it's not nil and we have an error then this is actually going to go ahead and actually throw an error for us and then we don't want to continue on and execute our code so what we're going to do is just very simply return so that is what's happening here and now well we have this new to do that we got from our json body okay so now all it is that we need to do is we need to grab our to-do's array and then just very simply append all of the to do's and then we're gonna append this new to-do onto it like so so new to do and then the last thing that we want to do is let's actually go ahead and return the new to do so over here we're going to say context and we're going to say this time dot intended json we're going to say that the http status is created so status dot created like so and then what we want to do is we want to convert this into json and return it so that's pretty much all it is that we need to do now all we have to say is router dot post we're going to also make this slash to do's we could also make it slash to do but let's just keep it consistent so slash to do's and this one's going to be what what is this one going to be called this one's going to be add to do like so so now let's go ahead and test this out so what i'm going to do is i am going to exit out of this run this again let's see if it's running okay so let's go back to postman and what i'm going to do is i'm going to create a brand new request this one's going to be a post request and this one's going to be to slash to do so what i'm going to do is i'm not going to add anything in the request body i'm going to send this off and you can see here right away we're getting a 400 bad request error now let's go to the body let's go to raw like so and then over here we're going to supply it with some json so we're going to say json id this one's going to be 1 and then over here we're going to say the item that we want to do we're going to say make bed and then lastly we're going to say completed we're going to say that that is false okay so we're going to go ahead and send that off we get 201 created and now if we send this request off over here you can see that um oh yeah apologies this should not be title this should be item so let's go over here what did i do here so let's go here do we have anything called oh like right here this should be item apologies for that you can see exactly why this is important because this is the mapping so let's go over here let's try that again we're going to say item now and now if we were to make this request again you can see we're still getting title which is very strange oh yeah that's because we need to restart our server so let's go ahead and restart that ctrl c and then go run main dot go so now if we make this request initially we don't have our data now we're going to make this post request there we go we make this request and there we go we have our new data all right that is cool and terrific let us now create a new endpoint that is going to allow us to get a specific to do by the id so what i eventually want to do is if i want to get a specific to do all i want to do is i want to hit this get end point and i'm going to do slash to do and then slash the to do id like so and what this should do is just give us this object right over here rather than this whole array so in order to do this let's actually create a function that is going to iterate over this array and find the correct to do with that specific id so over here let's create this new function so over here i'm going to say get to do by id and over here this is going to take in the id which is going to be a string now this is going to return the to do as well as an error so let's define that type over here so it's going to return a to do and an error now it's really either going to return one or the other so if it actually returns a to do then the error is going to be nil however if it returns an error then the to do is going to be nil okay so all it is that we need to do here is very simply iterate through this array and then find a specific spinous specific to do so over here we're going to say 4 i then over here we're going to say t for to do is equal to range to do's and then over here we're going to say if t dot id is equal to the id that we pass in right over here then what we are going to do is very simply return to do of i and then the error is going to be nil we're not going to have an error so to use a file like so okay so that's the that's if we find it however if we don't find it then we want to throw or return that we got no to do but we also want to return an error so over here what we're going to do is we're going to get this package called errors errors like so and then over here we're going to say well we want to return an error so we're going to say errors dot new and then here we're just going to simply say to do not found like so so that is going to be the function now let's actually create the function that the handler is going to utilize so over here we're going to say funk we're going to say to do or maybe call this get to do get to do like so now let's remember over here this one was get to do's this one's going to be get to do one specific to do this too is going to take in the context so over here we're going to get this context we're going to pass that context in like so and now the first thing that we need to do is somehow extract that dynamic id from this url now this is actually known as a path parameter so somehow we need to extract that and get it from uh um that url so how are we going to do that well remember all the information that we need is inside of the context so all it is that we really need to do is just say that the id is going to be colon equal and we're going to say context dot param dots param and then over here we're gonna specify exactly what we want well we want the id now you might be thinking where is this string coming from well when we create this new endpoint what we're gonna do is we're gonna say slash to do's and then slash to define that this is going to be dynamic we're going to say slash colon id like so and this is going to call the get to do function this right over here is going to be what the variable is going to be stored as and as you can see here we're going to say we want to get the id okay i hope that makes sense so now what we can very simply do is call this function right over here this get to dues by id function so over here what we're going to do and let's actually move this to the bottom because it makes more sense that way to have all of our route handlers here and then over here what we're going to do is we are going to say uh so we're going to say we're going to get the to do or the error and that's going to be equal to colon equals and we're going to say get to do by id we're going to pass in the id like so now over here remember we might either get a to do or an error one of them is going to be nil so let's just check if the error is not nil then we want to do is we want to throw an error so we're going to say if it is not nil then what we want to do is we want to do context dot intended json and then right in here we're going to say http we're going to say status.not found so this is a not found error and then we're also going to let's actually supply it with the custom message we can do that with gin dot h and then right in here we can supply it with a custom message saying something like um you can say message and then over here we can say to do not found like so okay so that's really all it is that we need the last thing that we can do if we actually um if we uh don't hit this if block and over here let's actually return so if we don't hit this if block then what we want to do is we actually want to return that very specific to do so over here we can just say that we want to return or maybe we can just say c dot intended json or not c contacts dot intended json now we're going to say that this is going to be equal to we're going to say status so let's say http status we're going to say that this is going to be status okay and then we're also going to return that to do like so okay so that's pretty much all it is that we need to do let's change this to capital like that and there we go so now let's go over here and let's try to make this request okay the first thing we need to do is restart our server let's go ahead and do that let's restart it let's make this request again and there we go you can see that we are getting a specific item so now the last thing that it is that we need to do is to very simply have the ability to update the completed status of a to do so let's do that in the next section let us now create a brand new endpoint that is going to allow us to update the status of a specific to do now this is going to be a patch request because we are going to update something that is already existing and in order to specifically identify what to do we want to update we're going to use these same parameters over here so the same path parameters that we have right over here so slash to do slash colon id except this is going to be a patch request so let's just go ahead and copy this and over here we're going to change this to patch like so and then over here let's define a brand new function so right here we're going to say toggle toggle to do status something like that and then over here this is going to take in the context as usual like so okay and of course we're not using uh python here this is gonna be a function like so okay so now that we got that the first thing that we need to do is we need to extract the id from the path parameter we've actually already done that the next thing that we need to do is we need to find the specific id is the specific to do by that id we actually already done that right over here with this function so we can actually reuse this function now if we don't have the to-do if the to-do doesn't really exist then we want to throw an error which we actually did right over here so what we can actually very simply do is copy this code and paste it in here so now over here we're getting the id we're getting the specific to do by id and if the to do doesn't exist we're throwing an error so if we don't hit this if block then what we want to do is very simply uh convert the or toggle the completed status so what we're going to do is we're going to say to do dot completed is equal to equal to exclamation mark to do dot completed and so what this is going to do is it's going to flip the boolean value so if it was true it's going to flip it to false if it was false it's going to flip it to true so it's going to allow us to well toggle the last thing that we need to do is send back that new to do that we updated so of course we're gonna do that with the context dot intended json and then over here we're gonna say http [Music] status okay and then over here we're going to supply it with that new to do and that's pretty much all it is that we need to do now right over here what we're going to say is we're going to utilize the toggle to do status function so let's go ahead into postman so the first thing what i'm going to do is i always kind of forget to do this but we need to run our server restart and run our server so i'm going to get this right now initially it's false so what i'm going to do is i'm just going to very simply change this to patch i'm going to send this off and now you can see that this is true if i go ahead and try to get it now you can see that it is still true if i try to get all of them you can see that this is the only one that is true and now what i'm going to do is i'm going to try to go over here to slash 3 patch so i can change it back to a false and you can see that the toggle is working fine so now if i get the whole list you can see that they're all false so i hope you guys learned quite a lot this was a really nice tutorial a really good introduction to building rest apis with go and i'll see you guys in the next one
Info
Channel: Laith Academy
Views: 113,334
Rating: undefined out of 5
Keywords:
Id: d_L64KT3SFM
Channel Id: undefined
Length: 34min 14sec (2054 seconds)
Published: Thu Feb 24 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.