go iris初探

首先安装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的试探。

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 一、Python简介和环境搭建以及pip的安装 4课时实验课主要内容 【Python简介】: Python 是一个...
    _小老虎_阅读 11,267评论 0 10
  • 《Docker环境下的前后端分离部署与运维》课程脚本 [TOC] 一、Docker虚拟机常用命令 先更新软件包yu...
    mingminy阅读 3,736评论 0 0
  • Swift1> Swift和OC的区别1.1> Swift没有地址/指针的概念1.2> 泛型1.3> 类型严谨 对...
    cosWriter阅读 13,872评论 1 32
  • JBoss AS7集群方案...2 基础环境搭建...2 安装jdk.3 AS7安装、配置3 AS7群集配置、测试...
    lannerate阅读 7,951评论 0 1
  • 申金鑫 17101223365 转载自公众号flysnow_org 【嵌牛导读】:本文介绍了各种系统下的Go语言环...
    Felixxin阅读 8,661评论 0 20

友情链接更多精彩内容