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 路由》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

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

The Linux Command Line

The Linux Command Line

William E. Shotts Jr. / No Starch Press, Incorporated / 2012-1-17 / USD 39.95

You've experienced the shiny, point-and-click surface of your Linux computer-now dive below and explore its depths with the power of the command line. The Linux Command Line takes you from your very ......一起来看看 《The Linux Command Line》 这本书的介绍吧!

HTML 压缩/解压工具
HTML 压缩/解压工具

在线压缩/解压 HTML 代码

图片转BASE64编码
图片转BASE64编码

在线图片转Base64编码工具

正则表达式在线测试
正则表达式在线测试

正则表达式在线测试