安装 Gin
这里我们使用 vendor 工具安装、比如 Govendor
go get
govendor
go get github.com/kardianos/govendor
- 创建项目目录并使用
cd
进入
mkdir -p $GOPATH/src/github.com/yourusername/project && cd "$_"
- 初始化项目 vendor 并添加 gin
govendor init
govendor fetch github.com/gin-gonic/gin@v1.3
Hello World
在项目文件夹下建立 main.go
,这是我们程序的主入口。
package main
import (
"github.com/gin-gonic/gin"
"net/http"
)
func main() {
r := gin.Default()
r.GET("/", func(c *gin.Context) {
c.String(http.StatusOK, "Hello World!")
})
r.Run(":9527")
}
运行 go run main.go
并在浏览器上访问 http://127.0.0.1:9527/
或者 http://localhost:9527/
即可看到浏览器输出 Hello World
备注
从 github
克隆 https://github.com/iliuxu/learn-go-gin
该仓库,并运行git checkout 01A
即可检出对应代码。
0 评论
评论已关闭