Go-martini Basic Primer

Source: Internet
Author: User
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 ("----")}





Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.