Github每日精选(第77期):Go (Golang) 编写的 HTTP Web 框架gin

Gin

Gin 是一个用 Go (Golang) 编写的 Web 框架。由于httprouter,它具有类似 martini 的 API,性能提高了 40 倍。如果您需要性能和良好的生产力,您一定会喜欢 Gin。

在这里插入图片描述

github的地址在这里

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

go install github.com/gin-gonic/gin@latest

在代码的目录下执行:

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

在您的代码中导入它:

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

导入net/http. 例如,如果使用常量,例如http.StatusOK.

import "net/http"

以下是快速开始的全部代码:

package main

import (
  "net/http"

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

func main() {
  r := gin.Default()
  r.GET("/ping", func(c *gin.Context) {
    c.JSON(http.StatusOK, gin.H{
      "message": "pong",
    })
  })
  r.Run() // listen and serve on 0.0.0.0:8080 (for windows "localhost:8080")
}

执行完以后:

[GIN-debug] GET    /ping                     --> main.main.func1 (3 handlers)
[GIN-debug] [WARNING] You trusted all proxies, this is NOT safe. We recommend you to set a value.
Please check https://pkg.go.dev/github.com/gin-gonic/gin#readme-don-t-trust-all-proxies for details.
[GIN-debug] Environment variable PORT is undefined. Using port :8080 by default
[GIN-debug] Listening and serving HTTP on :8080

在这里插入图片描述

在浏览器中,访问链接,出现如下的代码。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

go2coding

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值