Quick start

Create an empty file, let's assume its name is example.go, then open it and copy-paste the below code.

package main

import "github.com/kataras/iris/v12"

type PingResponse struct {
    Message string `json:"message"`
}

func main() {
    app := iris.New()
    app.Use(myMiddleware)

    app.Get("/ping", func(ctx iris.Context) {
        res := PingResponse{
            Message: "pong",
        }
        ctx.JSON(res)
    })

    /* Same as:
    app.Handle("GET", "/ping", func(ctx iris.Context) {
        ctx.JSON(iris.Map{
            "message": "pong",
        })
    })
    */

    // Listens and serves incoming http requests
    // on http://localhost:8080.
    app.Listen(":8080")
}

func myMiddleware(ctx iris.Context) {
    ctx.Application().Logger().Infof("Runs before %s", ctx.Path())
    ctx.Next()
}

Start a terminal session and execute the following.

Show me more!

Let's take a small overview of how easy is to get up and running.

Wanna re-start your app automatically when source code changes happens? Install the iris-cli tool and execute iris-cli run instead of go run main.go.

At the next section we will learn more about Routing.

Last updated

Was this helpful?