Search⌘ K
AI Features

First Route, API, and HTML Response

Explore how to create a simple GET API route in Beego, link it to a controller handler, and render an HTML response using templates. Understand route definition, controller functions, and template integration to build responsive web pages with Beego.

In this lesson, we will create a simple API that will be routed to a controller function, and a simple HTML template will be rendered as a response.

Defining the route

Let’s create a new API called GET /hello. All the APIs are listed in the file routers/router.go. We will add our API here and map it to its handler, a controller function.

Go (1.18.2)
package routers
import (
"hello_beego/controllers"
"github.com/astaxie/beego"
)
func init() {
beego.Router("/", &controllers.MainController{})
beego.Router("/hello", &controllers.MainController{}, "get:Hello")
}

Line 11: A new route is created. The beego.Router() function is used to route the /hello API to the MainController controller. The handler function is Hello() for the HTTP method GET. ...