这很有趣,因为https://github.com/kataras/iris和https://iris-go.com和https://docs.iris-go.com有很多这样的例子,你会花时间问那,您可能检查了 Iris 的第三方分支,而不是 Iris 本身您在哪里找到该代码片段?
正确的方法:
package main
import "github.com/kataras/iris"
func main() {
app := iris.New()
app.Get("/hi", func(ctx iris.Context) { // go > 1.9
ctx.Writef("Hi %s", "iris")
})
app.Run(iris.Addr(":8080"))
}
这是另一个示例,一个简单的服务器,它向客户端呈现带有消息的模板文件:
// file: main.go
package main
import "github.com/kataras/iris"
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
app.Get("/user/{id:long}", func(ctx iris.Context) {
userID, _ := ctx.Params().GetInt64("id")
ctx.Writef("User ID: %d", userID)
})
// Start the server using a network address.
app.Run(iris.Addr(":8080"))
}
<!-- file: ./views/hello.html -->
<html>
<head>
<title>Hello Page</title>
</head>
<body>
<h1>{{.message}}</h1>
</body>
</html>
$ go run main.go
> Now listening on: http://localhost:8080
> Application started. Press CTRL+C to shut down.
如果您需要有关 Iris 的任何帮助,您可以随时通过实时聊天https://chat.iris-go.com与支持团队联系。祝你今天过得愉快!