第一篇:开发环境服务启动

github地址
https://github.com/gin-gonic/gin
 
分几步
1、随便创建一个文件夹(不要有中文、空格和奇怪的字符串,没有为什么)

2、在 api.jtthink.com下创建topic ,代表 话题相关API



3、用goland 打开topic目录

 

在golang中执行命令

4、cd进入topic执行: go mod init topic.jtthink.com

5、在当前目录下执行

   go get  github.com/gin-gonic/gin

 

测试代码:

package main

import (
	"github.com/gin-gonic/gin"
)
func main() {
	router:=gin.Default()
	router.GET("/", func(context *gin.Context) {
		context.Writer.Write([]byte("hello gin"))
	})
	router.Run()//如果不指定端口 默认是8080
}

 浏览器请求:

 

 返回Json数据代码1

package main

import (
	"github.com/gin-gonic/gin"
	"net/http"
)
func main() {
	m:=make(map[string]interface{})
	m["username"]="pizixu"
	router:=gin.Default()
	router.GET("/", func(context *gin.Context) {
		context.JSON(http.StatusOK,m)
	})
	router.Run()//如果不指定端口 默认是8080
}

 

浏览器访问:

返回Json数据代码2(使用内置方法gin.H)

package main

import (
	"github.com/gin-gonic/gin"
	"net/http"
)
func main() {
/*	m:=make(map[string]interface{})
	m["username"]="pizixu"*/
	router:=gin.Default()
	router.GET("/", func(context *gin.Context) {
		context.JSON(http.StatusOK,gin.H{
			"username":"pizixu",
		})
	})
	router.Run()//如果不指定端口 默认是8080
}

 

浏览器访问:

 

 

 

 

 

 

 

posted @ 2020-06-19 16:57  痞子胥  阅读(209)  评论(0)    收藏  举报