Project introduction

GetGoing: Introduction to Golang Introduction to API development with Go
8 minutes
Share the link to this page
Copied
  Completed
You need to have access to the item to view this lesson.
This is a free item
$0.00
د.إ0.00
Kz0.00
ARS$0.00
A$0.00
৳0.00
Лв0.00
Bs0.00
B$0.00
P0.00
CA$0.00
CHF 0.00
CLP$0.00
CN¥0.00
COP$0.00
₡0.00
Kč0.00
DKK kr0.00
RD$0.00
DA0.00
E£0.00
ብር0.00
€0.00
FJ$0.00
£0.00
Q0.00
GY$0.00
HK$0.00
L0.00
Ft0.00
₪0.00
₹0.00
ISK kr0.00
¥0.00
KSh0.00
₩0.00
DH0.00
L0.00
ден0.00
MOP$0.00
MX$0.00
RM0.00
N$0.00
₦0.00
C$0.00
NOK kr0.00
रु0.00
NZ$0.00
S/0.00
K0.00
₱0.00
₨0.00
zł0.00
₲0.00
L0.00
QR0.00
SAR0.00
SEK kr0.00
S$0.00
฿0.00
₺0.00
$U0.00
R0.00
ZK0.00
Already have an account? Log In

Transcript

Hi everyone, in the next couple of videos, we're going to be making a to do API. And the specification for that API is in the resource section of the repository. So I want you all of you guys to take a look at this. And now what the structure is going to be is that we're going to be devoting one video to understanding the theory and theory and how the syntax works. And in the next video, we're going to be including that syntax by implementing the real world endpoint. And all of the endpoints that we are going to be implementing are defined over here.

So for the sake of this to do API, I'm going to code everything in Visual Studio code. And just so you can get a good view of all of the directories and everything easily. So let's get started with main.go. And with our current expertise, we can make a check API health point, which is just going to return us a response code and response code 200 status. So yep, So let's get started you're going to do a package meet. And then let's get started with the phone.

And let's quickly get get also up and running. Listen and so and we want everything to be listening on port 3001 it's doing right now and see if you obviously want to import the format package and the net slash HTTP package. Yep. So let's define a new multiplexer and let it be a straight up door new fork mux. And we're gonna do a mux door, handle func and he wanted to handle slash ping route. Whenever you get a get request on the slash spring road, you want to say that do something HTTP toward this one's right.

And then we have a reference to the HTTP request. Then using both of these things, I'm going to say that if our dot method is equal to get all we can do HTTP dot method get get, then they don't something. Right. So for our purposes, when we are making when we mentioned an API, in our case, we're going to be making a JSON API. So JSON is JavaScript Object Notation. So it's in the value, it's in the value of key value pairs.

So an example of JSON might be key and value. So all of our Things are going to be in JSON. So one thing about ghost rocks is that we can serialize and deserialize those structs in the form of JSON. So let's have our response struct ready so we can do, or, let's do one more thing. Let's have all of our structs in some other file. So we can do a struct start.

Let's create a new folder for our structs or I'm gonna call it struct. So I'm going to shift my function to structs move, and I'm gonna call it I'm gonna call it packet straw. Yep, so I'm gonna define a new struct which is gonna say this once. And then so is going to take two fields first is a code which is Going to be an integer. And the second thing that this is going to take is the body, which can be interface or anything, right now. Now what I want to do is, whenever I say whenever I send in a response, I want it to be serialized as JSON.

So for that we can use tags. So you can say JSON serialize this as code. And if you're talking about the body, so utilize this as the body. Now, whenever we whenever you are sending a response in the JSON format, it's going to say, okay, it's going to look for these tags, JSON, and then it's going to automatically serialize the input fields accordingly. Now, to import this package, we can simply do a utility to a struct. The new motor pack, the name of the package was structs.

Okay, you can do a struct stored response. So we automatically included our response. So now to send something in JSON, we do a JSON dot new encoder, because we are encoding our current structure. And we are encoding it into the response, right. So we created a new JSON encoder, which acts like a wrapper around response writer because eventually we're going to be needing to write everything to the response writer. And then we can encode our new structure or our data.

Now it's time to define our data. Our data is going to be strux. About response. Code is in the case of our API specification going to be 200. And I just want this one to be blank. So 200 or in our case, we can do HTTP dot status.

Okay. status, minor typo. Okay. And then bored. You know what one more thing that we can do is that we can do W dot. Yeah, we can we can write headers.

But in this case, we don't need to do so. So let's see what happens. So, I'm going to start this over right now. I'm going on, glued on means it's listening. Now, let us do a curl request or other let us see in the browser only because we are using a get request. Then what happens so slash ping?

And it's a photo for Nord phone. No. Why is that? Because HTTP dot listen and serve is listening on port 3000. And we are doing slash Ping. Well, that's my back.

Now I'm going to start this over again, I included the mocks, this time, refer to a ping, this is going to send it as JSON. So I have this Chrome extension, which converts to JSON and structures it. But if you take a look at code, you do a quick put localhost 3000, and I do a thing, then it's gonna send it in our JavaScript Object Notation format, where our code is 200. And our body is four. So I also recommend that you guys download this Chrome extension, it's very good. And this is, this is how we've created a force endpoint, a slash ping endpoint.

And towards the course of the next videos, we're going to be creating more endpoints. And I'm going to be explaining to you how each and everything works, so stay tuned guys and see you in the next video.

Sign Up

Share

Share with friends, get 20% off
Invite your friends to LearnDesk learning marketplace. For each purchase they make, you get 20% off (upto $10) on your next purchase.