GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — GitHub - kinjew/gin-context-ext

BranchesTags
Open more actions menu

Latest commit

 

History

2 Commits
2 Commits

Folders and files

NameName
Last commit message
Last commit date
.gitignore
.gitignore
 
 
README.md
README.md
 
 
context.go
context.go
 
 
go.mod
go.mod
 
 
go.sum
go.sum
 
 

Repository files navigation

Gin Context扩展包

1.包说明

本包扩展了gin context加入了以下两个函数,用于API请求时统一数据返回格式。

//Success 是对Context扩展方法 接口请求成功时调用
func (c *Context) Success(code int, msg string, data interface{}) {
	c.JSON(200, gin.H{
		"ret":  1,
		"code": code,
		"msg":  msg,
		"data": data,
	})
}

//Error 是对Context扩展方法 接口请求失败时调用
func (c *Context) Error(code int, msg string, data interface{}) {
	c.JSON(200, gin.H{
		"ret":  0,
		"code": code,
		"msg":  msg,
		"data": data,
	})
}

2.使用方法

1.在基于gin项目的router包中引入本包

import (
	ctxExt "git.sprucetec.com/golang-pkg/gin-context-ext"
	"github.com/gin-gonic/gin"
)

可以根据需求使用ctxExt作为的context包的别名,防止包名冲突。

2.在路由方法中使用扩展包handle方法调用控制器方法。

r.GET("/people/:id", ctxExt.Handle(peopleModule.GetPerson))

3.在控制器中相应处理方法中接受c *ctxExt.Context扩展后的context为参数,然后调用扩展的方法。

/*
获取人员
*/
func GetPerson(c *ctxExt.Context) {
	id := c.Params.ByName("id")
	fmt.Println(id)
	if person, err := peopleMod.GetPerson(id); err != nil {
		c.Error(1008, "无此记录!", "")
	} else {
		c.Success(200, "succ", person)
	}
}

About

No description, website, or topics provided.

Resources

Readme
Activity

Stars

0 stars

Watchers

1 watching

Forks

0 forks
Report repository

Releases

Packages

Contributors

Languages

You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.