[Golang] Getting started with Gin

2022-04-11 hit count image

Let's see how to develop a web server with Golang using Gin web framework that is the mos popular web framework of Golang.

Outline

In this blog post, I will introduce how to develop a web server with Golang. There are many web frameworks in Golang, but I will show you how to use Gin web framework which is the most popular web framework of Golang for the development of web server.

You can see the full source code of the blog post at the following link.

Blog series

This blog post is made in the series about how to use Gin web framework in Golang. If you want to see the other usages of Gin web framework, please check out the following blog posts.

  • [Golang] Getting started with Gin

Create Go project

To check how to use Gin web framework, execute the following command to prepare the Go project by the Go module.

mkdir start
cd start
go mod init github.com/dev-yakuza/study-golang/gin/start

After then, you can see the ./go.mod file is created like the below.

module github.com/dev-yakuza/study-golang/gin/start

go 1.17

Install Gin web framework

To use Gin web framework, execute the following command to install the Gin web framework.

go get -u github.com/gin-gonic/gin

After then, you can see the contents about Gin web framework are added to the ./go.mod file like the below.

module github.com/dev-yakuza/study-golang/gin/start

go 1.17

require (
  github.com/gin-contrib/sse v0.1.0 // indirect
  github.com/gin-gonic/gin v1.7.7 // indirect
  github.com/go-playground/locales v0.14.0 // indirect
  github.com/go-playground/universal-translator v0.18.0 // indirect
  github.com/go-playground/validator/v10 v10.10.1 // indirect
  github.com/golang/protobuf v1.5.2 // indirect
  github.com/json-iterator/go v1.1.12 // indirect
  github.com/leodido/go-urn v1.2.1 // indirect
  github.com/mattn/go-isatty v0.0.14 // indirect
  github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
  github.com/modern-go/reflect2 v1.0.2 // indirect
  github.com/ugorji/go/codec v1.2.7 // indirect
  golang.org/x/crypto v0.0.0-20220331220935-ae2d96664a29 // indirect
  golang.org/x/sys v0.0.0-20220406163625-3f8b81556e12 // indirect
  golang.org/x/text v0.3.7 // indirect
  google.golang.org/protobuf v1.28.0 // indirect
  gopkg.in/yaml.v2 v2.4.0 // indirect
)

Also, you can see the ./go.sum file is created.

How to use Gin

Next, let’s see how to use Gin web framework to make a web server. Create the ./main.go file and modify it like the below.

package main

import "github.com/gin-gonic/gin"

func setupRouter() *gin.Engine {
  r := gin.Default()
  r.GET("/ping", func(c *gin.Context) {
    c.String(200, "pong")
  })
  return r
}

func main() {
  r := setupRouter()
  r.Run(":8080")
}

Let’s see the code one by one.

func setupRouter() *gin.Engine {
  r := gin.Default()
  r.GET("/ping", func(c *gin.Context) {
    c.String(http.StatusOK, "pong")
  })
  return r
}

You can make a web server by gin.Default() provided by Gin web framework. And, you can bind the specific URL(/ping) to the function which does something you want. In here, I just return the string pong with StatusOK(200) status code.

func main() {
  r := setupRouter()
  r.Run(":8080")
}

After you define the functions for the specific URLs, you can run the web server by the run function with the 8080 port.

Next, execute the following command to run the web server.

go run ./main.go

And then, when you open the web browser with the http://localhost:8080/ping URL, you can see the result like the below.

pong

Completed

Done! we’ve seen how to use Gin web framework to make a web server in Golang. From now on, I will post about how to use Gin web framework more and more.

Was my blog helpful? Please leave a comment at the bottom. it will be a great help to me!

Posts