Build a RESTful HTTP API in Golang w/ Mux

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey guys how's it going in this video we will be building a HTTP API in golang and we will be using the gorilla web toolkit MUX package for the routing so let's just do a quick demo of this API so here I have postman and this is an application that we can use to test HTTP api's and currently I have all of these requests set up already for each of the routes that we'll be creating in this API so the first route here is the route which has the HTTP method of post and the URL is slash posts this route is used to create new posts and posts are the entity that we are using in our API so if we actually go back to the code and here is the post struct and as you can see we have three fields we have the title the body and author the title and the body are of type string and the author is a custom type called user and user is defined up here and it contains the full name the user name and email okay so let's go back to postman and here we can see that these are the fields that we have included in the request body okay and the next request here is the get slash post request so this request is used to get all of the posts in our collection because in our API we have a slice down here called posts and this is the variable where we are going to store all of our posts and the get slash post request is used to get all of the posts in that slice okay and the next request here is the get slash posts slash ID so this looks very similar to the previous one however here in the URL we have slash zero so this can actually be anything this can be any number so for example this could be stash posts / 100 or / 35 or stash - this is because this is a dynamic root parameter meaning that it's like a variable and what this means is that our code can see what this data is and so we can use this data in our root handlers so for example in this case this root parameter is used for the post ID so for example when we make this request we would be getting the post which has the ID of 2 and if we change this to get / posts / 50 then we would be getting the post which has the ID of 50 okay let's move on to the next request this request is the purple quest and the put request is used for updating we will see how this works in just a minute let's just look at the next one which is patch and the patch request is also used for updating but in a slightly different way and the next one the last request here is the delete request which is used for deleting posts great ok let's now go and first of all let's see the get / post request let's send this okay and here we have a empty array in the API response that's good because we haven't created any posts yet so let's go to the post request and let's send this request this is the JSON body that we are sending in the request let's send this okay and here we can see that we have this post in the response that's great let's now go to the get request and let's send this great so here we have an array with this post inside the array great let's now go to the get slash posts / ID route and let's set the ID to 0 and this is because the ID of the posts is actually in this case in this API is the index position of the post in the slice and since the post stats we want to get is the first item in the array that means that we want to set the ID to zero here okay let's send this okay and now we have an object which contains all of the post fields in the response great let's move on to the put request so the proquest is used for updating posts so here we have the HTTP method set to put and the URL set to slash posts slash zero okay good let's now go to the body so this is the request body and here I have some data that I have quoted before and it's simply setting the title to updated by put request and it's setting the body to ABC and it's changing the author fields to full name Jane Doe user named Jane Doe and email Jane Doe at tess calm okay so let's send this request okay and we can see that we have an object returned and this is the updated post data and it looks good all of these fields have been changed to what we set in the request body up here cool all right let's now move on to the Pat requests and actually before we move on to the patch quest let's first talk about why we need two different methods for updating posts because here we have the proquest and the patch requests and they are both used for updating but there is one big difference between them so let's go to the put request and let's say we wanted to only update a few fields so not update all of these fields but only update let's say the title so let's update the title and let's set this to this has been updated again okay let's now send this okay and let's go down to the response so the title has been updated and it's been set - this has been updated again that's correct however everything else has been set to its default value all of these values here are empty strings the reason that this is happening is because the way that we're going to be coding the put request handler is we're going to be replacing the whole post object with what we have sent in the request body so this object here is completely replacing the post object in the slice and since only the title has been defined all of the other fields have been set to their default values so that's why we need another route which can handle partial updates for when we don't need to update the whole post and that's why we need the patch request let's go to the patch request and let's go to the body okay so here we have object which contains only the body filled okay so let's send this request okay and here we have the response cool all right so the body has been updated - this has been updated by a patch request and the title is still the same from the put request and these have remained the same from when we did this put requests here so to show this a bit clearer let's actually go back to the put request and let's let's set all of the fields here to have a value so let's go here to the proquest and let's go to the body of this request and I'm just going to undo what I did before so that's we have this object here with all of these fields and their values and let's send this great okay so let's now go back to the patch request and let's only update let's go back here let's see what can we update let's update the body again so it's currently ABC and we can confirm this if we go to the get request here and let's send this request and here in the response we can see that the fields contain the updated data great let's now go back to the patch request and let's set the body to testing one two three okay and currently it's set to ABC so let's send this and let's see what happens okay great so here we have all of the data that we had before so for example the title has stayed the same and the author data has stayed the same but for the body has been updated to testing one two three great and let's confirm this by going to the get request and let's send this request and now we can see that this has been updated to testing one two three great and now let's go and test the deletes route okay and let's set this to delete slash posts stash zero that's good okay so let's go and send this now let's send this request and here we don't get any response body because we don't need a body because there is no data to send back to us but since the status is set to 200 okay we know that this request has worked let's now confirm this by going to the get request and let's send this great so now we get a status of 404 not found and here we can see that the request message sorry the response message is no post found with specified ID great so now that post no longer exists cool alright so that was a demo of our API so let's get started building it alright then let's get started the first thing I want to do is find a folder where I want to create the portrait and this is the folder where I want to create the project so let's create a new folder and it's cool to this golang basic HTTP API okay and then I'm going to open this in a visual studio code which is the code editor that I'm going to be using okay okay and first of all make sure that your editor has golang support so in Visual Studio code the way that you do that is by going to the extensions and then searching for go and then installing this extension here okay once you've done that then you have to then go to the command palette which is here and then you have to run the command go install / update tools and then click on that and click this checkbox here to select all of the tools and then click OK I've already installed all these tools so I'm not gonna click OK but once you've clicked ok vs code will then install all of these tools for you okay all right let me just click out that let's close this and go back to the Explorer okay and let's now create our file for this tutorial we will be writing all of our code in one file this is because it's a very simple API and we don't need more than one file so I'm going to create a new file and I'm gonna call to this HTTP - API don't go okay and the first thing we have to do is we have to set the package as main and we F do creates the main function great okay the next thing we have to do is we have to import some things so so let's do import first of all let's import encoding slash JSON this API will accept JSON in the body of the requests and it will respond with JSON in the body of the response so that is why we have to import encoding slash JSON okay and let's now import net slash HTTP and also we have to import a third party library called gorilla MUX so let's actually go to the gap page for that this is the HTTP Rueter that we are going to be using in our project so let's go down here and we can see that we have to use the go get - you get up calm slash gorilla slash marks we have to use this command to install the dependency all right so let's just copy that and let's go back to our code our code editor and let's go to the terminal and I'm just gonna paste this in okay and press ENTER and now now install the gorilla mocks library okay and now we can import it up here so github.com slash gorilla slash marks great okay and now let's go back to the main function the main function is a special function because this function is the function that's called when we execute our go application so in here we have to set up the router so let's do Rooter is equal to MUX dot new Rooter okay let's now save this and we get an area oh and that's because these should be brackets normal brackets not curly brackets sorry about that let's save that now okay and it removed my imports let's just control Zed that V should stay the reason that those were deleted is because each time I save visual studio code execute go thumped which is a command which formats all of the code and it removes the imports that are unused and we haven't used these yet but we will do soon so I'm trying to control that that and let's go here so here we have an error and it's saying that new Rueter returns a new route instance and it's been declared this variable has been declared but it's not used okay that's fine we are going to use it soon we actually use it now to set up the roots so let's do ruta dot handle funk and this takes in a path and let's set the path to slash test and let's set the function to test okay and now we actually have to create this function so let's go down here and let's create the test function okay and this function is a route handler and it therefore has taken two parameters the first of which is the response writer which we will call W and this is of type HTTP dot response writer okay and the second parameter is the request the HTTP request variable and this we can call our and this is off type pointer so we use the asterisk here to say that this is a pointer and it's a pointer to a HTTP dot request value okay great alright then okay so now we can do very simply we can do W dot right and for now let's just pass in a string and let's just call this this is a test okay so this will respond with a string that says this is a test and we can't actually do this because this method is expecting a byte array excuse me a slice a a byte slice so we have to do the conversion so to do that we can do square brackets bytes and then we can enclose this string in brackets like that so that should hopefully now work let's save this now okay and that has actually removed my encoding slash JSON import but don't worry we can add that back later because for now we just want to test out this route here okay so oh one thing we have to do before that is we have to start the web server so let's go here we first have to start this web server this web server has to be listening to requests and to do that we can do HTTP don't listen and serve and let's pass in the address we can make this listen on localhost port 5000 okay so this just means localhost port 5000 okay and the second argument here is the Rueter this is the handler right and we have to pass in the marks Reuter okay great so that should now work this listen and serve method actually returns an error and we can see how this works if we go to the github page for gorilla mocks and let's search for listen answer and we can see here how it's being used in the examples we can see that logged or fatal is used to surround the listen and serve method so that's if there is an error then it's logged to the console okay so I just wanted to let you know that that is what you can do you can create an error variable and you can assign it to the return value of this method great okay so let's save this now and let's go to the terminal and let's run this file let's type in go run HTTP - API don't go okay press ENTER and now we get this Windows security alert because this application is accessing the network so I'm just going to allow access there we go and now it's running okay so our application is now listening for HTTP requests to the address which is localhost port 5000 so now let's try and make some requests to our API I am going to be using postman to make these requests okay so let's just open up postman okay and I'm going to make a new request to localhost 5,000 slash test okay because that's the route that we set up here slash test okay let's send this request and there we go we get this is a test returned back to us that's awesome so we actually have our first functioning route it's not very useful but it does work that's awesome okay so now let's go back to the code and let's make this a little bit better instead of sending a string we could instead send a JSON object okay so let's delete this and first of all let's go back up to this import statement here and let's import encoding slash trace on and let's go down here and let's now do json dot new encoder which takes in a iot writer so this takes in a variable which satisfies the interface IO dot writer and the response writer that we have here does satisfy the i/o dot writer interface so we can pass in W and we can then do dot encode and then in here we can pass in a value that can be converted to JSON and we can do this by using a struct so in go a struct is a collection of named fields so we could create a new type for example we could go up here and you could do type test and then struct and then we could do something like data which is of type string and that would be a struct and then we could pass in an instance of that strapped in here however for this simple example that we have here we can use a anonymous struct which means we don't have to predefined the struct to create a instance of a struct I hope that makes sense but it's basically an anonymous strapped right so we don't have to predefine the type okay so the way that we would do this is by typing in struct and then curly brackets and then here we have to define the fields in the struct just like we would when we create a struct using the type then the straps so this is a struct which is called test and we can set the fields here and we could call this data which is our top string so the same way that we create this shot this way is the same way that we have to create the struct here in the anonymous struct so here we have two sets for example let's call this field ID and let's say that this is a string and there we go that's what we have to do for that and then now that we've defined what this struct contains we now have to set the value so go to the end of this curly bracket block here here and then do another pair of curly brackets like that and here we can set the value of these fields and in this case we only have one field which is the ID so we could do ID and then you can set this to 5 5 5 or we could just put 5 5 5 and that would work as well because this is the shorthand way of doing it this is the shorthand way of setting values to an instance of a struct ok and yes so that is how you create an anonymous struct we first use these struct keyword and then we have a block here which contains the struct definition and we then have another block here which contains the values of the fields ok so um let's go back up here and let's remove this it would probably have been easier to just use a normal struct type like we had here that would have been easier probably but it's pretty nice to know how to create an anonymous struct ok let's save this now and let's go to postman and let's send this and we get the same result ok that's because we forgot to restart the server so let's press ctrl C to cancel that come on and then this do go run HTTP API don't go okay and we get this Windows security alert pop-up again okay let's just allow access okay and let's go back to postman and let's send great so now we get a JSON object back which contains the ID which has the value 555 awesome however postman thinks that this response is in text format not JSON the reason that this is happening is because we didn't set the content type header on the response so the way that we can do this is we can go up here and do W header dot sets and then pass in the key and the value the key will be content - type and the value will be application slash JSON let's save that now and let's go to the terminal and let's restart the server so ctrl C to cancel that and then do go run HTTP API it go okay and we now get this shot again okay so this is showing up each time we start the server this can get quite annoying so there is actually a way to prevent this from happening the reason that this is happening each time we restart the server is because we are using me go run command and each time we use the go run command go will generate a new executable file which has a different name each time and therefore windows will keep on asking us each time because it's a different file name and it doesn't remember what we said for the previous build of the application and we can see that if we look at this here so here we can see that this is build four six four zero six zero six eight two and let's cancel this and let's ctrl C and let's rerun that command and we can now see that this is completely different so each time we use the go run command a new executable is created each time with a different name and therefore Windows Firewall thinks is a completely different application it doesn't remember what we chose here because usually Windows Firewall will remember what we chose the next time we open the application so let's just click cancel here and let's ctrl C to cancel that and let's use a different method of building and executing our application so instead of of using go run let's use go build the go build command we'll build our application into an executable file okay so press ENTER and then we can now see that we have this exe file sharp here this is the compiled version of alko program so now we want to run this program so let's go to the tunnel and let's now do dot slash golang - basic - HTTP - api and press enter and that will now run the program and this will show up again that's fine let's click on allow access and there we go that's fine let's now do ctrl C and let's run that again let's execute that executable file again and now this time we don't get that pop-up because Windows Firewall remembers that we trust this application because it has the same name all right so let's do ctrl C and let's try and recompile this program and then run the program let's do go build again actually let's just change something quickly let's just change something quickly let's go here and let's just comment this out and let's save this and then let's go here and let's do go build again okay and let's now try and execute the program and it works without showing the Windows Firewall pop-up great even when we change our application and we compile it and then execute it the Windows Firewall pop-up doesn't show awesome ok so from now on that is what we'll be doing we will first be using the go build command to compile our code and generate the executable file and we will then be executing that executable file to start our API great ok let's get back to what we were doing let's go here and let's uncomment this ok let's save it and then let's go to the terminal and it's ctrl C and instead of doing two commands let's do one line which contains those two commands so go build and then we can use the ampersand two times to then type in another command that will be executed straight after go build has finished so here we can then do golang - basic - HTTP api dot exe okay press Enter and there we go so that is now running the API ok let's go to postman and let's click send and now we get the same response but now it's formatted as JSON because postman now knows that this response is in JSON format because of the headers let's click on the headers tab here and we can now see that the content type has been set to application slash JSON great ok let's go back to the code and let's make this a little bit more interesting so let's create a slice and let's create a route that can add items to that slice so let's go up here and let's create a slice ok so let's call this data VAR data is off type string slice string slice okay and let's set this equal to a new string slice okay and let's create a new string slice okay this is a composite literal this is one of the ways we can create a slice in go by using the composite literal syntax alright then okay so let's now go down here and let's create a new route handler let's call this add item and this is a route handler so it will have the same parameters as the test function here so we'll get the response writer so W HTTP response writer HTTP response writer ah pointer to HTTP request okay this asterisk here means it's a pointer so that's what it means the type that this pointer points to is HTTP request all right let's now go up here and let's set up a new route so let's do Reuter dot handle func and this will take in the path which we can set as slash add and then we also want to send data so one way we can send data in a HTTP request is by putting the data inside the request path so for example we could have a dynamic variable inside this path and using the Marx library we are able to do this using curly brackets so here we can type in the dynamic variable name inside these curly brackets and then later inside the route handler function we can then access that data using the variable name so for example let's set this to data so now we have created a route which has a dynamic variable with the identifier data okay and let's just set the function here to add item okay great let's now go here back down to the add item method and inside here the first thing we want to do is access that dynamic variable so the way that we do this is by creating a new variable which we can call data and we can set this to max VARs and this is a function that returns the route variables for the current request if any okay so let's call this function and pass in the HTTP request pointer which we named our okay this parameter here great okay so this vast function will return a map which has keys which are strings and the values are strings as well and the keys are the variable identifies so in this case data is the identifier so we can so we can get access to the data variable by accessing the item in this map that has the key of data so for example we can now use square brackets to access the data value by typing in data okay great so now we have access to the data variable great okay so now we can add that data to the slice this slice up here and we can do that using the append function which is an inbuilt function so let's say data is equal to append and then we have to pass in the slice which we call data and actually I just realized that we shouldn't call this data let's instead call this routes data or routes variable and let's now pass in the route verbal here okay okay so sorry about that that was a bit confusing I should have called this data let's actually change this variable name here to something let's rename this to something different to make this less confusing let's change this to item okay so now the route variable is called item so now we can go back down here to the add item function and we can change this to item okay okay and now we can do JSON new encoder and then pass in W and then do dot encode and you can encode the data so then this will return back the slice to the caller and this is useful so that we can then see the state of the slice so each time we append data to the slice we can then see the items inside the slice okay let's save this now and let's go to the terminal and let's control-c to cancel the execution of that and then let's do go build and then and and don't slash golang - basic - HTTP - api don't you XE okay and that's now running let's now go to postman and let's now create a new tab and let's set this to localhost 5,000 slash slash ad and then we can send it and see what happens hopefully we should get error because we haven't actually defined a route handler for this we only defined a route handler for slash ad slash this variable here so this should return an error let's try and send it great okay so here we get a 404 not found cool let's now do slash ad slash and then let's type in some data so let's just type in ABC and this can be anything right this can be anything but for now let's just set this to ABC and let's click send great so now we get a slice which contains ABC awesome let's go here and let's add a new item let's add one two three and it's click send great so now we have two items in the slice we have ABC and one two three great one issue we do have is that this is being displayed as text and not as JSON so let's fix this now let's go back to the API code and let's go to the add item function and let's do W dot header dots X content - height and let's now set this do application slash JSON JSON okay and let's save this now and let's go to the terminal and let's ctrl C to cancel that and let's now do that same command go build and and dot slash go line - basic - HTTP - API don't you see and actually we don't need this story you can see ID in here okay because you may be on Mac or on Linux and in that case this command will work like that okay press Enter okay and let's now go back to postman and let's really send this request okay and now we get an array with one two three and it's formatted with JSON styling here because postman knows that this is a JSON value because of the content type awesome okay great okay let's go back to the body and the reason we don't have the previous entries was because we restarted the go application so we are storing these items in this slice and each time we restart the application we lose all of the state so that's why we don't have those values but let's add a few more let's add a testing there we go and now we see one two three testing and let's add this is a test and this is a string that has spaces so that's quite interesting let's click send and now we have this is a test showing up here as well awesome great let's add another one slash at /dev stacker let's send that and now we have dev stacker here and let's add five five five awesome okay let's add let's add go down and let's add one and let's add 1337 awesome okay cool so we've created that this route that can add an item to a slice in our application awesome great okay let's not talk about HTTP methods so with HTTP requests we can use one of many different methods and if we go to postman and we go to this drop down here we can see all of these methods so these are the HTTP methods that we can use for our requests and so far we have just been using the get method but since we haven't specified the method for our routes in our code let's go back to the code and let's go up here to where we have set up the routes so here we haven't actually specified the methods for these routes so that means that machs will accept any method for the routes that can access the slash test path or the slash add stash item path so what that means is we can go to postman and you can change this to put and can send this and we now get the new item being added or we can set this to delete and we can send this and we have this new item here so this request works with any method but we can change this so that it only works with certain methods so let's go back to the code and let's be more specific about the methods let's go to the slash add slash item root here and let's do dot methods and here we can see that it takes in strings and these three dots here means that this is a very addict parameter a very addict parameter which basically means that we can enter in any number of strings so for example we think that this do you get and then do another string and let's type in delete and we can add as many as we want so we could add here put and post etc however for now let's just put get and delete ok so let's remove these two so that's we only have get and delete ok let's save this now and let's go to the terminal and let's do ctrl C to close that one and then let's do go build and and dot slash go line - basic - HTTP - API press ENTER ok let's go back to postman and let's now let's now go up here and let's change this to test and let's press ENTER and now we only have this one test here and that's because we restarted the server so now we only have one item great and we are currently using the delete method let's change this to the gets method and let's click send and now we have two tests great so that one worked let's try this with post and this shouldn't work because we are only allowing the get and the delete methods to be used on this route so let's see what happens when we press send great okay so we get this 4:05 method not allowed status which is an error status that is telling us that this method is not allowed for this route great let's now try this with patch and let's click send and we also get the four or five method not allowed let's try this with options let's click send and that doesn't work either great so this only works with get and delete lets get delete up here and let's send this and now we have four four test strings here awesome let's try and add one more send and that works perfectly as well great alright then okay let's now talk about the different types of methods and what they represent so there are no strict rules that are enforced on how we should use methods so for example we can use the delete method to get data however there are very strong conventions that we should follow right so for example it would be a bad idea a very bad idea to use the delete method to retrieve data and it wouldn't be good to use the get method to delete data usually the get method is used purely for retrieving data and the delete method is used for deleting data and the post method is conventionally used for creating new data so here we are currently using the delete method to add data to create new data so this is something the post method should be used for not the delete method because this is confusing so let's go back to the code and let's change this so that's we just use the post method for adding an item okay let's save this now and let's go to the terminal and restart the application so ctrl C do you cancel the execution of this one and now let's do go build and and go lang - basic - HTTP - API press enter and then let's go back to postman and now let's try and send this request and now we get a four or five method not allowed status great let's change this to get and let's send this and you also get a four or five status great let's now test this we have a post and let's click send and that works awesome great alright then let's go back to the code and let's make this a bit more interesting so first of all let's get rid of this test route here we don't need any more let's get rid of this and it's also delete the test function we don't need this okay so now we are just left with the add item function and the routes here okay so let's make this a bit more interesting instead of storing strings as our data let's instead use a custom type so let's go and create a custom type and in go we do this using structs so let's type in type and now we want to type in the name of this struct which will be item and this will be strapped and then we can use curly brackets and now we can type in the fields that this struct will contain so this will have a data field which will be of type string okay and that's all it will contain for now later on we will add one more field to this but for now this struct just contains one field called data which is of type string okay and the next thing we have to do is change this string slice to a item slice okay so let's change this to item and let's change this to item as well great let's now go to the add item function and let's now update this to use the item struct the custom type that we just created so first of all we aren't going to be using a root variable we are instead going to be using the root body so if we go back to postman we can see that we have this tab here called body this is where we can define the request body we are going to set up our API so that we can send JSON data in the request body so let's go and do that let's go back to the code and let's get rid of this okay and now let's go back up here to the main function and let's change this so that we don't have this root variable here let's get rid of this okay and now let's go down here back down to the add item function and we now have to get the item value from the JSON body we can do this using the JSON Dakota with the request body which is stored in the request value this one here so let's go and do that first of all let's create a new variable called new item which will be of type item next let's use the JSON dot new decoder decoder and let's pass in the i/o dot reader which will be the request which is r dot body ok great let's now do decode and inside here we have to pass in the variable where this JSON document should be decoded to and this is the variable that we created up here called new item and actually this isn't the value new item this is a pointer to this location to the location where the new item value is stored so that means that we need a pointer and we can do that by getting the of the new item variable and we do that in golang using the ampersand sign okay so that will now get the address of the new item variable great the next thing we have to do is we have to append this new item to the slice that we called data okay so let's go down here and let's do data is equal to append data and then pass in new item not root variable okay so new item great and that's actually it let's save this now and let's go to the terminal and actually it says here that we have a problem and it's saying that the exported type should have a comment or B unexploited right okay so the reason it's saying this is because this is an exported type meaning that it has a capital S up here which means it's exported so that other files can access it so we can either make this a small I so it's unexploited like that or we can just put a comment here and let's set this comment to item is a struct that groups all necessary fields into a single unit so that is basically the definition of a struct pretty generic but that will now prevent that warning from showing up here okay let's go to the terminal and let's now do control C and you go build and an dot slash go like - basic - HTTP - API press Enter great and now let's go to postman and and now let's try and do this request /ad /dev sucker this shouldn't work but let's see what happens let's fix end great so now we get a 404 not found status awesome and that's because we no longer have that routes which was slash add slash item variable we changed the route to just slash ad so let's change that to slash ad and let's click the send and let's see what happens cool so we get an array with an empty object the reason it's an empty object is because we didn't pass anything in the body so let's change this to war and let's change the type to JSON and let's now and let's now create a JSON object to send in the request body ok let's set data to ABC and let's just make this a bit smaller okay and let's click send so this isn't good the request is working because we get two objects now and let's try this again and we now get three objects showing up here so the request is is kind of working but it's not putting the data inside the object so let's go and fix this the reason this is happening is because this is an unexploited field ingo only fields whose name starts with an uppercase letter is exported so this field is not exported this means that this field can't be seen by code that is outside this struct and therefore the encode method down here the encode method won't put this field into the JSON because it can't see it so to fix this we can go back up here and we can set this to an uppercase letter okay so now we have data which starts with an uppercase letter which means it will be exported so let's save this now and let's try this again let's go to the terminal and its new ctrl C and it's now rerun this come on go build and add dot slash golang slash basic - HTTP - API press ENTER and let's now go to postman and let's now rerun this request awesome so now we have an array with one object in it and that object has the field with the key of data and the value of ABC awesome let's try this again and set this to testing and let's click send great so now we have two objects in this array awesome so that's working really well great but what if we wanted to set the key here to start off with a lowercase letter we can't set the the field here to have a low case letter because that means it won't be exported and we won't be able to see the field in the JSON here so how do we set this to a low case letter go now provides us with a very useful tool called JSON tags and it's included in the encoding slash JSON package that's we've imported up here and to use JSON tags we just have to do a back tick here and then we can do JSON : and here we have to pass in a string for what we want this key to be so let's set this to data and make sure that you encapsulate this in double quotes like that okay let's save this now and let's go to the terminal and let's do control C and let's now rerun that command press ENTER and now let's go to postman and let's now resend this request great so now we can see that this has a lowercase letter awesome so that is how JSON tags work this can be literally anything so this could be this can be anything let's save that and let's restart the server and let's go to postman and let's send this and now we can see that this has changed to this can be anything and the reason that we don't have any data here is because we have to actually change this as well to this and be anything okay let's now click send and now we can see that we have the data in here great let's now go back to the code let's now go back to the code and let's change this to data let's change this back to data okay and all right then let's now add a new field let's call this field other data and let's make this an integer an int and let's set the JSON tag to other data okay let's save this now and let's now go to the terminal and let's cancel the execution of this using control-c and it's now re-execute that come on and press ENTER and let's go to postman or then so let's now send this request like this and here we get an array with an object that contains data and other data great so the new field is showing up here awesome okay but we don't have any data in here and that's because we have to change this to data okay let's not pick send and now we have this new object here which has the data set to testing great and here we can see that the other data is being set to zero this is because we haven't specified what that field should be here so go is setting it to the default value for integers which is zero this is the zero value this is the default value for integers it's called the zero value and we can see if we search here oh Lang zero value it says here that variables declared without an explicit initial value are given their zero value and the zero value is zero for numeric types such as integer so that is why this is zero because no explicit initial value was providing let's provide a value here other data let's set this to five five five let's click send and now we can see that we have this new object here which contains five five five as the value for the other data field great cool all right so let's go back to our code let's now make this a bit more interesting let's actually delete this struct here and let's create some new custom types that will resemble a real life application all right then okay so let's create a new struct called post so type toast strap and the post struct will have a title field which will be a string and the JSON tag will be title with a lowercase T okay and it will have a body field which would be a string as well and the JSON tag will be body okay and the next field will be an author field author and now we don't really have a good type for this we can't really set this to a string because the author will have multiple properties such as the name and the email but for now let's set this to a string and let's just presume that the author field will be the name of the author and it set the JSON tag here to author great let's save this now and now we can see that we have some errors let's look at what this is this is a warning that saying exported type Post should have a comment or B unexploited okay so let's quickly give us a comment post is a struct that represents a single post okay let's save that now and that warning has gone great let's now fix these errors here let's change this to a slice of posts okay let's save that and that error is gone let's now move on to the next error which is here let's change this to new post which is of type post okay and let's save that now okay and let's fix these errors let's just change this to new post and it's changed this one to new post as well great let's save that and now we don't have any more errors great let's now go back up here and actually let's go to the terminal and let's cancel this one using ctrl C and let's read execute that command to rebuild the application and restart the API and let's now go to postman and let's test this out let's change the JSON body here and let's set title to Nicko's and let's set the body as lorem ipsum and let's set the author to John Doe okay let's send this request now we get an array with this object great let's now go back to the code and let's go here to this data variable and let's change the name of this variable to posts so that it makes a bit more sense and let's save this and it's going to fix the errors let's change this to posts and this to posts and this one here to posts let's save this and all of the errors go away great the next thing we want to do is we want to make this author be a be a more complex type the author should have multiple fields such as their full name their user name and the email so let's create a new custom type called user so type user straps and let's create a full name field full name field which is of type string and the JSON tag will be full name and let's create another field which will be user name which will be a string and the JSON tag will be username okay and the last field will be email which will be a string as well and the JSON tag will be email great let's save this now and let's now go back down to here and actually let's fix this warning let's just put a comment here user is a struct that represents a user in our application okay let's save that great so now we don't get this warning anymore let's go back down here to the post struct and let's just change this author type to user great let's save this now and let's now go to the terminal and let's now we execute the go build command and restart the API let's now go to postman and let's and let's now send this and we get this object here and the author field is a object which contains the full name the user name and the email great but these don't have values because we didn't specify what they would be we actually provided an incorrect value here so go didn't put any values in here so let's change this to an object and let's set the full name to John Doe and let's set the user name to John Doe and let's set the email to Jane Doe at Tesco ok let's now send this request and it's now scroll down here and now we have this object here which contains all of that information great so that works so now we have an API that can handle a more complex a more real-life situation where we have data that has multiple properties and nested objects so this is a nested object here so yes that's what most real life a prize will have and our API can now handle that great alright then let's go back to the code and let's make this API more interesting let's implement for crud functionality stands for create read update and delete and those are the four operations that we do with data we create data we read data and we update data and we delete data so let's make this API perform all four of those functionalities let's implement that now first of all let's go down here and let's actually change this route this route doesn't make much sense the slash add route doesn't make much sense because the path is describing the action right so slash add will add an item so the path is describing the action but the method is already doing that we are using the post method here which typically represents creating new data or adding new data so we don't need the route path to describe the action because the method is already doing that so let's change this to slash posts so now the route path is describing the resource and not the action great that's much better the next thing we can do is create a route for getting the items so let's do that now let's create a new route handler looser handle funk and let's set this to slash posts and let's now create that function so let's go here and let's create a new function called get all items and actually let's change this to get all posts and this is a HTTP route handler so it has to take in the HTTP response writer and the HTTP request which is a pointer to a HTTP request okay and there we go let's now go back up here this line of code here and let's set the function to get all posts and now let's do dot methods and let's set this to get okay so the get method will be used to get all the posts great let's now go to the get small posts function and let's implement this let's type in json dot new encoder and passing w and do dot encode and pass in the posts slice let's save this now and let's now go to the terminal and let's rerun this command okay let's now go to postman and let's go here and it's trying to send this request great so here we have a 404 not found status that's good because we changed the route path the path is no longer slash add but it's now slash posts okay let's now click send and now we get a 200 ok status and we get this response body which has an array with the object that we just passed into the JSON body here into the request body cool ok so that works let's now go and see if the get request works let's now go and test if the get request works let's go here and this was an old request that we had let's just close this and let's create a new request and let's set this to a gets method and let's set the URL to localhost 5,000 slash slash posts okay and let's click send great so here we now have an array of objects however it's formatted in text not in JSON so to fix this we can go back to the code and we can set the header so W dot header and set the content type header to application slash JSON okay let's save this now and we're guessing an error oh this should be dot set sorry okay so this should be like this and then dot set like that save that and there we go let's now go to the terminal and that's ctrl C to cancel that and it's really execute this command okay let's now go to postman and let's resend this request and now we get an empty array and it says JSON here great let's now go to the post request and let's send this let's send this request great and now we have this here let's now go back to the get request and let's send this and now we can see that object in here great so if you get requests it works let's go back to the post request and let's add a few more let's set this to second post second post this this is the second post okay let's click send okay and here we have the second post great let's now go back to the get request and let's send this request and now we can see both of the posts here great awesome ok so that's working perfectly let's now go back to the code and let's let's now create another route for guessing and individual post so not all of the posts but an individual post so let's go here and let's do Reuter dots handle fun and this would be slash posts slash and then here we need a route parameter which will be the ID of the post that is being requested so let's use curly brackets here and let's set the parameter as ID okay and and let's now go and create the handler function so let's go down here and let's do funk actually let's um let's do it up here so that it makes more sense so we will have funk get post and then we have the get all posts and then we have the add item okay so we will have the get functions up here and this is a route handler so we have to pass in W HTTP response writer and R which will be a pointer to a HTTP request okay okay so now let's go back up here to this line of code here and let's pass in get post and let's now do dot methods and this will be get this route we'll be using the get method okay great let's now go to the get post function and let's implement it so first of all we have to get the ID of the post from the route parameter okay so here we can do VAR ID param which is off type string is equal to max VARs and it takes in the pointer to the HTTP request value which in this case is R and then this returns a map so this will return a map of all of the route parameters in this case we only have one parameter and we are only interested in getting that one which is the ID parameter so so because this returns a map we can get the ID value by just passing in ID in square brackets to get access to the value in this map which has the key of ID okay and the next thing we have to do is we have to convert this from a string to a integer so the way that we can do this is by using a function that is part of the string conversion package and this function is called a 2i and it returns two values it returns the integer and it returns an error and if there's an error then the error value won't be nil so let's do ID so that is what we will call the variable that holds the integer ID and we will then say error is equal to STR Co NV so that is the package that we want to use and then do dot a to I okay and then pass in the string that we want to convert to an integer and that is the ID Pam variable okay and actually this has to have a code on here because you aren't initializing the variables explicitly using var so that's why we have to use the code on here okay and the next thing we have to do is check if there was an error diff error isn't equal to nil then there was an error there was an error so let's do W right header and let's pass in 400 this is the status code and let's do W dot right and this method takes in a slice of bytes so let's actually do a conversion from a string to a slice of bytes so let's type in slice up bites and then do brackets and in here we can then type in a string and it will then be converted to a slice of bytes so let's type in ID could not be converted to integer okay and then this return let's return so that the code below won't be executed if there's an error okay so let's now go here and let's do some error checking if ID is greater than or equal to the length of the posts slice then the ID is out of range because in this API the ID corresponds to the position of the post in the slice so the ID is the index so if the ID is greater than or equal to the length of the posts then the ID is invalid it's out of range so we have to do W dot right header 404 this is the status code for not found and then let's do W dot right and let's do the same thing as above let's convert a string to a slice of bytes so let's do a slice of bytes and then brackets and then inside here let's type in a string no post found with specified ID okay and then let's find that post so let's do post : equals posts and then let's get the post that's in the index position specified by the ID and then let's do W dot header dot sets content type to application slash JSON and then let's do json dot new encoder and pass in w and then do dot encode and pass in post so this will then send that post that is at position ID in the post slice it will send this in the response all right then let's save this now and we can see that the string conversion package has been automatically imported great so let's now go to the terminal and let's restart the API let's re-execute this command to build our code and start the API and press enter and let's now go to postman and let's first of all go to this get request here get low close to 5,000 slash posts let's send this and we can see that we have no posts so before we can try and get an individual post we first have to create one so let's go to the to the post request here the request with the method of post so that we can create a new post so let's change this to first post okay okay and let's click send okay and that's been added let's now go to this request click send and we can see that we get all of the posts and currently we only have this one here okay let's now go and create a new request here and let's test out the request to get a spur cific post an individual post so let's set the HTTP method to get and let's set the URL to localhost 5,000 slash posts slash and then here we have to pass in the ID and currently we only have one item this one here and this item is in index 0 because it's the first value in the slice so it's at index 0 let's go back to this request here and let's type in 0 ok let's click send and there we go we get the first post being shown here in the response awesome let's add another post let's go to this request here the request with the post method and let's change this to second post second post ok let's click send ok and now we can see that we have two values in this array the first post and the second post great let's now go to the get request and let's click send and we can see here that we get an array with both of those posts here so now this is where this request here the request that gets an individual post will look different to this one here because this request here has returned two items and this one will now still only return this one because we are retrieving an individual post at index 0 let's click send great so we only get this post here awesome let's change this to 1 let's change the ID to 1 and let's click send and now we have the second post here in the response great so this route is working awesome let's go back to the code and let's create another route let's now create a route for updating a post let's go up here and let's do route don't handle funk and let's do slash posts slash and then here we will need a route parameter for the ID because we need to know which post we need to update so let's do curly brackets and let's type in ID like we did up here with this one okay and now we have to pass in the route handler function so let's go and create that now let's scroll down here and let's create a function for the update route handler so let's do function update post and this will take in W which will be a HTTP response brighter and up taking R which is a pointer to a HTTP request okay there we go and let's now go back up here and let's specify that the route handler function is update post there we go and then let's do dot methods and for this we are going to use the puts method the put method is usually used to update resources so that is the method that we are going to use for this route okay let's go back down to the update post function and let's implement this function first of all we have to get the ID of the post that we have to update and that ID is in the route parameters so let's get the ID of the post from the route parameters okay let's do the same thing that we did up here so let's do that same thing down here let's do VAR ID param which is of type string is equal to max' dot avars and pass in our and this will return a map so we can then just pass in ID to get the value that's at the key ID okay and then we can do ID comma error colon equals and then do string conf so STR Co NV dot a2 I and then pass in the ID param string like that and that will then convert the ID parameter from a string to an integer and store it in the ID variable okay and now we have to check if there was an error so if era isn't equal to nil then then there was an error so let's do W right header and set the status to 400 and then let's do W dot rights and this takes in a slice of bytes so let's do a conversion from string to slice of bytes okay okay so do the square brackets and then byte and then normal brackets and inside here we can pass in a string ID could you know you converted to integer okay and then let's do some error I think yeah ID is greater than or equal to the length of the post slice then the ID is out of range the ID is invalid so let's do W right header and let's set the status code to 404 before not found and let's now do W dot right and let's do a conversion from string to a slice of bytes so do the square brackets and then bite and then the brackets as usual and type in a string here which will be no post found with specified ID okay and then here we have to return so that the code below doesn't get executed if there's an error and actually we have to do the same thing up here let's return here as well okay great let's now go down here okay so that is all of the error checking out of the way and now we can get the value from the JSON body in the request okay so first of all we have to create a variable to hold the decoded data from the JSON body and let's call this variable updated post which is off type post and now let's get the value from the JSON body let's decode it into this variable so let's do it drinks on dot new decoder and pass in the JSON document which is V request dot body okay and then let's do dot decode and here we have to pass in a pointer to the place where we want to store the decoded data and that is the updated post variable so let's get the address of the updated post variable and pass it into the decode method so to get the address we have to use the ampersand and then the variable name updated post okay great and then let's do so now we have to update the post value in our posts slice so let's do posts and let's get the post at the index of the ID that was passed in in the request parameter and let's set this equal to updated post okay and then let's just send it in the response so let's do trace 1 dot new encoder and pass in W and then do dot encode and then pass in updated post and and we have to do W dot header dot sets content - type and set this to application slash JSON okay great so that's good let's save this now great so let's now test this let's go to the terminal and let's do control C and this will execute this command press enter and let's go to postman and let's create a new request here and let's set this to put and actually before we can do this we first have to create a new post those currently if we go to the get slash posts route and send we don't have any posts so let's go to the post request and let's change this to first post okay okay and send this okay let's go to the get request again send and now we have one item in the array which is this first post great let's now go to the put request and let's type in localhost 5,000 slash posts slash and then here we can type in 0 and let's go to the body tab here and let's go to war and set it to JSON and let's now and actually let's go to the post request and let's just copy what we've got here and let's paste it in the put request okay and let's just change some things so let's change this to updated and let's click send and it's saying cannot get any response oh okay that's because I I got the port wrong I missed out 0 okay great so so localhost 5,000 slash posts slash 0 let's click send and there we go great so here we have the updated post object in the response body and the title has been set to updated great let's go to the get request and let's do get slash posts slash 0 and let's end this and here we can see that we have updated awesome let's go to the get stash posts so we get all posts routes we did send this just before we made the put request so this was the state of the posts slice just before we made the update let's see if this says updated okay let's click send great awesome so that post has been updated great let's go back to this request here and let's now see what happens if we change these fields in here inside the nested object let's see if we can change this to Jane Doe Jane Doe let's change the user name to Jane Doe and it's change this to Jane Doe at testing comm and let's now click send okay and that seems to work the fields inside the author object have been updated so here we have Jane Doe and Jane Doe and Jane Doe at testing comm awesome so this route works but what happens if we don't specify all of the fields let's say that we only wanted to update the title so then there is no point in specifying these fields because we're not changing them so let's not include them and let's just include the title and let's change this to this has been updated again okay let's click send okay and this happened so the title has been updated that's good but all of the other fields have been set to their 0 values their default values so they are now all blank the reason this is happening is because we are replacing the post with what was passed in into the request body and since we haven't specified all of the other fields all of the other fields were set as their zero value their default value and in this case since these are all strings these are all the empty string because that is the zero value of strings so the pull request is actually typically meant to replace the object so this is actually okay however it would be nice to provide a way for the consumer of this API to be able to send update requests with only the data that they want to change so that the consumer of this API doesn't have to send the whole object with all of its fields in the request body if they don't have to update all of the fields okay so let's create a route that can take in partial data and just make the updates to those fields okay so let's go back to the code and let's go up here okay and let's do Reuter dot handle funk and this would be slash post slash ID as usual and let's go and create the route handler function for this and let's call this PAC post okay and this is a route handler so it has to take in the HTTP response writer and has to take in a pointer to HTTP double press okay and let's now go back up here and let's pass in that function here so cache post and the methods will be patch okay great let's now go back down here and let's implement the patch post function so first of all we have to get the ID of the post from the root parameter and we have done this already up here and also in the get post function as well so all of this code is exactly the same so let's go to the update post function and let's just copy this section here let's copy it okay and it's pasted it here in the patch post function great okay let's now get the current value get the current value so let's create a variable called post and let's set this equal to posts and then pass an ID okay and then let's do json dot new decoder and pass in our and then do dot the code and then let's pass in the address of this post variable here because the decode function requires a pointer so let's pass in the address of post okay and and then let's now do W header dot sets contents - type and set this equal to application slash JSON and then let's do trace on dot new encoder and pass in W and then do dot encode and then let's pass in post to respond with the updated post value okay let's save this now and let's go to the terminal oh we have an area what's this D code reads the next JSON encoded value dah dah dah dah cannot use our as type I or reader in argument oh okay this is because this has to be our body okay let's save that there we go great okay so now we don't get any errors cool all right cool okay yes so this will decode the JSON object in the request body and then it will be decoded into the post variable okay let's save this now and let's go to terminal let's do control C and let's reax acute this command okay let's go to post man let's create a new request here and let's set the method to patch okay and before we do this let's actually go and create a new post because currently if we go to the get slash posts request and send we don't have any posts so let's go to the post request and let's send this okay so now we have the first post in the posts slice okay great and let's confirm that by going to the get stash post request and sending it and there we go so now we can see that we have an array with this item in it great let's now go to the patch request and let's set the URL to localhost 5,000 slash posts slash zero okay and and let's go to the body tab and let's click on war and set this to JSON okay and let's create a JSON object here and let's set the title to updated okay let's click send and there we go so now we have the updated value being shown here and the title is now set to updated and everything else is the same great so we were able to send only one field to the API and only that field was updated great awesome let's now try and also update something else let's update the body and let's set this to this has been updated as well okay and let's click send and now we can see that the body has also been updated great let's remove these and let's try and update the full name of the author okay so let's try and update that let's do otter is an object and let's set for name to so it's currently John Doe let's change this to Jane Doe Jane Doe okay and let's send this and there we go so this was able to only update the full name of the author great there's also updates the user name to Jane Doe and it's click send great cool so the patch root works perfectly awesome let's just double check that it's worked by going to the get request and we can see here that we have this first post let's click send and let's see if it's been updated it hasn't been updated so this didn't work the patch requests that we made didn't work because this post value hasn't been updated the reason this is happening is because here we are creating a new variable and the contents of this variable are copied from what's stored at the ID index of the posts slice so here we are creating a new rebel and we are decoding into that rebel however this new variable is lost it's not saved to the slice so there's two ways we can fix this here we could do posts ID is equal to post so save that and let's go here and let's do ctrl C and we execute this command in the terminal okay and let's now go to postman and let's try this again let's go to the post request and let's create this post okay that worked let's go to the get request and click send okay so now we have this first post here great let's now go to the patch request and let's send this request great so that seems to have worked here let's now go to the get request and let's see if it's updated here let's send this great this has now been updated here we have the full name of Jane Doe and the user name is Jane Doe great so this has been updated awesome so that is one way of fixing this issue another way is we can set the post to be a pointer to what's stored at posts ID so let's try that let's delete this line of code here and let's set this post virbull equal to the address of what stored at posts ID so let's use the ampersand to get the address of what stored at posts ID so now this is a pointer to that value and in here we can remove the ampersand here because it's already a pointer okay let's save this now and let's go to the terminal ctrl C and let's reax acute this command and let's go to postman and let's try this again let's send us a request and here we can see that we don't have any items so let's go to the post request and click send okay let's go to the get request again click send and now we can see this first post okay okay let's now go to the patch request and let's now send this request okay let's now go to the get request and let's send this and hopefully it should be updated click send and there we go this has now been updated the full name is set to Jane Doe and the username is set to Jane Doe great so this method also works by using a pointer we were able to fix the issue all right then okay so that now works the last thing we have to do is create the delete root so let's go up here and let's do Rooter handle fun and let's set the path as slash posts slash ID okay and we now have to create the root handler function so let's go to the bottom here and let's create a new function called delete post okay and this will take in a HTTP response writer and a pointer to a HTTP request okay cool so let's go back up here and let's set the handler function to delete post great and let's set the methods here dot methods to delete great okay let's now go to the bottom and implement the delete post function so first of all we have to get the ID and do the error checking which is the same as what we've been doing in all of the other functions so let's go to the patch post function and let's just copy this code here let's copy it and let's paste it in the delete post function okay great cool okay so so now we have to remove the post from the posts slice and to do this we have to use a trick because there isn't any delete method on a slice in go the good news is is that there is a page on the golang wiki that shows us how to delete from a slice so from here we can just copy this and paste it in our code okay so let's paste it in here there we go and let's change a to posts and let's change it here posts as well and it's changed it here to posts as well great cool alright and let's also change this I to ID and here as well ok great let's just add a comment here delete the post from the slice okay and I also put the link here to that to that github page that github wiki page for these slice tricks this one here okay cool so the next thing we have to do is we have to send the response and for the response we don't really have anything to send in the response body so let's just send a 200 ok status and that should be enough so let's do W right header and let's just set this to 200 ok great and that's it let's save this now and let's go to the terminal and it's control C this to cancel that and let's really execute this command press ENTER and let's go to postman and let's send this request to the get slash posts request let's send that and we have an empty array okay let's go to the post request and let's send this okay and let's go to the get request again let's send this and now we can see that we have one post in the slice alright then okay so let's now go and create a new request in postman let's set the HTTP method to delete so that's we can test out our delete route and now let's type in the URL of our delete route which is host 5,000 slash posts and then here we have to do slash and then the ID of the post and the ID is going to be zero because it's the first item in the slice okay and let's go and click send and here we have a 200 ok so here we have a 200 okay status so that looks promising and let's now go to the get request let's go back to the get slash posts request and let's click on send and now we get back an empty are a great so that means that the delete route worked because it was able to delete our post from the slice awesome great so that route works let's now do a few more tests let's go to the post request ok and let's click on send and let's go to the body and let's try and change some of these and let's change this to 2nd post and click send and let's go to the get request and let's send this ok and we now get two posts in the JSON array let's change this so that this says this is the second post so let's go to the patch request and let's go to the body and first of all actually actually first of all let's go to the URL and let's change this to 1 ok let's change the ID to 1 because we want to change the second post which is the second element of the slice so the index of this post is 1 okay so let's go back to the patch request and let's now go to the JSON body here to the JSON body and let's change this so that we just set the body and let's set the body as this is the second post and let's now pick send and now we get back the updated post and the body is as this is the second post great so the patch request works that's good okay let's now go to the put request and let's try and do a put request to slash one to that post which has ID of one okay and let's go to the body and let's try this let's send this and now everything else has been set to its default value the zero value of these fields great ok so the title has been set to this has been updated again like we did up here and everything else has been set to its default value this is good because we are using a put request so our intent is to replace the post that is at index one of the slice and that's what we did great let's now go to the delete route and let's try and delete it but before we do that lets go to the get request and let's send this okay and here we can see that the second post has been updated to this post data here that's good let's now go to the get request here to get a specific post and let's send get slash posts slash one and let's go and send this and now we get just that post in the response great and the reason I wanted to do this was so that when we delete it we can then go back and see if it's still here okay so let's go to the delete route the delete request here and let's click send oh and let's just change this 2/1 so that the post with ID 1 will be deleted ok let's click send and we get a 200 ok so that looks good let's now go to the get request and let's now click send and now we get a error could not get any response could not get any response let's go back to the code and the terminal and let's see what happened so here we get an error okay okay and here we can see that the error is that the index was out of range index was average one with a length one this does make sense because the index is arranged but I thought we did add some validation to prevent these errors from occurring to prevent these errors from crashing our program so let's go and see why this is happening let's go to the get if you get get post function get to post and let's see what's going on so if ID is greater than or equal to length of posts then this should happen okay so the ID that we passed in was one so if one is greater than or equal to length of posts and the length of posts was one because we still have one post inside if we go here and send this then we have one post in here so the length of posts should have returned one so if one is greater than or equal to one then this should have happened oh okay so the reason that this is happening is because this code is still being executed because we aren't returning here we should return so that we prevent this code below here from executing okay so let's now save this with the return statement here okay and let's now go to the terminal and let's now do ctrl C to cancel that and it's now we execute that command okay press ENTER and let's go to postman and let's click on send here on the get request we get slash posters request on the get slash post request okay press send and we get an empty right that's good let's now go to the post request and let's click send and let's go to the get request and let's click send okay so we now have this post in here let's now go to the get request here and let's do get slash posts slash zero and let's click send okay and let's now go to the delete request here and let's change this to slash posts slash zero because we want to delete the post at position zero of the slice at index zero of the slice okay and let's now click on send we get a 200 ok let's now go to the get slash posts slash zero request and let's now click on send and hopefully we won't get an error like we did last time okay let's click send and there we go now we see no post found with specified ID and a 404 not found status great so this is now fixed because before this would have crashed let's actually go and see what happens when we comment out this line of code let's just comment that out let's save that and let's go to the terminal let's do ctrl C and we execute that command and let's go to post man and first of all let's go to this request here the get slash post request and it's click send okay so we get an empty array that's good let's now go back to the get slash posts / ID request where the ID here is zero and let's click send and here we get that error where the API can't respond because we get this error here so there we go so it's because of this line okay so we have to return here so that this code isn't executed when the ID is out of range when the index is out of range okay let's now save this and let's go back to the terminal and let's do control C and let's reax acute this press enter and let's go back to postman and I think we've tested everything but let's do one last check let's go to the post request and actually let's go to the get request here let's go to get slash posts and let's click send and we get an empty array that's good let's go to the post request to create a new post let's call this first post and let's change this to testing one two three this is the first post okay and let's click send okay and we get this array back okay and let's now go to the get request here and let's click send and we can now see that we have this post in the array great okay let's now go to this request here and now let's click send okay and that works as well let's actually change this to an ID that is out of range let's change this to an ID that doesn't exist for example five and let's click send and we get a 404 not found error great let's try this again with slash zero now fix end and we get this back great so that works let's now go and try and update this using the put request so let's actually go back to the post request and let's just copy this JSON object and it's paste it into the JSON body of this request the put request let's paste that in and let's actually change this to slash posts slash zero because that's the one that we are working on because we only have one post in here so slash posts slash zero and let's now go to the JSON body and let's make some changes let's change this to update it by put request ok and let's go to the body and let's just change this to ABC and it's changed the author as well and let's change this to Jane Doe and let's change the username to Jane dot doe and let's go to the email and change this to Jane Doe at test calm ok let's now click send here we get back the object with the updated films great so the put request works let's now go to the patch request and let's change this URL to slash posts slash zero and let's just change the body and let's change this to this cuz being updated by patch request by a patch request okay let's click send and there we go the body has been changed to this has been updated by a patch request and everything else has remained the same great so the patch request is working and the final route that's we have to test is the delete route okay so let's make sure that this is slash posts slash zero and let's make sure that the method is delete and let's go and click send okay and we get a 200 ok status that looks good let's now go to the request and let's send this request and here we get a 404 not found because it doesn't exist because we deleted it awesome ok so that's working let's now go to the get request here the get slash post request that gets all over the posts and let's see if we get an empty array ok let's send this and we get an empty array awesome cool so that was all of the routes we have now tested all of our routes so now this API can create read update and delete data awesome so now we have a fully functioning crud API that's been written in golang awesome ok so that's the end of this tutorial thank you so much for watching this video please let me know what you thought of this video down in the comments below and I'll see you in the next video
Info
Channel: Devstackr
Views: 25,374
Rating: undefined out of 5
Keywords: webservice, web development, golang api development, web services golang, go api development, go backend, golang backend, json, go json api, golang json, route params, dynamic route variables, HTTP protocol, protocol
Id: HmiybuiEZI4
Channel Id: undefined
Length: 130min 50sec (7850 seconds)
Published: Sun Jan 12 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.