Installing and configuring Gin
Gin is a third-party package. To install Gin in Go projects, we need to use the go get
command. The command takes the URL of the package to be installed as an argument. Issue the following command to install the gin
package from GitHub:
go get github.com/gin-gonic/gin
Note
If you're running Go 1.16 and above, you need to disable Go modules via the GO111MODULE=off
option.
When checking out the gin
package, the go get
command creates a Gin directory in the $GOPATH/src
path. The directory will contain the source code of the Gin framework:
Begin by creating the hello-world
project directory under $GOHOME/src/hello-world
or any directory that seems appropriate:
mkdir -p $GOHOME/src/hello-world cd $GOHOME/src/hello-world
Open the folder with VSCode and create a main.go
file inside the project folder that contains the following content:
package main import "github.com/gin-gonic/gin" func main() { Â Â Â router := gin.Default() Â Â Â router.GET("/", func(c *gin.Context) { Â Â Â Â Â Â Â c.JSON(200, gin.H{ Â Â Â Â Â Â Â Â Â Â Â "message": "hello world", Â Â Â Â Â Â Â }) Â Â Â }) Â Â Â router.Run() }
The first line, package main
, indicates that this is the main module in this project. The import
section is for importing the gin
package. This package provides us with the router
variable, which is declared right below import
and the API context to be used while we send the response in our main
function.
Next, we create an HTTP GET
method on the root (/
) resource and define a function to be called when HTTP requests hit the root endpoint. The function sends a JSON response with a status code of 200 (OK) with a body of "message": "test successful"
.
Finally, we must deploy the router on port 8080
using the router.Run()
method. The following diagram summarizes how an HTTP request is processed in Gin:
To run the app, execute the following command from the terminal session:
go run main.go
All the files and commands executed henceforth will be within this directory. If you followed the setup process, you should see the following output in your Terminal:
Point your favorite browser to http://localhost:8080
. You should see a "hello world"
message:
Awesome – you have successfully started an HTTP server in Go with the Gin framework.
Back to the terminal, Gin will trace the HTTP requests:
You can use a cURL
command to issue an HTTP request:
curl -X GET http://localhost:8080
Alternatively, you can use an advanced REST client such as Postman. You can download the right version based on your platform from the following URL: https://www.getpostman.com/apps.
Once it has downloaded, run the wizard and open Postman. Set the fields as follows:
- HTTP method:
GET
- URL:
http://localhost:8080
- Headers: Set
Content-Type
toapplication/json
The request should be configured like so:
It's worth mentioning that by default, the HTTP server is listening on port 8080
. However, if the port is being used by another application, you can define a different port by adding an argument to the Run
method:
r.Run(":5000")
This command will run the server on port 5000
, as shown in the following screenshot:
Note that the port
parameter needs to be passed as a string, prepended by colon punctuation.
You should now be familiar with the basics of building and running a simple web application. In the next few sections, we will cover how to enhance those functionalities with third-party packages. But before we do that, let's cover how to manage Go dependencies.