Hello, Iris Go

最近在学习 Iris 框架,首先,我们参照教程来写个 Hello World。


Install Go & Iris

brew install go
go get -u github.com/kataras/iris

Create working directory & main.go

mkdir $GOPATH/src/gowork
cd $GOPATH/src/gowork
touch 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/hello
	app.Get("/hello", func(ctx iris.Context) {
		ctx.JSON(iris.Map{"message": "Hello, Iris Go!"})
	})

	app.Run(iris.Addr(":8080"), iris.WithoutServerError(iris.ErrServerClosed))
}

Run Web Server

go run main.go
Now listening on: http://localhost:8080 
Application started. Press CTRL+C to shut down.

参考:

https://iris-go.com/v11/start
https://golang.org/doc/code.html#GOPATH
https://github.com/iris-contrib/examples

 650 total views,  1 views today