本篇概要:
1 开发环境、最简单的服务启动;
github 地址:https://github.com/gin-gonic/gin
# 创建模块文件夹
mkdir -p /Users/hualaoshuan/go/go-gin/api.gin.test.com/topic
cd /Users/hualaoshuan/go/go-gin/api.gin.test.com/topic
# 创建 go.mod
go mod init topic.gin.test.com
# 安装 gin,在当前目录执行
go get github.com/gin-gonic/gin
- 文件
/Users/hualaoshuan/go/go-gin/api.gin.test.com/topic/main.go
package main
import (
"github.com/gin-gonic/gin"
"net/http"
)
type Topic struct {
TopicID int
TopicTitle string
}
func main() {
// 赋值
//m := make(map[string]interface{})
//m["username"] = "huahua"
// 创建路由
router := gin.Default()
router.GET("/", func(context *gin.Context) {
// context.Writer.Write([]byte("hello"))
// context.JSON(http.StatusOK, m)
context.JSON(http.StatusOK, Topic{101, "话题"})
})
router.Run() // 默认 8080
}