Functional Style

This style is closer to the syntax of Go itself, so we tend to recommend using this route registration method.

Example:

  1. func main() {
  2. web.Get("/hello", func(ctx *context.Context) {
  3. ctx.WriteString("hello, world")
  4. })
  5. web.Run()
  6. }

Note that inside the functional style of writing, we only need to provide a method that uses the *context.Context argument. This context is not GO’s context package, but Beego’s context package.

Or:

  1. func main() {
  2. web.Post("/post/hello", PostHello)
  3. web.Run()
  4. }
  5. func PostHello(ctx *context.Context) {
  6. ctx.WriteString("hello")
  7. }

All APIs:

  1. Get(rootpath string, f HandleFunc)
  2. Post(rootpath string, f HandleFunc)
  3. Delete(rootpath string, f HandleFunc)
  4. Put(rootpath string, f HandleFunc)
  5. Head(rootpath string, f HandleFunc)
  6. Options(rootpath string, f HandleFunc)
  7. Patch(rootpath string, f HandleFunc)
  8. Any(rootpath string, f HandleFunc)

Suggestions

One of the difficulties to face when using a functional registration approach is how to organize these methods.

In the controller style, all methods are naturally split by the controller. For example, in a simple system, we can have a UserController, and then all the methods related to User can be defined in this Controller; all the methods related to orders can be defined in the OrderController.

So from this point of view, Controller provides a natural way to organize these methods.

Well, in the functional style, we lack such a design.

An intuitive way is to organize them by files. For example, all user-related ones in their own project api/user.go.

If there are more methods, then one should further consider organizing by packages, which is also recommended. For example, creating a user directory under api, where all the methods for handling user-related requests are defined.

The last way, is to define a Controller by itself, but this Controller only has the effect of organizing the code, and is not the kind we emphasize inside the controller style.

For example, we can write a Controller without using web.Controller:

  1. type UserController struct {
  2. }
  3. func (ctrl UserController) AddUser(ctx *context.Context) {
  4. // you business code
  5. }

This Controller cannot be used for controller style reason registration. It’s just to provide a similar way to organize the code.

Reference

route rules

namespace