icon-cookie
The website uses cookies to optimize your user experience. Using this website grants us the permission to collect certain information essential to the provision of our services to you, but you may change the cookie settings within your browser any time you wish. Learn more
I agree
blank_error__heading
blank_error__body
Text direction?

Quick start

Gerasimos (Makis) Maropoulos edited this page on Jun 18 · 2 revisions

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"

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

    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.

# run example.go and visit http://localhost:8080/ping on browser
$ go run example.go

Show me more!

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

package main

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

func main() {
    app := iris.New()
    // Load all templates from the "./views" folder
    // where extension is ".html" and parse them
    // using the standard `html/template` package.
    app.RegisterView(iris.HTML("./views", ".html"))

    // Method:    GET
    // Resource:  http://localhost:8080
    app.Get("/", func(ctx iris.Context) {
        // Bind: {{.message}} with "Hello world!"
        ctx.ViewData("message", "Hello world!")
        // Render template file: ./views/hello.html
        ctx.View("hello.html")
    })

    // Method:    GET
    // Resource:  http://localhost:8080/user/42
    //
    // Need to use a custom regexp instead?
    // Easy;
    // Just mark the parameter's type to 'string'
    // which accepts anything and make use of
    // its `regexp` macro function, i.e:
    // app.Get("/user/{id:string regexp(^[0-9]+$)}")
    app.Get("/user/{id:uint64}", func(ctx iris.Context) {
        userID, _ := ctx.Params().GetUint64("id")
        ctx.Writef("User ID: %d", userID)
    })

    // Start the server using a network address.
    app.Listen(":8080")
}
<!-- file: ./views/hello.html -->
<html>
<head>
    <title>Hello Page</title>
</head>
<body>
    <h1>{{.message}}</h1>
</body>
</html>

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.

Measure
Measure
Related Notes
Get a free MyMarkup account to save this article and view it later on any device.
Create account

End User License Agreement

Summary | 16 Annotations
Parameters in path
2020/07/02 07:25
Dependency Injection
2020/07/02 07:25
hero
2020/07/02 07:26
Request & Response & Path Parameters
2020/07/03 06:18
Custom Preflight
2020/07/03 06:18
Register Dependencies
2020/07/03 06:18
Querystring parameters
2020/07/03 06:18
Grouping routes
2020/07/03 06:18
Using middleware
2020/07/03 06:18
Model binding and validation
2020/07/03 06:18
Cookies
2020/07/03 06:18
Basic Authentication
2020/07/03 06:18
a function dependency can accept previous registered dependencies and update or return a new value of any type
2020/07/03 07:06
Party.ConfigureContainer
2020/07/03 07:07
testInput
2020/07/03 09:15
Built-in Func
2020/07/03 09:59