美文网首页
go iris初探

go iris初探

作者: sweetsky0901 | 来源:发表于2019-05-07 11:28 被阅读0次

首先安装go环境。

$ brew install go

安装iris

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

创建一个go的工作路径

$ mkdir /Users/xxx/goworkdir/src/gowork
$ touch main.go

编辑main.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.Logger().SetLevel("debug")
    // Optionally, add two built'n handlers
    // that can recover from any http-relative panics
    // and log the requests to the terminal.
    app.Use(recover.New())
    app.Use(logger.New())

    // Method:   GET
    // Resource: http://localhost:8080
    app.Handle("GET", "/", func(ctx iris.Context) {
        ctx.HTML("<h1>Welcome</h1>")
    })

    // same as app.Handle("GET", "/ping", [...])
    // Method:   GET
    // Resource: http://localhost:8080/ping
    app.Get("/ping", func(ctx iris.Context) {
        ctx.WriteString("pong")
    })

    // Method:   GET
    // Resource: http://localhost:8080/hello
    app.Get("/hello", func(ctx iris.Context) {
        ctx.JSON(iris.Map{"message": "Hello Iris!"})
    })

    // http://localhost:8080
    // http://localhost:8080/ping
    // http://localhost:8080/hello
    app.Run(iris.Addr(":8080"), iris.WithoutServerError(iris.ErrServerClosed))
}

启动server

$ go run main.go

这样就完成了第一次对iris的试探。

相关文章

  • go iris初探

    首先安装go环境。 安装iris 创建一个go的工作路径 编辑main.go 启动server 这样就完成了第一次...

  • 从hello-world开始go iris

    从hello-world开始go iris iris框架是少数支持MVC的go web框架。在简单业务逻辑测试中,...

  • Golang Context

    参考Go语言实战笔记(二十)| Go ContextGolang context初探 一、WaitGroup 这是...

  • Iris框架认识

    关于Iris Iris是一个通过GO编写的快速的,简单的,但是功能齐全和非常有效率的web框架Iris为你下一个网...

  • golang iris validator

    iris 并没有内置数据校验,所以使用go-playground的validator

  • Go初探

    package main import “fmt” func main(){ fmt.Println(“hello...

  • Go初探

    1 Go语言特性 自动垃圾回收 更丰富的内置类型 函数多返回值 错误处理 匿名函数和闭包 类型和接口 并发编程 反...

  • go iris学习日记(二) go iris连接mysql并返回

    承接上一篇go iris框架的文章, 最近想用它来写一个服务端。话不多说, 直接上代码。首先创建main.go文件...

  • go web开发之iris(二)初识iris

    iris不做介绍,相关的信息可以在github上找到。iris的特点就是简单、全面、易于上手。先来看一个简单的例子...

  • GO的web框架_IRIS

    前置条件go环境安装: https://www.jianshu.com/p/da4f93485051 一、安装 g...

网友评论

      本文标题:go iris初探

      本文链接:https://www.haomeiwen.com/subject/ffqeoqtx.html