基础格式样例代码
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
}