Gream is a Dream Web Framework written in Go(Golang)
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的状态.
有疑问加站长微信联系(非本文作者)