Gin Web FrameworkGo 的 HTTP Web 框架

联合创作 · 2023-09-20 10:18

Gin 是一个用 Go 语言开发的 Web 框架,提供类 Martini 的 API,但是性能更好。因为有了 httprouter 性能提升了 40 倍之多。

$ cat test.go
package main

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

func main() {
    router := gin.Default()
    router.GET("/", func(c *gin.Context) {
        c.String(http.StatusOK, "hello world")
    })
    router.GET("/ping", func(c *gin.Context) {
        c.String(http.StatusOK, "pong")
    })
    router.POST("/submit", func(c *gin.Context) {
        c.String(http.StatusUnauthorized, "not authorized")
    })
    router.PUT("/error", func(c *gin.Context) {
        c.String(http.StatusInternalServerError, "an error happened :(")
    })
    router.Run(":8080")
}
浏览 11
点赞
评论
收藏
分享

手机扫一扫分享

编辑
举报
评论
图片
表情
推荐
点赞
评论
收藏
分享

手机扫一扫分享

编辑
举报