This is a creation in Article, where the information may have evolved or changed.
The Martini Framework is a powerful development framework for rapidly building modular Web applications and services using the go language as a development language. Martini is a framework that is designed to handle web-related content, and does not have its own ORM or detailed layered content. So when we use Martini as our development framework, we also need to select the appropriate ORM and other packages. Yesterday, the elephant brother looked down, feeling still pretty dick pretty simple, not wordy on the code.
Package Mainimport ("Github.com/astaxie/beego/context" "Github.com/go-martini/martini" "Github.com/martini-contrib /render "" Net/http "" FMT "//define a own middleware, where Beego context is injected into the Func mycontext () martini. Handler {return func (res http. Responsewriter, req *http. Request, C Martini. Context) {CTX: = context. Context{request:req, Responsewriter:res}ctx. Input = context. Newinput (req) ctx. Output = context. Newoutput () C.map (CTX)}}func main () {m: = Martini. Classic () M.use (render. Renderer ())//inject middleware (processor middleware for rendering JSON and HTML templates) M.use (Mycontext ())//inject your own written middleware M. Use (func (C martini). Context) {fmt. Println ("before a Request") C.next ()//next method last processed Fmt.println ("after a Request")})//Normal Get Way Route m.get ("/", Func () string {return "Hello world!"}) Routing group M. Group ("/books", func (R Martini). Router) {r.get ("/list", Getbooks) r.post ("/add", Getbooks) r.delete ("/delete", Getbooks)})// We use middleware to inject a handlerm.use (MyHeader (m)) m.runonaddr (": 8080")//Run the program listening Port}func getbooks () string {return "books"}// Middleware Handlerfunc MyHeader (M *martini. ClassicmarTini) Martini. Handler {return func () {M.group ("/app", func (R Martini). Router) {my: = new (APP) r.get ("/index", My. Index) r.get ("/test/:aa", My. Test)})}}//applies the processing Type app Struct{}func (this *app) Index (R render. Render, CTX context. Context) {fmt. Println (CTX. Input.query ("action")) CTX. WriteString ("Hello World")}func (this *app) Test (R render. Render, params martini. Params, req *http. Request) {fmt. Println (params) Parm: = Make (map[string]interface{}) if T, OK: = params["AA"]; OK {parm["AA"] = t}req. Parseform () fmt. Println (Parm, req. Form) R.text ("----")}