生命不止,继续 go go go !!!

接下来,想跟大家一起分享一些golang语言成熟的、知名度比较高的web框架。

我们从iris web框架开始,开始呢,我们先不去计较和比较谁的速度快,谁的性能好,让我们先学习如何使用,积累到了一定程度后,再去进行测试各个框架的速度性能。

ris自称是Go语言中所有Web框架最快的,它的特点如下:

1.聚焦高性能 
2.健壮的静态路由支持和通配符子域名支持。 
3.视图系统支持超过5以上模板 
4.支持定制事件的高可扩展性Websocket API 
5.带有GC, 内存 & redis 提供支持的会话 
6.方便的中间件和插件 
7.完整 REST API 
8.能定制 HTTP 错误 
9.Typescript编译器 + 基于浏览器的编辑器 
10.内容 negotiation & streaming 
11.传送层安全性 
12.源码改变后自动加载 
13.OAuth, OAuth2 支持27+ API providers 
14.JSON Web Tokens

kataras/iris简介

github地址 
https://github.com/kataras/iris

Star: 7938

文档地址 
https://docs.iris-go.com/

描述 
关于kataras/iris的描述十分霸气: 
The fastest web framework for Go in (THIS) Earth. HTTP/2 Ready to GO. MVC when you need it. 
还是那句话,暂时不去计较,只是学习。

获取 
go get -u github.com/kataras/iris

快速开始

新建main.go 
新建views文件夹,在views中新建hello.html

main.go

package mainimport "github.com/kataras/iris"func main() {app := iris.New()app.RegisterView(iris.HTML("./views", ".html"))app.Get("/", func(ctx iris.Context) {ctx.ViewData("message", "Hello world!")ctx.View("hello.html")})app.Run(iris.Addr(":8080"))
}

hello.html

<html>
<head><title>Hello Page</title>
</head>
<body><h1>{{.message}}</h1>
</body>
</html>

运行,在浏览器输入http://localhost:8080/,得到运行结果。

basic认证

之前写过关于basic认证的文章: 
Go实战–通过basic认证的http(basic authentication)

