Quick start
Create an empty file, let's assume its name is example.go, then open it and copy-paste the below code.
1
package main
2
3
import "github.com/kataras/iris/v12"
4
5
func main() {
6
app := iris.Default()
7
app.Use(myMiddleware)
8
9
app.Handle("GET", "/ping", func(ctx iris.Context) {
10
ctx.JSON(iris.Map{"message": "pong"})
11
})
12
13
// Listens and serves incoming http requests
14
// on http://localhost:8080.
15
app.Listen(":8080")
16
}
17
18
func myMiddleware(ctx iris.Context) {
19
ctx.Application().Logger().Infof("Runs before %s", ctx.Path())
20
ctx.Next()
21
}
Copied!
Start a terminal session and execute the following.
1
# run example.go and visit http://localhost:8080/ping on browser
2
$ go run example.go
Copied!

Show me more!

Let's take a small overview of how easy is to get up and running.
1
package main
2
3
import "github.com/kataras/iris/v12"
4
5
func main() {
6
app := iris.New()
7
// Load all templates from the "./views" folder
8
// where extension is ".html" and parse them
9
// using the standard `html/template` package.
10
app.RegisterView(iris.HTML("./views", ".html"))
11
12
// Method: GET
13
// Resource: http://localhost:8080
14
app.Get("/", func(ctx iris.Context) {
15
// Bind: {{.message}} with "Hello world!"
16
ctx.ViewData("message", "Hello world!")
17
// Render template file: ./views/hello.html
18
ctx.View("hello.html")
19
})
20
21
// Method: GET
22
// Resource: http://localhost:8080/user/42
23
//
24
// Need to use a custom regexp instead?
25
// Easy;
26
// Just mark the parameter's type to 'string'
27
// which accepts anything and make use of
28
// its `regexp` macro function, i.e:
29
// app.Get("/user/{id:string regexp(^[0-9]+$)}")
30
app.Get("/user/{id:uint64}", func(ctx iris.Context) {
31
userID, _ := ctx.Params().GetUint64("id")
32
ctx.Writef("User ID: %d", userID)
33
})
34
35
// Start the server using a network address.
36
app.Listen(":8080")
37
}
Copied!
1
<!-- file: ./views/hello.html -->
2
<html>
3
<head>
4
<title>Hello Page</title>
5
</head>
6
<body>
7
<h1>{{.message}}</h1>
8
</body>
9
</html>
Copied!
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 modified 1yr ago
Export as PDF
Copy link