给vscode添加swagger注释代码模板(snippets)

781 阅读1分钟

在vscode中写swagger注释是一个比较繁琐的事情,大部分都是重复的工作。给vscode添加代码模板(snippets)是大大提升我们写swagger的效率。

实现方法

点击首选项->配置用户代码片段

image.png

在弹出的窗口搜索go,并选择go.json

image.png

在go.json中添加如下代码

{
	"Add Swagger API Annotation": {
		"prefix": "@api",
		"body": [
			"// @Summary $1",
			"// @Schemes",
			"// @Description $2",
			"// @Tags $3",
			"// @Accept json",
			"// @Produce json",
			"// @Param book body models.User  true  \"JSON Data\""
			"// @Success 200 {array} models.User",
			"// @Router $4 [get] ",
		],
		"description": "Add Swagger API Annotation"
	}
}

在go代码中,输入@api,按下tab键,即可生成对应的注释代码

// @Summary 
// @Schemes
// @Description 
// @Tags 
// @Accept json
// @Produce json
// @Param book body models.User  true  "JSON Data"
// @Success 200 {array} models.User
// @Router  [get] 
func AddHospital(ctx *gin.Context) {
}

你可以根据你自己的需求修改go.json中代码。