Go语言开发项目文件规范

1.controllers

存放控制器结构体

  1. 接收请求:从 HTTP 请求中解析数据(如路径参数、查询参数、请求体等)。

  2. 调用业务逻辑:与服务层(Service Layer)或模型层(Model Layer)交互,处理业务逻辑。

  3. 返回响应:根据处理结果返回 HTTP 响应(如 JSON、HTML 页面等)。

代码示例:

package admin

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

type ArticleController struct {
}

func (con ArticleController) Index(c *gin.Context) {
	c.String(200, "文章列表--")
}
func (con ArticleController) Add(c *gin.Context) {
	c.String(200, "-add--文章-")
}
func (con ArticleController) Edit(c *gin.Context) {
	c.String(200, "-Edit---文章")
}

 2.middlewares

中间键层,存放中间键

代码示例:

package middlewares

import (
	"fmt"
	"time"

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

func InitMiddleware(c *gin.Context) {
	//判断用户是否登录

	fmt.Println(time.Now())

	fmt.Println(c.Request.URL) 

	c.Set("username", "张三")
	cCp := c.Copy()
	//定义一个goroutine统计日志
	go func() {
		time.Sleep(2 * time.Second)
		fmt.Println("Done! in path " + cCp.Request.URL.Path)
	}()
}

3.models

存放公共方法

代码示例:

package models

import (
	"time"
)

// 时间戳转换成日期
func UnixToTime(timestamp int) string {
	t := time.Unix(int64(timestamp), 0)
	return t.Format("2006-01-02 15:04:05")
}

// 日期转换成时间戳 2020-05-02 15:04:05
func DateToUnix(str string) int64 {
	template := "2006-01-02 15:04:05"
	t, err := time.ParseInLocation(template, str, time.Local)
	if err != nil {
		return 0
	}
	return t.Unix()
}

// 获取当前时间戳
func GetUnix() int64 {
	return time.Now().Unix()
}

// 获取当前日期
func GetDate() string {
	template := "2006-01-02 15:04:05"
	return time.Now().Format(template)
}

// 获取年月日
func GetDay() string {
	template := "20060102"
	return time.Now().Format(template)
}

4.routers

存放路由

代码示例:

package routers

import (
	"gindemo09/controllers/admin"
	"gindemo09/middlewares"

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

func AdminRoutersInit(r *gin.Engine) {
	adminRouters := r.Group("/admin", middlewares.InitMiddleware)
	{
		adminRouters.GET("/", admin.IndexController{}.Index)
		adminRouters.GET("/user", admin.UserController{}.Index)
		adminRouters.GET("/user/add", admin.UserController{}.Add)
		adminRouters.GET("/user/edit", admin.UserController{}.Edit)
		adminRouters.GET("/article", admin.ArticleController{}.Index)
		adminRouters.GET("/article/add", admin.ArticleController{}.Add)
		adminRouters.GET("/article/edit", admin.ArticleController{}.Edit)
	}
}

5.static

css

代码示例:

h1{
    background: #000;
    color: #fff;
    text-align: center;
}
h2{
    color: red;
}

images

图片资源

js

存放静态资源

6.templates

存放页面展示

代码示例:

{
  
  {define "admin/index.html"}}



    
    
    Document


    

这是后台首页

{ {end}}

7.main.go文件

package main

import (
	"fmt"
	"gindemo09/models"
	"gindemo09/routers"
	"text/template"

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

type UserInfo struct {
	Username string `json:"username" form:"username"`
	Password string `json:"password" form:"password"`
}

type Article struct {
	Title   string `json:"title" xml:"title"`
	Content string `json:"content" xml:"content"`
}

func Println(str1 string, str2 string) string {
	fmt.Println(str1, str2)
	return str1 + "---" + str2
}

func main() {
	// 创建一个默认的路由引擎(包含 Logger 和 Recovery 中间件)
	r := gin.Default()
	//自定义模板函数 注意要把这个函数放在加载模板前
	r.SetFuncMap(template.FuncMap{
		"UnixToTime": models.UnixToTime,
		"Println":    Println,
	})
	//加载模板
	r.LoadHTMLGlob("templates/**/*")
	//配置静态web目录  第一个参数表示路由,第二个蚕食表示映射的目录
	r.Static("/static", "./static")

	routers.AdminRoutersInit(r)

	routers.ApiRoutersInit(r)

	routers.DefaultRoutersInit(r)

	r.Run()
}

你可能感兴趣的:(golang,开发语言,后端)