Routing

Before introducing routing, you need to understand the concept of Handler and Context.

Firstly, we need create a router. Let’s take Application as example.

router := clevergo.New()

GET

router.Get("/", index) // GET /
router.Get("/hello", hello) // GET /hello
router.Get("/users", queryUsers) // GET /users
router.Get("/users/:id", queryUser) // GET /users/1, GET /users/2

POST

router.Post("/users", createUser) // POST /users

PUT

router.Put("/users/:id", updateUser) // PUT /users/1, PUT /users/2

PATCH

router.Patch("/users/:id", updateUser) // PATCH /users/1, PATCH /users/2

DELETE

router.Delete("/users/:id", deleteUser) // DELETE /users/1, DELETE /users/2

OPTIONS

router.Options("/users", options) // OPTIONS /users
router.Options("/users/:id", options) // OPTIONS /users/1, OPTIONS /users/2
router.Head("/users", head) // HEAD /users

Any

Any matches ALL request methods.

// GET /posts
// POST /posts
// ...
router.Any("/posts", postHandler)

// GET /posts/1
// POST /posts/1
// DEELTE /posts/1
// PUT /posts/1
// PATCH /posts/1
// ...
router.Any("/posts/:id", postHandler)

Static Files

router.ServeFiles("/assets", http.Dir("/path/to/assets"))

Other

You can also register handler for other HTTP request methods via Handle.

router.Handle(http.MethodGet, "/", index) // equals to router.Get
comments powered by Disqus