Hello World with Beego

Hello World with Beego

Beego is a web framework written in Golang. Beego is used for rapid development of enterprise application in Go, including RESTful APIs, web apps and backend services. In this article, I will let you know how you can run your first rest api with Beego in very few steps.

Step 1: create a directory with name of your choice for example: beego-hello-world

Step 2: Open this directory in VSCode. I am using VSCode, you can use any other code editor of your choice. Now, open terminal and run below command to create go module.
go mod init beego-hello-world

Step 3: Create a file main.go in this directory and paste below code.

package main

import "github.com/astaxie/beego"

func main() {

	// create contr
	ctrl := &MainController{}

	// POST http://localhost:8080/ => ctrl.HelloWorld()
	beego.Router("/", ctrl, "get:HelloWorld")

	beego.Run()
}

type MainController struct {
	beego.Controller
}

func (ctrl *MainController) HelloWorld() {
	ctrl.Data["json"] = "hello world"
	ctrl.ServeJSON()
}

Step 4: You need to get Beego framework from repository to run this code. So use command to get it (in terminal).
go get github.com/astaxie/beego

Step 5: Now you are ready to run your first go code with Beego framework. Use command go run main.go to run your first application.

This will start a local server at your machine at port 8080. To test your application, open browser and type localhost:8080. you will get below output at your browser.

Leave a Reply

Your email address will not be published. Required fields are marked *