Gream is a Dream Web Framework written in Go(Golang)
Gream
是的门粪,Gream是我想編寫的Golang的web框架喷众,因為傳統(tǒng)的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
}
一種很函數(shù)的寫法罐孝,看到最接近rails寫法的應(yīng)該是 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"})
看上去就干爽多了, 目前已經(jīng)盡力能達到rails的router的狀態(tài).