package mainimport ("time""github.com/kataras/iris""github.com/kataras/iris/context""github.com/kataras/iris/middleware/basicauth"
)func newApp() *iris.Application {app := iris.New()authConfig := basicauth.Config{Users:   map[string]string{"wangshubo": "wangshubo", "superWang": "superWang"},Realm:   "Authorization Required",Expires: time.Duration(30) * time.Minute,}authentication := basicauth.New(authConfig)app.Get("/", func(ctx context.Context) { ctx.Redirect("/admin") })needAuth := app.Party("/admin", authentication){//http://localhost:8080/adminneedAuth.Get("/", h)// http://localhost:8080/admin/profileneedAuth.Get("/profile", h)// http://localhost:8080/admin/settingsneedAuth.Get("/settings", h)}return app
}func main() {app := newApp()app.Run(iris.Addr(":8080"))
}func h(ctx context.Context) {username, password, _ := ctx.Request().BasicAuth()ctx.Writef("%s %s:%s", ctx.Path(), username, password)
}

Markdown

Go实战–golang中使用markdown(russross/blackfriday)

package mainimport ("time""github.com/kataras/iris""github.com/kataras/iris/context""github.com/kataras/iris/cache"
)var markdownContents = []byte(`## Hello MarkdownThis is a sample of Markdown contentsFeatures
--------All features of Sundown are supported, including:*   **Compatibility**. The Markdown v1.0.3 test suite passes withthe --tidy option.  Without --tidy, the differences aremostly in whitespace and entity escaping, where blackfriday ismore consistent and cleaner.*   **Common extensions**, including table support, fenced codeblocks, autolinks, strikethroughs, non-strict emphasis, etc.*   **Safety**. Blackfriday is paranoid when parsing, making it safeto feed untrusted user input without fear of bad thingshappening. The test suite stress tests this and there are noknown inputs that make it crash.  If you find one, please let meknow and send me the input that does it.NOTE: "safety" in this context means *runtime safety only*. In order toprotect yourself against JavaScript injection in untrusted content, see[this example](https://github.com/russross/blackfriday#sanitize-untrusted-content).*   **Fast processing**. It is fast enough to render on-demand inmost web applications without having to cache the output.*   **Routine safety**. You can run multiple parsers in differentgoroutines without ill effect. There is no dependence on globalshared state.*   **Minimal dependencies**. Blackfriday only depends on standardlibrary packages in Go. The source code is prettyself-contained, so it is easy to add to any project, includingGoogle App Engine projects.*   **Standards compliant**. Output successfully validates using theW3C validation tool for HTML 4.01 and XHTML 1.0 Transitional.[this is a link](https://github.com/kataras/iris) `)func main() {app := iris.New()app.Get("/", cache.Handler(10*time.Second), writeMarkdown)app.Run(iris.Addr(":8080"))
}func writeMarkdown(ctx context.Context) {println("Handler executed. Content refreshed.")ctx.Markdown(markdownContents)
}

YAML

Go实战–go语言中使用YAML配置文件(与json、xml、ini对比) 
iris.yml

DisablePathCorrection: false
EnablePathEscape: false
FireMethodNotAllowed: true
DisableBodyConsumptionOnUnmarshal: true
TimeFormat: Mon, 01 Jan 2006 15:04:05 GMT
Charset: UTF-8

main.go

package mainimport ("github.com/kataras/iris""github.com/kataras/iris/context"
)func main() {app := iris.New()app.Get("/", func(ctx context.Context) {ctx.HTML("<b>Hello!</b>")})app.Run(iris.Addr(":8080"), iris.WithConfiguration(iris.YAML("./iris.yml")))
}

Post Json

Go实战–net/http中JSON的使用(The way to go)

package mainimport ("fmt""github.com/kataras/iris""github.com/kataras/iris/context"
)type Company struct {Name  stringCity  stringOther string
}func MyHandler(ctx context.Context) {c := &Company{}if err := ctx.ReadJSON(c); err != nil {panic(err.Error())} else {fmt.Printf("Company: %#v", c)ctx.Writef("Company: %#v", c)}
}func main() {app := iris.New()app.Post("/bind_json", MyHandler)app.Run(iris.Addr(":8080"))
}

curl命令行执行:

curl -d '{"Name":"vSuperWang", "City":"beijing", "Other":"shit"}' -H "Content-Type: application/json" -X POST http://localhost:8080/bind_json

Go实战--也许最快的Go语言Web框架kataras/iris初识(basic认证、Markdown、YAML、Json)相关推荐

  1. Go实战--也许最快的Go语言Web框架kataras/iris初识三(Redis、leveldb、BoltDB)

    生命不止,继续 go go go !!! 之前介绍了iris框架,介绍了如何使用basic认证.Markdown.YAML.Json等:  Go实战–也许最快的Go语言Web框架kataras/iri ...

  2. Go实战--也许最快的Go语言Web框架kataras/iris初识二(TOML、Cache、Cookie)

    生命不止,继续 go go go!!! 昨天介绍了iris框架,介绍了如何使用basic认证.Markdown.YAML.Json等:  Go实战–也许最快的Go语言Web框架kataras/iris ...

  3. Go实战--也许最快的Go语言Web框架kataras/iris初识四(i18n、filelogger、recaptcha)

    生命不止,继续 go go go !!! 继续分享关于kataras/iris框架 i18n i18n(其来源是英文单词 internationalization的首末字符i和n,18为中间的字符数) ...

  4. go java web框架_java程序员10分钟可上手的golang框架golang实战使用gin+xorm搭建go语言web框架restgo...

    1.首先上效果 是不是想起spring MVC? cd $GOPATH/src git clone https://github.com/winlion/restgo-admin.git 你将得到re ...

  5. 干货分享:六个知名的Go语言web框架

    框架一直是敏捷开发中的利器,能让开发者很快的上手并做出应用,甚至有的时候,脱离了框架,一些开发者都不会写程序了.成长总不会一蹴而就,从写出程序获取成就感,再到精通框架,快速构造应用,当这些方面都得心应 ...

  6. go web框架_干货分享:六个知名的Go语言web框架

    框架一直是敏捷开发中的利器,能让开发者很快的上手并做出应用,甚至有的时候,脱离了框架,一些开发者都不会写程序了.成长总不会一蹴而就,从写出程序获取成就感,再到精通框架,快速构造应用,当这些方面都得心应 ...

  7. Go语言web框架 gin

    Go语言web框架 GIN gin是go语言环境下的一个web框架, 它类似于Martini, 官方声称它比Martini有更好的性能, 比Martini快40倍, Ohhhh-.看着不错的样子, 所 ...

  8. 流行的Go语言web框架简介

    Golang被称为云计算时代的C语言,它以其独特的优势逐渐被越来越多的公司所关注和使用.为了充分利用Golang的Web开发优势,有必要熟悉一下Go语言的web框架. 1  Beego (http:/ ...

  9. spring框架 web开发_go语言web开发框架:Iris框架讲解(一)

    Golang介绍 Go语言是谷歌推出的一种全新的编程语言,可以在不损失应用程序性能的情况下降低代码的复杂性.谷歌首席软件工程师罗布派克(Rob Pike)说:我们之所以开发Go,是因为过去10多年间软 ...

最新文章

  1. tp5.0 根据经纬度 获取附近信息_php根据前端传递的经纬度获取区域地址信息
  2. Python中的test测试
  3. Leetcode224 基本加减计算器-双栈和状态转换
  4. Netflix Zuul与Nginx的性能对比
  5. 搜索二维矩阵 II—leetcode240
  6. 计算机基础知识第3版答案,计算机基础知识试题库及答案(3)
  7. 【Pytorch神经网络理论篇】 22 自编码神经网络:概述+变分+条件变分自编码神经网络
  8. JavaScript笔记-表格中放按钮并点击调用
  9. lede usb启动_OpenWrt LEDE 自动挂载USB U盘的方法
  10. C#汉字转拼音的实现方法
  11. java实现网站统计功能_网站访问量统计功能的实现
  12. uniapp教程,uni-app教程
  13. 计算机出现全部英文如何解决,电脑开机蓝屏出现一堆英文怎么解决,教你一招三分钟解决...
  14. Qt实现基于G.729A(G729A)的语音聊天
  15. 2015年京胜杯删数!删数
  16. 企业集群平台架构设计与实现--LVS篇(二)
  17. SpringCloud分布式框架
  18. 解决互斥锁lock,报tpp.c:63: __pthread_tpp_change_priority: Assertion异常
  19. Verilog语法_1(reg、wire、always语法)
  20. 5分钟读懂设计模式(2)---装饰者模式

热门文章

  1. 大学一年级计算机组成语结构试题,一年级语文期终试卷
  2. 12V升30V大功率2x100W双声道D类音频功放升压组合解决方案
  3. 新电脑必装的良心软件,你知道几个
  4. 这是我见过的“最美”数据中心机房布线
  5. NFC(近场通信)技术研讨会
  6. Win11 22H2 22621.521大版本更新!
  7. 一行代码解决判断IE浏览器和提示升级问题
  8. Influx windows版安装
  9. 初学者的Kubernetes圣经 1
  10. 原生JS 实现日期倒计时效果