Iris 框架安装步骤
前言
今天准备开始使用 Go 开发,用 iris 框架,记录安装过程。
环境
准备工作
- 配置host
# 感觉有点玄学,不知道有没有起到实际作用
192.30.253.113 github.com
151.101.185.194 github.global.ssl.fastly.net
安装步骤
- 在
$GOPATH
目录下执行go get -v github.com/kataras/iris
下载时间可能有点久 - 创建目录
$GOPATH\src\golang.org\x
,在该目录下执行
git clone git@github.com:golang/net.git
git clone git@github.com:golang/crypto.git
git clone git@github.com:golang/text.git
我安装完成之后是这样的,有几个是之前安装
测试
在 $GOPATH
目录下新建 iris
文件夹 ,新建 server.go
,内容如下
package main
import (
"github.com/kataras/iris"
"github.com/kataras/iris/middleware/logger"
"github.com/kataras/iris/middleware/recover"
)
func main() {
app := iris.New()
app.Use(recover.New())
app.Use(logger.New())
//输出html
// 请求方式: GET
// 访问地址: http://localhost:8080/welcome
app.Handle("GET", "/welcome", func(ctx iris.Context) {
ctx.HTML("<h1>Welcome</h1>")
})
//输出字符串
// 类似于 app.Handle("GET", "/ping", [...])
// 请求方式: GET
// 请求地址: http://localhost:8080/ping
app.Get("/ping", func(ctx iris.Context) {
ctx.WriteString("pong")
})
//输出json
// 请求方式: GET
// 请求地址: http://localhost:8080/hello
app.Get("/hello", func(ctx iris.Context) {
ctx.JSON(iris.Map{"message": "Hello Iris!"})
})
app.Run(iris.Addr(":8080")) //8080 监听端口
}
执行 go run server.go
$ go run server.go
Now listening on: http://0.0.0.0:8080
Application started. Press CTRL+C to shut down.
在浏览器中访问
http://localhost:8080/welcome
http://localhost:8080/hello
http://localhost:8080/ping
如下图
终端输出
[INFO] 2019/10/28 19:17 200 0s ::1 GET /welcome
[INFO] 2019/10/28 19:17 200 0s ::1 GET /hello
[INFO] 2019/10/28 19:17 200 0s ::1 GET /ping
如果有什么错误的地方,希望大家能指出,一起学习进步。
本作品采用《CC 协议》,转载必须注明作者和本文链接
推荐文章: