Make A Server With Golang In 10 Minutes Using Gin

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] yo what's up guys this is afix and welcome so gin is a framework that you can use with goling and it's basically something that handles different http routes and it just makes your application a whole lot more easy to navigate and just to code and put together so this is the gin github repo the link is right here i'll put it in the description there are some install steps that i am going to go through with you guys and i'm going to get everything set up and we're going to just set up a little simple server so you can also download the go link programming language or you could use a website like rebel dot it i recommend downloading go locally for best performance but really it's your choice i have golang already installed it's a really quick setup and you can run these different things from the command line using the go tools so what i'm going to do is open an instance of the command prompt and over here i'm going to make a directory and i'm just going to call it simple gen api or server you can call it api if you want it really doesn't matter in terms of naming so once you create that directory we are going to a cd into that and we are just going to open this up in the text editor of choice and in my case it is visual studio code so before we can start coding anything we have to initialize a go module so in visual studio code you can do control shift tilde key to open up an instance of terminal or you can just use that command prompt we had earlier so i will use the command go mod init and i will do github dot com slash afix.dev slash simple gen server and when you're creating a go module it is recommended to use github.com slash your username slash the project name and instead of using github you could do git lab or whatever source control version thing you're using so what i'm going to do is now click enter and it creates a new go module if you misspelled something don't worry you can go ahead and change the name in your go mod now what we are going to do is install the gin go module so i'm going to do go and i'm going to do go get dash you and i'm going to get github.com and this is run by gin dash janik and dash or slash gin and this will basically import the gin library for us to use and when i click enter it will download all the files from github once that is finished installing you'll see our go mod populated with these modules and what we're going to do is create a main.go file and over in main.go we are going to declare a module or package main and declare a main function and this is kind of the go starter boilerplate um you have to have this and this is really the entry point into your program now what we are going to do is we are going to create a new gin router so you do r the walrus operator so that basically assigns and creates the variable r and we are going to set it to gen dot defaults and this will use the default gin middleware and what middleware is is kind of what's in between where you receive a request and where you really handle that request so what i'm going to do is import the gin module so i will do import and parentheses or quotes github.com and save that and you will see that that air cleared up and it's back again i think it's just an issue with the tooling um so what we're going to do now is we're going to get do a get request on the router so r dot and it's all caps get now you can change this depending on the http verb you want to use um you can do something like r dot post or r dot patch um it really depends on what you want to use so i'm going to use r.get and this is going to be the route and that's just going to be the forward slash for now and what we take in is a function and it's going to have a parameter of c which is a gin dot context and this might have uri parameters different things that really work as a context and if you want to learn more about it you could just click on it click f12 visual studio code doesn't seem to be working right now it's just an issue with my go tooling but if you are interested and your tooling is working you can go and check that out with f12 or you can actually go to google chrome so you can just open a new instance of google chrome and search for whatever you want and i'm just going to do gin context and you can see there's gen dot context and you can see these different things about good context you can look at the type and the different things it has within it so what i'm going to do is go back to my visual studio code and i'm going to implement this function and what we're going to do is just simply send a json to c so c dot json and we're going to do http dot status okay since everything is okay and we are going to pass gin.h and this will basically create a json object for us and we are going to set the key message to the value okay and save that make sure to add that comma after it's something that joe language requires and we need to import net slash http and you can see all these errors cleared up so what this is doing is when we do a get request on the forward slash you will go and send the request you'll send it back http dot status okay and json with a message that says okay and now we have to actually run our server so what we're going to do is our dots run and we're going to pass in the port so i'm going to bind it to port 90 90. and that's pretty much it so just do ctrl s to save and we'll go down into our command prompt instance and redo go run main dot go and you see that there is an error and i believe it is here we have to actually specify colon when we're binding the ports and when we do go run main.go you will see this alert to allow access to public private networks just click allow access and everything will work now to actually test out the functionality of our server we'll have to create a new terminal and what we are going to say in it is we're going to curl localhost and we we bound it to port 1990 we're just gonna call 1990 and put in that forward slash click enter and you can see that we get message okay and you can see that this server is really straightforward you can see oh look it's a get request to this router and it sends this thing json and then you just run it on port 9090 and it's just a really simple framework that allows you to set up things really really with ease and you can also separate out these different functions into different files so i'm going to do a quick example of that i'm just going to make a simple get dot go and i'm going to do i'm actually going to put this in something called controllers and i'm going to go and drag that get.go on it and this is going to be of package controllers and there will be a funk and i'm just going to call this get and it's going to take in a c which is a gin dot context and we will just implement that function no you have to keep this first letter uppercase otherwise it won't export now what we are going to do is call c dot json and we are going to just to send it a message or place so http status dot status okay and we're going to do gen dot h to create some json um engine.h is really just a string to string map and we're going to send it message okay and put that colon or the comma right there so do ctrl s to save uh tooling is not working so i have to manually import everything which is not too bad so i'm going to import net slash http and i'm also going to import github.com and i'm going to save that i'm going back to main.go and instead of implementing this whole function what i'm going to do is just do controllers dot get and move this to this line don't need that and there's one issue and that is that we have first off we have the net.http import that we're not using anymore which is not allowed in go um and we need to import our own controllers package so what we're going to do is add into our import statement github dot com slash afix dash dev and what i'm just going to call this is the simple gin server so you can see this is the same thing in our go module here module and it has all this detail and over here we are going to implement the package controllers from this go module and this will basically import our controllers and you can see that this error cleared up now if we go back to our go running terminal we'll do control c to exit out and we're just going to run go run main.go once again allowing access and we're going back to our cmd and we're going to curl localhost9090 and we get the same message so what you can do is maybe create different controllers for every http verb and action and it just makes it really easy to organize i hope you guys enjoyed today's video i'm definitely going to have more back end oriented videos coming out soon and devops oriented videos i hope you learned something from it if you have any questions or you want to hang out you can join the discord server link in the description down below [Music] you
Info
Channel: Aphix
Views: 607
Rating: undefined out of 5
Keywords: golang, go, goland, microservices, server, golang server, go gin, golang gin, gin, dev ops, backend, golang framework, golang api, api
Id: _GE-8Db1JNQ
Channel Id: undefined
Length: 11min 23sec (683 seconds)
Published: Sat Mar 06 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.