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"))
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!