【Golang提效】Goland 配置 Controller 模板

【Golang提效】Goland 配置 Controller 模板

最近用golang来写一些自己调用的 web api,使用mvc模式,发现每次创建 controller 文件都要去别的文件里复制挺麻烦的。

于是,使用 goland 的 live template 来做一个 关键词 + tab 自动生成 controller 通用模板。

准备模板🐣

先准备一个项目基本的 controller文件模板

我自己的命名规则是文件名 + Controller ,可以根据情况修改

$FILE_NAME$ 为创建的文件名

下面代码包括 声明控制器、gin路由以及一个方法示例:

golang 复制代码
package v1

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

type ConceptController struct {
}

func (vm *ConceptController) Router(engine *gin.RouterGroup) {
	engine.GET("/Concept/test", vm.Example)
}

func (vm *ConceptController) Example(c *gin.Context) {
	c.JSON(http.StatusOK, gin.H{})
}

配置Live template🦅

  1. 在 setting / editor / live template 中 找到 Go 新建一个缩略词 controller (可按照自己风格修改)

  2. 填写 Abbreviation 和描述

  3. 将上面写好的代码复制到tempalate text

  4. 点击右下角Edit Variables 配置 $FILE_NAME$ 变量: fileNameWithoutExtension() 获取文件名,不包括扩展名,

  5. 点击应用

效果展示🐥🦅

模板内容按照自己实际项目进行修改 🐒

萌新码农,请大佬们多多关照 🌻

相关推荐
IT_陈寒10 分钟前
Python的GIL让我多写了500行代码
前端·人工智能·后端
IT爱学堂1 小时前
精讲软件设计师(软考中级)一站式通关课_实战课程_慕课网
后端
战天战地站房顶1 小时前
在 Windows Docker 中部署 PostgreSQL 17 + pgvector 完整指南
后端
卷无止境2 小时前
FastAPI Guard 全解析,从概念到工程落地的实战指南
后端·python
卷无止境2 小时前
FastAPI Users 全面解析:概念、原理与工程实战
后端·python
程序员爱钓鱼2 小时前
Go switch 详解
后端·面试·go
程序员爱钓鱼2 小时前
Rust Struct结构体详解:定义自己的复杂数据类型
后端·面试·rust
风流 少年2 小时前
Spring AI 2.0:MCP
java·后端·spring
北斗落凡尘2 小时前
LangGraph 入门实战(7)
后端·langchain
uzong3 小时前
业务新老系统数据迁移-负责人经验总结和复盘
后端