Basics of Go

Frameworks & External Libraries

Maximiliano Firtman

Maximiliano Firtman

Independent Consultant
Basics of Go

Check out a free preview of the full Basics of Go course

The "Frameworks & External Libraries" Lesson is part of the full, Basics of Go course featured in this preview video. Here's what you'd learn in this lesson:

Maximiliano discusses the benefits of using frameworks and external libraries to simplify the process of building applications in Go. The segment includes the implementation of the Gin web framework as an example to demonstrate how frameworks or external libraries can be utilized in Go development.


Transcript from the "Frameworks & External Libraries" Lesson

>> That's how you make a restful API manually. Yeah, some of you might be thinking, well, that's a lot of work because we need to fill out, check the post, check the method. Well, if you wanna move further, remember I mentioned a Gin tool before, but that's not the same one.

This is a different one. It's called Gin Web Framework. And now, you will see why it's called a, I'm sorry, Gin Web Framework, not Go Gin Web Framework. That it's kind of its expressed, so you have expressed framework, you have Gin for Go, okay? So it will make your life easier for working with API for example what else we are we are not validating a lot of JSON.

We cannot make like complex routing manually. Also we are not using course right now, I know if you know what courses, but we should add manually the header for course. So the cross origin resource sharing that's a JavaScript security restriction that we need to add manually those headers in our API.

But in this case, using these frameworks is easy, okay? You have here all the documentation, how to install it, and After you use it, you will see here you have all the installation, they have the same idea. So here is how Gin looks like. So you create a new Gin with this and then you say, okay, I wanna receive a GET on that URL, and this is my handler.

It seems clear because you don't receive the writer and the request. You have received something known as a context that inside you have the request and the response. I have some quick functions. In this case, c.JSON is responding with JSON. You have c.TXT, you have a lot of utility functions, and then you run a server.

So at the end, it's the same, but you write less code if you use the Gin Web Framework. And you are filtering the method directly when you hook your handler. Okay, does that make sense? Cool, yeah.
>> Could you explain how or demo using an external library in Go?

>> How to use an external library? Yeah, sure. Lets say you gonna use Gin. Well, following the steps will help you with that. So, in this case, the first step is to get, this is similar to MPM install for those of you using MPM. So typically, you follow the steps that you have in the library.

So in this case, we copy. Go get into the terminal. So for example here, I'm going to stop my server and go get. It will download everything. Typically it's not like NodeModules, I mean there are chain dependencies. But not so much as NodeModule most of the time. It's done.

So after it's done, first, do we have a NodeModules or something like that here? No, we don't have a folder for this. Everything goes to a Go temporary folder in your computer. So, it's not like a folder within your computer. But what we do have is a change in our Mod file.

We have a require with the list of all the dependencies that we have right now, because we installed it. So, it's changing go.mod actually, okay? And here you have all the versions and also you have some actions check for upgrades. That is just vs code is helping me executing some CLI commands to do that and the next step is to import it and that's all.

And you use it. So we go to main.go, we import the library, without import because I have an import block like this. Now it's complaining because I'm not using it. And then you use it. That's all, so, in this case, gin. So, you say gin., and you have gin.

So, you set mod and we can even copy and paste the simple example that we have here. Okay, now that, and you're done. So it's actually pretty simple. The only step is to run the CLI command to get, so go get on the URL typically from GitHub because it's open source.

If it's an open source package, and then you import that and you're done. So this case what will happen if I run this? Okay, now it's listening on 8080. What happened with my server? 3333, are both running?
>> Yes.
>> Yes, no, let's see well let's try it will look at whole 3333.

Nope it's not there. 8080? No phone because it's only registering /ping. Ping pong. [LAUGH] That's a ping pong. So why my server is down? By the way, it has a nice output in the console about the request, that Gin. Why? Because that line, line 34 run is staying there with the thread forever.

When you start the server, the thread on that server stops there. So then, line 36 is never being executed unless there is an abort process within Gin.
>> Can we use a Go routine?
>> We can use Go routines and then launch two different servers or one in a Go routine, and yeah, in that case, if they're using different ports.

Then you will be serving on both ports at the same time. One with Gin, the other one, or you can create a new function, or if you wanna try something quickly, you can create a function on the fly, as we saw before, okay? You put this in a function, you execute the function at the same time, so you define the function and execute that, and you do that in a Go routine.

It's a pretty fast way to execute a piece of code in a Go routine.

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now