Gin-0100 请求-响应

2022-7-27 Jeen

关于Gin框架的使用简介如下:

- 安装go开发环境

- 安装Gin依赖包

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

详细介绍及安装过程请参考官方文档 https://gin-gonic.com/zh-cn/docs/ 


创建一个目录用于开发  

$ mkdir -p /path/to/ginTest 

$ cd /path/to/ginTest


$ vi gin0100.go



package main

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

func main() {
	r := gin.Default()
	r.GET("/", func(c *gin.Context) {
		c.JSON(200, gin.H{
			"msg": "welcome to gin",
		})
	})
	r.Run("gin.local.com:8989") // 监听并在 gin.local.com:8989 上启动服务
}
保存文件后尝试启动服务


$ go run gin0100.go

提示错误 

gin0100.go:4:2: no required module provides package github.com/gin-gonic/gin: go.mod file not found in current directory or any parent directory; see 'go help modules'

参考提示,看看能不能解决

$ go help modules

... 

For a series of tutorials on modules, see
https://golang.org/doc/tutorial/create-module.

...

访问查阅对应的文档

... 以下内容截取自网页

For more on naming your module with a module path, see Managing dependencies.

$ go mod init example.com/greetings
go: creating new go.mod: module example.com/greetings

...截取End

或查阅官网的起步文档  https://go.dev/doc/tutorial/getting-started

大意就是需要解决模块的依赖问题,创建一个自有模块,以引入其他模块

初始化自有模块

$ go mod init local.com/ginTest 

安装模块依赖

$ go mod tidy

重试服务启动

$ go run gin0100.go

启动成功

访问 gin.local.com:8989 

响应成功 ~

 



GoLang GoLang Gin

(0) (356)

发表评论: