Gream 路由

栏目: Go · 发布时间: 7年前

内容简介:是的,Gream是我想编写的Golang的web框架,因为传统的golang web框架感觉都不太爽,就从路由来说就能看的出来,大家都基本是一种很函数的写法,看到最接近rails写法的应该是但是他这种写法也是很不爽. 所以干脆自己来写一个

Gream is a Dream Web Framework written in Go(Golang)

Gream 路由

Github

Gream

是的,Gream是我想编写的Golang的web框架,因为传统的golang web框架感觉都不太爽,就从路由来说就能看的出来,大家都基本是

package main

import "github.com/gin-gonic/gin"

func main() {
    r := gin.Default()
    r.GET("/ping", func(c *gin.Context) {
        c.JSON(200, gin.H{
            "message": "pong",
        })
    })
    r.Run() // listen and serve on 0.0.0.0:8080
}

一种很函数的写法,看到最接近rails写法的应该是 gobuffalo 框架.

func muxer() http.Handler {
  f := func(res http.ResponseWriter, req *http.Request) {
    fmt.Fprintf(res, "%s - %s", req.Method, req.URL.String())
  }
  mux := mux.NewRouter()
  mux.HandleFunc("/foo", f).Methods("GET")
  mux.HandleFunc("/bar", f).Methods("POST")
  mux.HandleFunc("/baz/baz", f).Methods("DELETE")
  return mux
}

a.Mount("/admin", muxer())

但是他这种写法也是很不爽. 所以干脆自己来写一个

GET("/home/{name}", "home#index")
GET("/home_json/{name}", "home#index_json")
GET("/admin_home/{name}", "admin/home#index")

scope := Scope("scope")
{
    scope.GET("/home1/{name}", "home#index")
    scope.GET("/home2/{name}", "home#index")
}

scope = Scope(H{"module": "admin"})
{
    scope.GET("/home1/{name}", "home#index")
    scope.GET("/home2/{name}", "home#index")
}

GET("/home_path/{name}", "home#index", H{"path": "ttt"})
GET("/home_module/{name}", "home#index", H{"module": "admin"})

namespace := Namespace("admin")
{
    namespace.GET("/homea/{name}", "home#index")
    namespace.GET("/homeb/{name}", "home#index")
}

//Resources("users", H{"except": "index"})
Resources("users", H{"only": "index,new"})

看上去就干爽多了, 目前已经尽力能达到rails的router的状态.


以上所述就是小编给大家介绍的《Gream 路由》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Coding the Matrix

Coding the Matrix

Philip N. Klein / Newtonian Press / 2013-7-26 / $35.00

An engaging introduction to vectors and matrices and the algorithms that operate on them, intended for the student who knows how to program. Mathematical concepts and computational problems are motiva......一起来看看 《Coding the Matrix》 这本书的介绍吧!

MD5 加密
MD5 加密

MD5 加密工具

XML 在线格式化
XML 在线格式化

在线 XML 格式化压缩工具

html转js在线工具
html转js在线工具

html转js在线工具