iris 编写程序
iris 框架是使用 go 语言编写的 Web 程序开发框架,它功能强大,使用简单,具有极高的开发和运行效率。
我们使用 iris 框架,编写一个最简单的 Web 应用,实现在浏览器输出 “Hello World”。
1. 创建项目 helloworld
创建 helloworld 项目目录。
$ mkdir helloworld && cd helloworld
2. 使用 go mod 管理包
$ go mod init helloworld
3. 编写程序,存放到 helloworld.go 文件,代码如下:
package main
import (
"github.com/kataras/iris"
)
func main() {
app := iris.New()
app.Handle("GET", "/", func(ctx iris.Context) {
ctx.HTML("<h1>Hello World<h1>")
})
app.Run(
iris.Addr("localhost:8000"),
iris.WithoutServerError(iris.ErrServerClosed),//无服务错误提示
)
}
4. 运行程序
在 helloworld 目录下,使用 go run 命令运行程序。
$ go run helloworld.go
5. 查看运行结果
打开浏览器,地址栏中输入:http://localhost:8000,就会看到浏览器中输出:Hello World。
下一章:Iris HTTP 主机配置
监听和服务你可以启动服务监听任何类型为 net.Listener 乃至 http.Server 实例。服务器的初始化方法应该是在最后, 通过 Run 方法执行。Go 开发者使用最常用的方法是通过使用 ...