golang gin第一个例子

gin框架的github地址

https://github.com/gin-gonic/gin

按照github上的文档,执行

go get -u github.com/gin-gonic/gin

不能成功。

还好有镜像可用,执行以下命令

go env -w GO111MODULE=on
go env -w GOPROXY=https://mirrors.aliyun.com/goproxy/,direct

切换到阿里云镜像

再执行

go get -u github.com/gin-gonic/gin

成功。

这时候新建一个文件夹MyExample,进入到该目录,执行

go mod init MyExample

执行成功后会生成一个go.mod文件

新建一个文件,比如叫MyExample.go

把以下代码拷贝进去

package main

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 (for windows "localhost:8080")
}

直接运行,然后再浏览器输入地址

http://127.0.0.1:8080/ping

浏览器显示如下,

{"message":"pong"}

测试成功。

参考链接:

https://github.com/gin-gonic/gin

https://www.sunzhongwei.com/problem-of-domestic-go-get-unable-to-download?from=sidebar_new

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章