Go语言框架Iris-01

安装Iris

go install github.com/kataras/iris@master

可能会出现连接不上的问题,可以使用七牛云的代理
具体可以看看这个https://goproxy.cn/

$ go env -w GO111MODULE=on
$ go env -w GOPROXY=https://goproxy.cn,direct

Hello,Iris

func main() {
	app := iris.New()
	app.Run(iris.Addr(":8558"), iris.WithoutServerError(iris.ErrServerClosed))
}

Get,Post测试

  • 注意app.Run要写道处理最后,不然会404页面
type Test struct {
	Name string `json:"name"`
	Pwd  string `json:"pwd"`
}
type Test2 struct {
	Name string `form:"name"`
	Pwd  string `form:"pwd"`
}

func main() {
	app := iris.New()
	app.Get("/", func(context context.Context) {
		path := context.Path()
		app.Logger().Info(path)
	})
	app.Get("/getTest", func(context context.Context) {
		param1 := context.URLParam("param1")
		app.Logger().Info(param1)
		context.WriteString("ok")
	})
	app.Post("/userinfo", func(context context.Context) {
		var test Test
		if err := context.ReadJSON(&test); err != nil {
			panic(err.Error())
		}
		app.Logger().Info(test)
		context.WriteString("ok")
	})
        app.Post("/userinfo2", func(context context.Context) {
		var test Test2
		if err := context.ReadForm(&test); err != nil {
			panic(err.Error())
		}
		app.Logger().Info(test)
		context.WriteString("ok")
	})
	app.Run(iris.Addr(":8558"))
}

RESTful风格Url

func main() {
	app := iris.Default()
	//newsid,限制只能为uint64
	app.Get("/news/{date}/{newsid:uint64}", func(context context.Context) {
		date := context.Params().Get("date")
		newsid := context.Params().Get("newsid")
		app.Logger().Info(date, newsid)
		context.WriteString(date + "," + newsid)
	})
	app.Run(iris.Addr(":8558"))
}

posted @ 2022-12-09 15:48  花茶冰糖  阅读(80)  评论(0编辑  收藏  举报