Create a GO project called myGin

. Go to the mod file

The module myGin go 1.16Copy the code

Install the gin

go get -u github.com/gin-gonic/gin
Copy the code

Create a main.go file

package main

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

func main(a) {
	r := gin.Default()

	r.Run()
}

Copy the code

Create the Routes folder and controller

The project structure is

Create an Index controller under Controller

package controller

//controller/Index.go

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

func Index(context *gin.Context) {

	context.JSON(200, gin.H{"msg": "hello world"})}Copy the code

Create routes.go in the Routes folder to load routes for the entire project

package routes

//routes/routes.go

import (
	"github.com/gin-gonic/gin"
	"myGin/controller"
)

func Load(r *gin.Engine) {

	r.GET("/", controller.Index)

}

Copy the code

The load route function is called in main.go

package main

import (
	"github.com/gin-gonic/gin"
	"myGin/routes"
)

func main(a) {
	r := gin.Default()

	routes.Load(r)

	r.Run()
}

Copy the code

Run your browser and visit http://127.0.0.1:8080


package controller

//controller/Index.go

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

func Index(context *gin.Context) {

	if 1+1= =2 {

		context.JSON(200, gin.H{"msg": "hello world"})
	}

	context.JSON(200, gin.H{"msg": "hello world"})}Copy the code

Browser access

There are two results. The correct way to write it is

package controller

//controller/Index.go

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

func Index(context *gin.Context) {

	if 1+1= =2 {

		context.JSON(200, gin.H{"msg": "hello world"})

		return
	}

	context.JSON(200, gin.H{"msg": "hello world"})}Copy the code

This problem occurs when a statement forgets to return, and it is inevitable in real development

Gin’s controller lacks a return value (response) compared to traditional Web frameworks

Now modify your controller to support return values

Create a new Response folder under your project and create a Response.go file

package response

//response/response.go

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

type Response struct {
	data interface{}}func Resp(a) *Response {

	return &Response{}
}

func (r *Response) Json(data gin.H) *Response {

	r.data = data

	return r
}

func (r *Response) String(data string) *Response {

	r.data = data

	return r
}

func (r *Response) Byte(data []byte) *Response {

	r.data = data

	return r
}

func (r *Response) GetData(a) interface{} {

	return r.data
}

Copy the code

Modifying the INDEX Controller

package controller

//controller/Index.go

import (
	"github.com/gin-gonic/gin"
	"myGin/response"
)

func Index(context *gin.Context) *response.Response {

	if 1+1= =2 {

		return response.Resp().Json(gin.H{"msg": "hello world"})}return response.Resp().Json(gin.H{"msg": "hello world"})}Copy the code

Back to the route, the route handler needs to return gin.HandlerFunc, write a function to convert it

package routes

//routes/routes.go

import (
	"github.com/gin-gonic/gin"
	"myGin/controller"
	"myGin/response"
)

func Load(r *gin.Engine) {

	r.GET("/", convert(controller.Index))

}

func convert(f func(*gin.Context) *response.Response) gin.HandlerFunc {

	return func(c *gin.Context) {

		resp := f(c)

		data := resp.GetData()

		switch item := data.(type) {

		case string:

			c.String(200, item)

		case gin.H:

			c.JSON(200, item)

		}

	}

}

Copy the code

Accessing the browser

complete