与其它框架比较
基础
index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>test</title>
</head>
<body>
<h1>{{ message }}</h1>
</body>
</html>
main.go
package main
import (
"gopkg.in/kataras/iris.v6"
"gopkg.in/kataras/iris.v6/adaptors/httprouter"
"gopkg.in/kataras/iris.v6/adaptors/view"
"time"
"fmt"
"io"
)
type User struct {
Name string
}
func main() {
app := iris.New()
app.Adapt(httprouter.New())
app.Adapt(iris.DevLogger())
//app.Adapt(view.HTML("template","html"))
app.Adapt(view.Django("template","html"))
timeWaitForCloseStream := 4 * time.Second
// html
app.Get("/html", func(c *iris.Context) {
//读取 json
var user User
c.ReadJSON(&user)
//读取 url参数
okmsg := c.URLParam("ok")
c.Render("index.html",map[string]interface{}{
"message": okmsg,
})
})
app.StaticWeb("/static","template")
app.Get("/getFile", func(c *iris.Context) {
file := "template/index.html"
c.SendFile(file,"index.html") //直接跳出下载
})
app.Get("/writer", func(ctx *iris.Context) {
i := 0
// goroutine in order to no block and just wait,
// goroutine is OPTIONAL and not a very good option but it depends on the needs
// Look the streaming_simple_2 for an alternative code style
// Send the response in chunks and wait for a second between each chunk.
go ctx.StreamWriter(func(w io.Writer) bool {
i++
fmt.Fprintf(w, "this is a message number %d\n", i) // write
time.Sleep(time.Second) // imaginary delay
if i == 4 {
return false // close and flush
}
return true // continue write
})
// when this handler finished the client should be see the stream writer's contents
// simulate a job here...
time.Sleep(timeWaitForCloseStream)
})
app.Listen(":8080")
}