go系列(5)- beego自己写controller
生活随笔
收集整理的這篇文章主要介紹了
go系列(5)- beego自己写controller
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
前邊的系列文章已經(jīng)講述了如何安裝環(huán)境, beego的處理邏輯都是在Controller里面完成的,下面就寫一個最簡單的Controller。
我們在寫自己的controller的時候,一定要繼承beego.Controller,也一定要記得導(dǎo)入?github.com/astaxie/beego
獲取用戶請求數(shù)據(jù)的方法
用戶通常通過Get,Post等方式來發(fā)送數(shù)據(jù),beeGo里面會自動解析這些數(shù)據(jù),我們可以用下面這些方法來處理。
- GetString(key string) string
- GetStrings(key string) []string
- GetInt(key string) (int64,error)
- GetBool(key string)(bool,error)
- GetFloat(key string)(float64,error)
1.我們先在router/router.go里面添加一條路由,路由指向我們自己的目錄
beego.Router("/getdata", &controllers.TestController{}, "get:GetData")
2.然后在controllers/test.go里面寫上自己的代碼
package controllersimport ("github.com/astaxie/beego" )type TestController struct {beego.Controller }func (c *TestController) GetData() {id := c.GetString("id")c.Ctx.WriteString(id)name := c.Input().Get("name")c.Ctx.WriteString(name) }
注意這個package 必須是controllers,新建的話一般都是main,所以記得修改。
代碼寫完后重啟,此時,我們訪問?http://127.0.0.1/getdata?id=888&name=kumufengchun ,如果再測試機上,訪問ip:port/getdata?id=888&name=kumufengchun,? 那么頁面就會顯示如下
888kumufengchun
?
轉(zhuǎn)載于:https://www.cnblogs.com/kumufengchun/p/10275545.html
總結(jié)
以上是生活随笔為你收集整理的go系列(5)- beego自己写controller的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 12道锋味谢霆锋与赵薇的十年之约拍摄地点
- 下一篇: 金圣多少钱啊?