ZVVQ代理分享网

初学者理解 Go 框架(go流行框架)

作者:zvvq博客网
导读对于 go 初学者来说,掌握框架至关重要。以下是最流行的 go 框架:gin:轻量级、高性能 http 框架,以其简洁的 api 而著称。echo:轻量级 http 框架,提供中间件支持和可定制的日志记录

针对 go 新手来说,把握架构尤为重要。以下属于最流行 go 架构:gin:轻量、性能卓越 http 架构,因其简约的 api 而着称。echo:轻量 http 架构,给予消息中间件大力支持和支持定制的日志记录装置。gorm:对象关系映射器(orm),优化了数据库交互。beego:全栈式 web 架构,给予 orm、路由器和js框架等服务。buffalo:全栈式架构,致力于应用程序安全性、系统测试生产准备就绪性。

新手了解 Go 架构

序言

Go 架构带来了分层架构、内嵌消息中间件和常用工具,优化了 Go 应用程序的研发流程。对于 Go 新手来说,掌握这些架构尤为重要。下面我们就分析 Go 中最流行架构,同时提供实战案例 来证明其应用。

1. Gin 架构

Gin 是一款轻量、高性能 HTTP 架构。它因其简约的 API 良好的文本文档而著称。

实战案例 :建立一个简单的API

packagemain

import(

"github.com/gin-gonic/gin"

"net/http"

)

funcmain(){

r:=gin.Default()

r.GET("/ping",func(cgin.Context){

c.JSON(http.StatusOK,gin.H{

"message":"pong",

})

})

r.Run()

}

2. Echo 架构

Echo 是一款类似 Gin 的轻量 HTTP 架构。它提供了消息中间件适用、链条式要求处理过程,以及一个支持定制的日志记录装置。

实战案例 :应用消息中间件

packagemain

import(

"github.com/labstack/echo/v4"

"github.com/labstack/echo/v4/middleware"

)

funcmain(){

e:=echo.New()

//开启 CORS 跨域请求

e.Use(middleware.CORS())

e.GET("/ping", func(c echo.Context) error {

return c.String(http.StatusOK, "pong")

})

e.Logger.Fatal(e.Start(":1323"))

}

3. GORM

GORM 是一个对象关系映射器(ORM),它优化了 Go 里的数据库交互。它支持各种数据库系统,并提供了一系列强大的功能。

实战案例 :传送到数据库系统

packagemain

import(

"fmt"

"gorm.io/driver/mysql"

"gorm.io/gorm"

)

funcmain(){

dsn:="user:password@tcp(127.0.0.1:3306)/database"

db,err:=gorm.Open(mysql.Open(dsn),&gorm.Config{})

iferr!=nil{

panic(err)

}

fmt.Println("Successfullyconnectedtothedatabase.")

}

4. Beego 架构

Beego 是一款全栈式 Web 架构,提供了一系列开箱即用的功效,包含 ORM、路由器、js框架等。

实战案例 :建立 CRUD 运用

packagemain

import(

"beego.me/beego/v2/core/logs"

"beego.me/beego/v2/server/web"

"fmt"

)

typeUserstruct{

Idint

Namestring

}

varusers[]User

funcmain(){

web.Router("/user/create",&controller{},"post:Create")

web.Router("/user/update",&controller{},"put:Update")

web.Router("/user/delete",&controller{},"delete:Delete")

web.Router("/user/:id",&controller{},"get:Get")

web.Run()

}

typecontrollerstruct{

web.Controller

}

//Createauser

func(ccontroller)Create(){

user:=User{}

iferr:=c.ParseForm(&user);err!=nil{

logs.Error(err)

c.Ctx.WriteString(err.Error())

return

}

users=append(users,user)

c.Ctx.WriteString(fmt.Sprintf("Usercreatedsuccessfully:%+v",user))

}

5. Buffalo 架构

Buffalo 是一款致力于应用程序安全性、系统测试生产准备就绪的全栈式架构。

实战案例 :建立RESTAPI

packagemain

import(

"github.com/gobuffalo/buffalo"

"github.com/gobuffalo/buffalo/binding"

"fmt"

)

typeUserstruct{

IDint

Namestring

Emailstring

}

funcmain(){

app:=buffalo.New(buffalo.Options{})

app.GET("/users",func(cbuffalo.Context)error{

//Retrieveallusersfromthedatabase

returnc.Render(http.StatusOK,r.String("HelloWorld"))

})

app.POST("/users",func(cbuffalo.Context)error{

user:=&User{}

iferr:=binding.Bind(c.Request(),user);err!=nil{

returnerr

}

//Createtheuserinthedatabase

returnc.Render(http.StatusCreated,r.String("Usercreatedsuccessfully"))

})

app.PUT("/users/{id}",func(cbuffalo.Context)error{

user:=&User{}

iferr:=binding.Bind(c.Request(),user);err!=nil{

returnerr

}

//Updatetheuserinthedatabase

returnc.Render(http.StatusOK,r.String("Userupdatedsuccessfully"))

})

app.DELETE("/users/{id}",func(cbuffalo.Context)error{

//Deletetheuserfromthedatabase

returnc.Render(http.StatusOK,r.String("Userdeletedsuccessfully"))

})

app.Serve()

}

把握 Go 架构能够显着提高研发效率和应用软件品质。通过分析这种架构的原理和实战案例 ,Go 新手能够快速入门并构建健硕、可扩展性应用程序。

以上就是关于新手了解 Go 架构的详细内容,大量欢迎关注站其他类似文章!