iris web框架安装使用【我的go学习第十课】

1.iris安装

go get -u github.com/kataras/iris
复制代码

安装命令简单,但是会报错,主要是网络问题:

(base) PS C:\Users\livingbody\Downloads> go get -u github.com/kataras/iris
package golang.org/x/net/publicsuffix: unrecognized import path "golang.org/x/net/publicsuffix": https fetch: Get "https://golang.org/x/net/publicsuffix?go-get=1": dial tcp 172.217.163.49:443: connectex: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond.
复制代码

2.解决办法

使用国内七牛网盘的go module镜像

具体如下

go env -w GO111MODULE=on
go env -w GOPROXY=https://goproxy.cn,direct
复制代码

3.再次安装

# 在新建项目文件夹下执行
$ mkdir myapp
$ cd myapp
$ go mod init myapp
$ go get github.com/kataras/iris/v12@master # or @v12.2.0-beta3
复制代码

安装日志


(base) PS C:\Users\livingbody\goworkspace\src\juejin> go mod init
go: C:\Users\livingbody\goworkspace\src\juejin\go.mod already exists
(base) PS C:\Users\livingbody\goworkspace\src\juejin> go get -u github.com/kataras/iris
go: downloading github.com/kataras/iris v0.0.2
go: downloading github.com/kataras/golog v0.0.18
go: downloading github.com/BurntSushi/toml v0.3.1
go: downloading github.com/kataras/pio v0.0.8
go: downloading github.com/kataras/sitemap v0.0.5
复制代码
(base) PS C:\Users\livingbody\goworkspace\src\juejin> ls


    目录: C:\Users\livingbody\goworkspace\src\juejin


Mode                 LastWriteTime         Length Name
----                 -------------         ------ ----
-a----         2022/7/13     21:56           2464 go.mod
-a----         2022/7/13     21:56          27966 go.sum
复制代码

4.验证

写个小程序试试

package main

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

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

	app.Get("/", func(ctx iris.Context) {
		ctx.HTML("Hello <strong>%s</strong>!", "World")
	})

	app.Listen(":8080")
}

复制代码

访问 localhost:8080 出现“Hello World!”即成功

1.jpg