1. Iris起服務
package main import "github.com/kataras/iris" func main() { //1.創建app結構體對象 app := iris.New() //返回一個application //2.端口監聽(啟動服務本質就是監聽端口) //iris.WithoutServerError 設置錯誤 app.Run(iris.Addr(":7999"), iris.WithoutServerError(iris.ErrServerClosed)) //也可以不設置錯誤 //application.Run(iris.Addr(":8080")) //application.Run(iris.Addr(":8080"), iris.WithoutServerError(iris.ErrServerClosed)) //第二種 }
2. 數據請求方式的分類
所有的項目中使用的請求都遵循HTTP協議標准,HTTP協議經過了1.0和1.1兩個版本的發展。
-
HTTP1.0定義了三種請求方法: GET, POST 和 HEAD方法。
-
HTTP1.1新增了五種請求方法:OPTIONS, PUT, DELETE, TRACE 和 CONNECT 方法。
因此,我們可以說,HTTP協議一共定義了八種方法用來對Request-URI網絡資源的不同操作方式,這些操作具體為:GET、POST、PUT、DELETE、HEAD、OPTIONS、TRACE、CONNECT等八種操作方式。
請求方式:
package main import ( "github.com/kataras/iris" "github.com/kataras/iris/context" ) func main() { app := iris.New() //第一個參數是路徑 //第二個是匿名函數,處理請求 app.Get("/getRequest", func(context context.Context) { //處理get請求,請求的url為:/getRequest path := context.Path() //以日志的方式打印 app.Logger().Info(path) //2020/02/05 07:17 /getRequest }) //1.處理Get請求 app.Get("/userpath", func(context context.Context) { //獲取Path path := context.Path() app.Logger().Info(path) //2020/02/05 07:18 /userpath //寫入返回數據:string類型 context.WriteString("請求路徑:" + path) }) //2.處理Get請求 並接受參數 //http://localhost:8000/userinfo?username=yang&pwd=123456 app.Get("/userinfo", func(context context.Context) { path := context.Path() app.Logger().Info(path) //獲取get請求所攜帶的參數 userName := context.URLParam("username") app.Logger().Info(userName) pwd := context.URLParam("pwd") app.Logger().Info(pwd) //返回html數據格式 context.HTML("<h1>" + userName + "," + pwd + "</h1>") }) //3.處理Post請求 form表單的字段獲取 app.Post("/postLogin", func(context context.Context) { path := context.Path() app.Logger().Info(path) //context.PostValue方法來獲取post請求所提交的form表單數據 name := context.PostValue("name") pwd := context.PostValue("pwd") app.Logger().Info(name, " ", pwd) context.HTML(name) }) //4.處理Post請求 Json格式數據 //Postman工具選擇[{"key":"Content-Type","value":"application/json","description":""}] //請求內容:{"name": "davie","age": 28} app.Post("/postJson", func(context context.Context) { //1.path path := context.Path() app.Logger().Info("請求URL:", path) //2.Json數據解析 var person Person //通過context.ReadJSON()讀取傳過來的數據 if err := context.ReadJSON(&person); err != nil { panic(err.Error()) } //返回格式化的內容 context.Writef("Received: %#+v\n", person) }) //5.處理Post請求 Xml格式數據 /* * 請求配置:Content-Type到application/xml(可選但最好設置) * 請求內容: * * <student> * <stu_name>davie</stu_name> * <stu_age>28</stu_age> * </student> * */ app.Post("/postXml", func(context context.Context) { //1.Path path := context.Path() app.Logger().Info("請求URL:", path) //2.XML數據解析 var student Student if err := context.ReadXML(&student); err != nil { panic(err.Error()) } //返回格式化的內容 context.Writef("Received:%#+v\n", student) }) //6.put請求 app.Put("/putinfo", func(context context.Context) { path := context.Path() app.Logger().Info("請求url:", path) }) //7.delete請求 app.Delete("/deleteuser", func(context context.Context) { path := context.Path() app.Logger().Info("Delete請求url:", path) }) app.Run(iris.Addr(":8000"), iris.WithoutServerError(iris.ErrServerClosed)) } //自定義的struct type Person struct { Name string `json:"name"` Age int `json:"age"` } //自定義的結構體 type Student struct { //XMLName xml.Name `xml:"student"` StuName string `xml:"stu_name"` StuAge int `xml:"stu_age"` } type config struct { Addr string `yaml:"addr"` ServerName string `yaml:"serverName"` }
數據返回類型:
package main import ( "github.com/kataras/iris" "github.com/kataras/iris/context" ) type Teacher struct { Name string `json:"Name"` City string `json:"City"` Other string `json:"Other"` } //程序主入口 func main() { app := iris.New() /** * 通過Get請求 * 返回 WriteString */ app.Get("/getHello", func(context context.Context) { context.WriteString(" Hello world ") }) /** * 通過Get請求 * 返回 HTML數據 */ app.Get("/getHtml", func(context context.Context) { context.HTML("<h1> Davie, 12 </h1>") }) /** * 通過Get請求 * 返回 Json數據 */ app.Get("/getJson", func(context context.Context) { context.JSON(iris.Map{"message": "hello word", "requestCode": 200}) }) //POST app.Post("/user/postinfo", func(context context.Context) { //context.WriteString(" Post Request ") //user := context.Params().GetString("user") var tec Teacher err := context.ReadJSON(&tec) if err != nil { panic(err.Error()) } else { app.Logger().Info(tec) context.WriteString(tec.Name) } //fmt.Println(user) //teacher := Teacher{} //err := context.ReadForm(&teacher) //if err != nil { // panic(err.Error()) //} else { // context.WriteString(teacher.Name) //} }) //PUT app.Put("/getHello", func(context context.Context) { context.WriteString(" Put Request ") }) app.Delete("/DeleteHello", func(context context.Context) { context.WriteString(" Delete Request ") }) //返回json數據 app.Get("/getJson", func(context context.Context) { context.JSON(iris.Map{"message": "hello word", "requestCode": 200}) }) app.Get("/getStuJson", func(context context.Context) { context.JSON(Student{Name: "Davie", Age: 18}) }) //返回xml數據 app.Get("/getXml", func(context context.Context) { context.XML(Person{Name: "Davie", Age: 18}) }) //返回Text app.Get("/helloText", func(context context.Context) { context.Text(" text hello world ") }) app.Run(iris.Addr(":8000"), iris.WithoutServerError(iris.ErrServerClosed)) } //json結構體 type Student struct { Name string `json:"name"` Age int `json:"age"` } //xml結構體 type Person struct { Name string `xml:"name"` Age int `xml:"age"` }