codecamp

gfast 后端开发-接口文档

接口文档使用​swagger​,只要你在写代码的时候,稍加几个注解,文档自动生成。

使用方法

使用前提:先安装​swag

 ​go install github.com/swaggo/swag/cmd/swag​ 会在​gopath​的​bin​目录下生成​swag​可执行文件,确保​bin​目录已设置环境变量可全局执行。

1、入口文件设置swagger信息

package main

import (
    _ "gfast/boot"
    _ "gfast/router"
    "github.com/gogf/gf/frame/g"
)

// @title gfast
// @version 2.0
// @description gfast 在线API文档
// @host localhost:8200
// @BasePath /
func main() {

}

2、在控制层Controller的方法上添加注解来描述接口信息如:

// @Summary 信息列表
// @Description 信息列表
// @Tags 文章管理
// @Param data body cms_news.ReqListSearchParams true "data"
// @Success 0 {object} library.Response "{"code": 200, "data": [...]}"
// @Router /system/cms/news/list [get]
// @Security
func (c *CmsNews) List(r *ghttp.Request) {
    ...

3、生成更新api接口文档

在 ​./​ 项目根目录下(和main.go文件同级目录下)执行:​gf swagger --pack​ 生成或更新api文档。

执行后在项目根目录会自动生成swagger文件夹,其中保存了接口文档数据​swagger.json

m_8684dc7501a11186f984567d6fa83a17_r

swaggo 详细文档地址请查阅:https://github.com/swaggo/swag

goframe gf工具使用: https://goframe.org/pages/viewpage.action?pageId=1115794


gfast 后端开发-代码生成
gfast 插件-微信公众号管理
温馨提示
下载编程狮App,免费阅读超1000+编程语言教程
取消
确定
目录

关闭

MIP.setData({ 'pageTheme' : getCookie('pageTheme') || {'day':true, 'night':false}, 'pageFontSize' : getCookie('pageFontSize') || 20 }); MIP.watch('pageTheme', function(newValue){ setCookie('pageTheme', JSON.stringify(newValue)) }); MIP.watch('pageFontSize', function(newValue){ setCookie('pageFontSize', newValue) }); function setCookie(name, value){ var days = 1; var exp = new Date(); exp.setTime(exp.getTime() + days*24*60*60*1000); document.cookie = name + '=' + value + ';expires=' + exp.toUTCString(); } function getCookie(name){ var reg = new RegExp('(^| )' + name + '=([^;]*)(;|$)'); return document.cookie.match(reg) ? JSON.parse(document.cookie.match(reg)[2]) : null; }