0
点赞
收藏
分享

微信扫一扫

Gin 基础1

分湖芝蘭 2022-05-06 阅读 58

本篇概要:

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
}
举报

相关推荐

0 条评论