go语言gin框架入门学习

基础格式样例代码

	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() // listen and serve on 0.0.0.0:8080
	}

r := gin.Default() 设置默认路由
默认开启8080,如果需要开启其他的,可以设置r.Run(9090)

输入 http://localhost:8080/ping
即可看到输出内容 {“message”:“pong”}

参考链接:https://www.cnblogs.com/zzg521/p/10417120.html

实例代码

			package main
		import "github.com/gin-gonic/gin"
		
		type Topic struct {
			TopicId int
			TopicTitle string
		}
		func main() {
			r := gin.Default()
			r.GET("/ping", func(c *gin.Context) {
				/*c.JSON(200, gin.H{
					"message": "pong",
				})
				c.JSON(200,&Topic{101,"这是你"})
				*/
		
				m:=make(map[string]string)
				m["topicid"]="102"
				m["topictitle"]="title"
				c.JSON(200,m)
			})
			r.Run() // listen and serve on 0.0.0.0:8080
		}
发布了39 篇原创文章 · 获赞 8 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/weixin_42123182/article/details/89337252