iris-go

中柠檬 · · 13057 次点击 · · 开始浏览    
这是一个创建于 的文章,其中的信息可能已经有所发展或是发生改变。

rt

与其它框架比较

文档说明

基础

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")
}

 


有疑问加站长微信联系(非本文作者)

本文来自:开源中国博客

感谢作者:中柠檬

查看原文:iris-go

入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889

13057 次点击  
加入收藏 微博
上一篇:压力测试
1 回复  |  直到 2018-09-12 09:12:59
暂无回复
添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传