Mastering Beego Controllers in Go: From Basics to Advanced Methods
This guide explains how Beego's Controller component works within the MVC architecture, shows how to define and embed a controller in Go, demonstrates common request‑handling methods, and walks through the underlying source code and interface definitions for full backend control.
Beego Controller Overview
In Beego’s MVC architecture the Controller type handles HTTP requests, mediates between models and view templates, and provides a lifecycle of hooks.
Creating a Controller
Define a struct that anonymously embeds beego.Controller and import the package github.com/astaxie/beego (or github.com/beego/beego/v2/server/web for newer versions).
type MyController struct {
beego.Controller
}Typical Handler Implementation
Implement HTTP‑method functions (e.g., Get) on the struct. Inside the method you can populate c.Data (a map[interface{}]interface{}) with values that the template can use, and set c.TplName to the template file.
package controllers
import (
"github.com/astaxie/beego"
)
type MainController struct {
beego.Controller
}
// GET / route
func (c *MainController) Get() {
c.Data["Website"] = "beego.me"
c.Data["Email"] = "[email protected]"
c.TplName = "index.tpl"
}If beego.AutoRender is true (default), Beego automatically calls Render() after the handler finishes, so the template is rendered without an explicit call.
Direct Response
When a template is not needed you can write directly to the response body:
func (c *MainController) Get() {
c.Ctx.WriteString("hello")
}Controller Core Structure
type Controller struct {
// request/response context
Ctx *context.Context
Data map[interface{}]interface{}
// routing metadata
controllerName string
actionName string
methodMapping map[string]func()
gotofunc string
AppController interface{}
// template configuration
TplName string
ViewPath string
Layout string
LayoutSections map[string]string
TplPrefix string
TplExt string
EnableRender bool
// XSRF protection
_xsrfToken string
XSRFExpire int
EnableXSRF bool
// session store
CruSession session.Store
}ControllerInterface
type ControllerInterface interface {
Init(ct *context.Context, controllerName, actionName string, app interface{})
Prepare()
Get()
Post()
Delete()
Put()
Head()
Patch()
Options()
Finish()
Render() error
XSRFToken() string
CheckXSRFCookie() bool
HandlerFunc(fn string) bool
URLMapping()
}Lifecycle Hooks
Init : called by the framework before any request handling; it sets Ctx, controller name, action name, initializes Data, and stores a reflected reference to the concrete controller.
Prepare : runs after Init and before the specific HTTP‑method handler; useful for authentication, logging, or request preprocessing.
Get / Post / Delete / Put / Head / Patch / Options : correspond to the respective HTTP verbs. The default implementation returns HTTP 405; overriding them provides custom logic.
Finish : executed after the HTTP‑method handler (and after Render if enabled); ideal for resource cleanup such as closing DB connections.
Render : renders the template identified by TplName when EnableRender (or global beego.AutoRender) is true.
Template Resolution
If TplName is not set, Beego falls back to a file named controller/<lowercase‑method>.tpl under the view directory (e.g., maincontroller/get.tpl).
References
Beego source repository:
github.com/astaxie/beegoGo Development Architecture Practice
Daily sharing of Golang-related technical articles, practical resources, language news, tutorials, real-world projects, and more. Looking forward to growing together. Let's go!
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
