Gin下载安装遇到的坑

  • 下载安装gin
go get -u github.com/gin-gonic/gin
  • 事例

编译运行下边的代码

package main

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

func main() {
	// 创建一个默认的路由引擎
	r := gin.Default()
	// GET:请求方式;/hello:请求的路径
	// 当客户端以GET方法请求/hello路径时,会执行后面的匿名函数
	r.GET("/hello", func(c *gin.Context) {
		// c.JSON:返回JSON格式的数据
		c.JSON(200, gin.H{
			"message": "Hello world!",
		})
	})
	// 启动HTTP服务,默认在0.0.0.0:8080启动服务
	r.Run()
}
  • 报错
cannot find module providing package github.com/gin-gonic/gin: working directory is not part of a module
  • 解决方案
$  go mod init gin 
$  go mod edit -require github.com/gin-gonic/gin@latest
$  go mod tidy 

在编译运行,搞定

posted @ 2021-03-11 11:52  tianzhh_lynn  阅读(1429)  评论(0编辑  收藏  举报