These are chat archives for gin-gonic/gin

4th
Jun 2015
Manu MA
@manucorporat
Jun 04 2015 00:51
@DenisNeustroev
router.Handle("GET", "/path", handler)
Alex
@alehano
Jun 04 2015 05:06
When I create a new group, do existing middlewares apply to it? Can create group without mw? Can I delete some mw?
Alex
@alehano
Jun 04 2015 07:44
How can I use http.Handler as a middleware
gopher-philippines
@ambo1994
Jun 04 2015 09:01
panic: path segment conflicts with existing wildcard
router.GET("/", GetUsers)
router.GET("/:id", GetUser)
Please help!! :)
Manu MA
@manucorporat
Jun 04 2015 11:49
@ambo1994 I do not recommend to have /:id better /user/:id
@alehano do existing middlewares apply to it? YES Can create group without mw? YES
Can I delete some mw? NO
Manu MA
@manucorporat
Jun 04 2015 11:55
if you want to remove a middleware, you are doing something wrong. Use groups or route-level middlwares if you want to use a middleware only in a subset of the API
Manu MA
@manucorporat
Jun 04 2015 13:42
@ambo1994 anyway I just tried your code, and it works
func main() {
    router := gin.Default()
    router.GET("/", func(c *gin.Context) {
        c.String(200, "index")
    })
    router.GET("/:id", func(c *gin.Context) {
        c.String(200, "%s", c.Param("id"))
    })
    router.Run(":8080")
}
it works
DenisNeustroev
@DenisNeustroev
Jun 04 2015 13:48
@manucorporat This is the first thing that I did, but the program throws an error. I'm a newbie in Go and came from Node.js.
router.GET("/",  IndexGetEndpoint) // works properly
router.Handle("GET", "/", IndexGetEndpoint) // doesn't work
Manu MA
@manucorporat
Jun 04 2015 13:55
@DenisNeustroev oh! I see you are creating two routes to the same path...
you can not do that, what do you want to do?
  1. Registering a route is not idempotent
  2. The muxer ensures that no more than one route matches a given path
DenisNeustroev
@DenisNeustroev
Jun 04 2015 14:06

@manucorporat haha.. you got me wrong

router.GET("/",  IndexGetEndpoint) // works properly
// router.Handle("GET", "/", IndexGetEndpoint)

but

// router.GET("/",  IndexGetEndpoint)
router.Handle("GET", "/", IndexGetEndpoint) // doesn't work

I don't understand why it happens. I was looking for an answer in the source of Gin, but Handle and GET has almost the same code

GET just wrap Handle
Manu MA
@manucorporat
Jun 04 2015 14:11
@DenisNeustroev I just tried it and it works, in fact all the unit tests are passing
go get -u github.com/gin-gonic/gin
try to get the last version
what error are you getting?
DenisNeustroev
@DenisNeustroev
Jun 04 2015 14:20
@manucorporat cannot use IndexGetEndpoint (type func(*gin.Context)) as type []gin.HandlerFunc in argument to r.RouterGroup.Handle
I'm using the latest version of Gin
func IndexGetEndpoint(c *gin.Context) {
    // GetClippings("clippings.txt")
    c.HTML(http.StatusOK, "index.tmpl", gin.H{
        "clippings": "test",
    })
}
func main() {
    r := gin.Default()
    r.LoadHTMLGlob("views/*")
    r.Use(static.Serve("/", static.LocalFile("public", false)))

    // SetRoutes(r)
    // r.GET("/", IndexGetEndpoint)
    r.Handle("GET", "/", IndexGetEndpoint)

    r.Run(":8080")
}
Manu MA
@manucorporat
Jun 04 2015 16:44
nop! you are not using the last version. because that error.
I am not sure, what is going on in your machine... but you are not using the last version. Otherwise you would no get that error.
Manu MA
@manucorporat
Jun 04 2015 16:50
DenisNeustroev
@DenisNeustroev
Jun 04 2015 17:54
OK, thanks, I'll try again later, but I'm not sure that something has changed in 5 h.
Jamie Stackhouse
@itsjamie
Jun 04 2015 18:15
It's because the types are different
You are passing a singular function into something that takes a slice of them
if you want it to work, you'll need to write []gin.HandlerFunc{IndexGetEndpoint}
Although, in the most recent code that got indexed by GoDoc, they all use ...handlers, so that should be a variadic list of gin.HandlerFunc, which should accept the singular form.
So like manucorporat said, you must be using a old version I guess.
Manu MA
@manucorporat
Jun 04 2015 19:45
@DenisNeustroev @itsjamie in the past it was []gin.HandlerFunc{IndexGetEndpoint} but as @itsjamie said, right now it is a variadic list of gin.HandlerFunc
DenisNeustroev
@DenisNeustroev
Jun 04 2015 23:30
@manucorporat @itsjamie thank men, now it works fine :clap: