温欣爸比

  • 主页
  • Alfred Workflow
  • 《Vim 练级手册》
  • 常用命令
  • 代码笔记
  • 合辑
  • 在线工具
所有文章 友链 关于我

温欣爸比

  • 主页
  • Alfred Workflow
  • 《Vim 练级手册》
  • 常用命令
  • 代码笔记
  • 合辑
  • 在线工具

Go Web 框架 iris 简单入门

2019-03-01

作为后端开发,初识一门语言时,选一款合适的 Web 框架是很有必要的。



  • 下载
  • Hello World
  • 日志输出
  • template 使用
  • 更多的 method

经过这篇文章 6 款最棒的 Go 语言 Web 框架简介的介绍,有理由相信 iris 是目前最优秀的 Web 框架,今天简单了解下这个框架的使用。

下载

1
$ go get -u github.com/kataras/iris

Hello World

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
package main

import (
"github.com/kataras/iris"
)

var app *iris.Application

func main() {
app = iris.Default()
app.Get("/hello", func(ctx iris.Context) {
ctx.JSON(iris.Map{
"message": "Hello World",
})
})
// listen and serve on http://0.0.0.0:8080.
app.Run(iris.Addr(":8080"))
}

初始化和路由配置都很简单

1
2
3
4
5
6
7
8
9
10
11
$ go run main.go
$ http :8080/hello

HTTP/1.1 200 OK
Content-Length: 25
Content-Type: application/json; charset=UTF-8
Date: Thu, 28 Feb 2019 07:25:00 GMT

{
"message": "Hello World"
}

日志输出

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
package main

import (
"github.com/kataras/iris"
"github.com/kataras/iris/middleware/logger"
)

var app *iris.Application

func main() {
app = iris.Default()
app.Use(logger.New())
app.Logger().SetLevel("debug")
app.Get("/hello", func(ctx iris.Context) {
app.Logger().Debug("Hello World")
app.Logger().Info("Hello World")
app.Logger().Error("Hello World")
ctx.JSON(iris.Map{
"message": "Hello World",
})
})
// listen and serve on http://0.0.0.0:8080.
app.Run(iris.Addr(":8080"))
}

template 使用

使项目目录结构如下

1
2
3
4
5
6
7
.
├── README.md
├── main.go
├── static
│   └── hello.js
└── templates
└── index.html

注册 template 目录和匹配文件

1
app.RegisterView(iris.HTML("./templates", ".html"))

初始化静态文件目录

1
app.StaticWeb("static", "./static")

传递 template 变量,并显示页面

1
2
3
4
app.Get("/index", func(ctx iris.Context) {
ctx.ViewData("name", "wxnacy")
ctx.View("index.html")
})

运行

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
$ go run main.go
$ http :8080/index

HTTP/1.1 200 OK
Content-Length: 129
Content-Type: text/html; charset=UTF-8
Date: Thu, 28 Feb 2019 07:56:57 GMT

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
Hello wxnacy
</body>
</html>

$ http :8080/static/hello.js

HTTP/1.1 200 OK
Accept-Ranges: bytes
Content-Length: 51
Content-Type: application/javascript; charset=UTF-8
Date: Thu, 28 Feb 2019 07:57:33 GMT
Last-Modified: Thu, Feb 28 2019 07:47:22 GMT

function hello(){
console.log("Hello World");
};

完整代码地址:https://github.com/wxnacy/study/tree/master/goland/src/iris_examples/mvc-templates

更多的 method

1
2
3
4
5
6
7
8
9
app.Post("/", func(ctx iris.Context){}) -> for POST http method.
app.Put("/", func(ctx iris.Context){})-> for "PUT" http method.
app.Delete("/", func(ctx iris.Context){})-> for "DELETE" http method.
app.Options("/", func(ctx iris.Context){})-> for "OPTIONS" http method.
app.Trace("/", func(ctx iris.Context){})-> for "TRACE" http method.
app.Head("/", func(ctx iris.Context){})-> for "HEAD" http method.
app.Connect("/", func(ctx iris.Context){})-> for "CONNECT" http method.
app.Patch("/", func(ctx iris.Context){})-> for "PATCH" http method.
app.Any("/", func(ctx iris.Context){}) for all http methods.

更多例子

最近更新
Alfred Workflow 命令行帮助工具
最近热读
Go 判断数组中是否包含某个 item
Vim 高级功能 vimgrep 全局搜索文件
办理北京工作居住证的一些细节
Go 语法错误:Non-declaration statement outside function body
Mac 电脑查看字体文件位置
扫码关注公众号,或搜索公众号“温欣爸比” 及时获取我的最新文章
赏

谢谢你请我喝咖啡

支付宝
微信
  • go
Go 自动打开系统默认浏览器
简单了解非对称加密算法
  1. 1. 下载
  2. 2. Hello World
  3. 3. 日志输出
  4. 4. template 使用
  5. 5. 更多的 method
© 2017 - 2022 温欣爸比 京ICP备15062634号 总访问量3289次 访客数3241人次 本文总阅读量3次
Hexo Theme Yilia by Litten
  • 所有文章
  • 友链
  • 关于我

tag:

  • python
  • flask
  • javascript
  • docker
  • 工具
  • openresty
  • 微信
  • java
  • hexo
  • 杂谈
  • vim
  • git
  • mysql
  • http
  • linux
  • mac
  • tmux
  • ssh
  • 算法
  • 开发
  • node
  • 杂文
  • jinja2
  • maven
  • spring
  • 北京
  • 生活
  • springboot
  • react
  • shell
  • graphql
  • iterm
  • expect
  • nginx
  • sqlalchemy
  • html
  • electron
  • vagrant
  • elastic
  • 宝贝
  • ansible
  • css
  • jquery
  • go
  • markdown
  • awk
  • redis
  • leetcode
  • zsh
  • 漫威
  • ssr
  • android
  • ffmpeg
  • chrome
  • vmware
  • youtube
  • windows
  • jupyter
  • excel
  • jq
  • Mac
  • Homebrew
  • mongo
  • py2
  • HomeBrew
  • movie
  • nodejs

    缺失模块。
    1、请确保node版本大于6.2
    2、在博客根目录(注意不是yilia根目录)执行以下命令:
    npm i hexo-generator-json-content --save

    3、在根目录_config.yml里添加配置:

      jsonContent:
        meta: false
        pages: false
        posts:
          title: true
          date: true
          path: true
          text: false
          raw: false
          content: false
          slug: false
          updated: false
          comments: false
          link: false
          permalink: false
          excerpt: false
          categories: false
          tags: true
    

  • Guru99
每天看书
每天背单词
每天一篇
写写代码
听听周杰伦
爱爱老婆