hi there sir dog yagerlop here for infoworld in this episode of smartgo we're going to create a
super simple json rest api in go using a couple of goes native packages in a third-party package
for the native package we're going to be using encoding json and netslash http and the third
party package will be our routing system http router go does have a native http routing system
but http router has gained a reputation for being faster and more efficient than the native options
so it's worth mentioning in the context of this demo by the way if you remember our demo for the
gen web framework a few videos back http router is used as a base component of gen
but we don't need all of gin just to set up a simple json api to create a json api we only
really need three things a handler for web requests which is what http router gives us
a way to serialize data in the json format which is what encoding.json provides us with
and a data structure that we will fill out and serialize into json the structure we're
going to be creating and using here is not very complex at all it's just a struct with two fields
name and year but the basic idea should be easy to grasp we're going to get a request from the
client populate an instance of the struct with data we want based on that request serialize
that structure json and then return the json to the client now with http router each endpoint for
our api is handled through a different function each route function that we have here is going
to take three parameters there is an http response writer object which is a way to return a response
a pointer to an http request object if we need the requests data and an httprouter.params object
which gives us access to url parameters so with the first route index all this does is just return
some placeholder text to the root of our site it's basically our poor man's form of api discovery
to tell you where to really go but it's worth going into a little detail about how this works
inside the function we're using format.fprint to take a string result and send it to the
http responsewriter object that we've that we've taken in here it's named w http router
will automatically handle taking that formatted response in the w variable and returning it back
to the client so if we navigate to this url you see all we're going to get back is plain text and
nothing that we pass in is going to change that now for actual api we have an endpoint that
has a single variable declared in its url name and which is handled by the movie api function
so there we're creating an instance of our struct and we're adding data to it for the
name you'll notice that we're using the params object and it's by name method to extract the
name variable from its position in the url for the year we're just going to use 1982 as an arbitrary
value for the sake of this demo a more functional api would pipe in data from whatever other source
we might be using like a database lookup or something but this is just to give you the idea
so once we have our data object we then serialize it with encode and we use the responsewriter
object again named w to receive those results so we can send them back to the client
so let's navigate to this url and pass in a couple of responses and the responses are about what we
would expect and as you can see from some of these responses even space even names with
spaces in them will get handled properly instead of being mangled in the url now i did say this was
a very basic json api obviously i've left out a lot of details here that you needed a proper api
like authentication or rate limiting or supporting the open api specification all of those things are
important if you're creating a public-facing api instead of just something you're setting up for
your own internal consumption so in future videos i'm going to expand on what we have here and show
how features like that can be added in but here we have the core of it you take data from the client
you turn it into an instance of a data object you serialize that into json and then you send it back
and we have support both within go's native libraries and outside and third-party libraries
to do every single one of these things so that's it for this video if you liked it please leave
a comment below don't forget to subscribe to the tech talk channel on youtube and for
more smart go and more david serdar be sure to follow me on facebook youtube and infoworld.com