Gin框架

Gin框架学习

gin文档

github:https://github.com/gin-gonic/gin

官网文档:https://gin-gonic.com/zh-cn/docs/quickstart/

中文文档:https://learnku.com/docs/gin-gonic/1.7

Gin项目:https://github.com/go-admin-team/go-admin

docker 部署Gin :https://github.com/leosanqing/go_foodie_shop/blob/master/doc/Docker%E9%83%A8%E7%BD%B2%E9%A1%B9%E7%9B%AE.md

 

Gin 是什么?

Gin 是一个用 Go (Golang) 编写的 HTTP web 框架。 它是一个类似于 martini 但拥有更好性能的 API 框架,由于 httprouter,速度提高了近 40 倍。如果你需要极好的性能,使用 Gin 吧。

———————————————— 原文作者:Go Gin 1.7 转自链接:https://learnku.com/docs/gin-gonic/1.7/go-gin-document/11352

快速入门

要求

  • Go 1.13 及以上版本

安装

要安装 Gin 软件包,需要先安装 Go 并设置 Go 工作区。

1.下载并安装 gin:

$ go get -u github.com/gin-gonic/gin

2.将 gin 引入到代码中:

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

3.(可选)如果使用诸如 http.StatusOK 之类的常量,则需要引入 net/http 包:

import "net/http"
  1. 创建你的项目文件夹并 cd 进去

$ mkdir -p $GOPATH/src/github.com/myusername/project && cd "$_"
  1. 拷贝一个初始模板到你的项目里

$ curl https://raw.githubusercontent.com/gin-gonic/examples/master/basic/main.go > main.go
  1. 运行你的项目

$ go run main.go

开始

不确定如何编写和执行 Go 代码? 点击这里.

首先,创建一个名为 example.go 的文件

$ touch example.go

接下来, 将如下的代码写入 example.go 中:

package main

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

func main() {
r := gin.Default()
r.GET("/ping", func(c *gin.Context) {
c.JSON(200, gin.H{
"message": "pong",
})
})
r.Run() // 监听并在 0.0.0.0:8080 上启动服务
}

然后, 执行 go run example.go 命令来运行代码:

# 运行 example.go 并且在浏览器中访问 HOST_IP:8080/ping
$ go run example.go
posted @ 2023-02-03 17:01  游走De提莫  阅读(93)  评论(0编辑  收藏  举报