gin environment construction:
If the environment variable of go is configured, you can open the terminal to install our gin
go get github.com/gin-gonic/gin
An error is reported when turning to gin:
package command-line-arguments imports github.com/gin-gonic/gin imports github.com/gin-gonic/gin/binding imports github.com/go-playground/validator/v10 imports github.com/leodido/go-urn: no Go files in C:\Users\莫迪\go\src\github.com\leodido\go-urn
direct execution
go mod init gin
go mod edit -require github.com/gin-gonic/gin@latest
go mod vendor
Then it will report an error:
go: updates to go.mod needed; to update it: go mod tidy
Only the modules that the mod depends on need to be updated
go mod tidy
It can be run successfully:
Run a helloworld:
package main
import "github.com/gin-gonic/gin"
func main() {
//创建一个路由引擎
r := gin.Default()
//配置路由 可以进行配置多个路由
r.GET("/", func(c *gin.Context) {
/**
有两个参数 第一个参数表示返回状态码 第二个参数表示返回的data数据
*/
c.String(200, "zhi:%v", "beijing")
})
r.GET("/news", func(context *gin.Context) {
context.String(200, "news")
})
r.POST("/add", func(context *gin.Context) {
context.String(200, "这是一个用于添加数据的接口")
})
r.PUT("/edit", func(context *gin.Context) {
context.String(200, "这是一个用于修改数据的接口")
})
r.DELETE("/delete", func(context *gin.Context) {
context.String(200, "这是一个用于删除数据的请求")
})
//启动一个web服务
//r.Run() // 默认指定的端口号为8080
//可以进行指定端口进行 启动一个文本服务
r.Run(":8888")
}