How it works…

Once the command has executed successfully, the web application will run on the default Beego port 8080.

Next, executing a GET request from the command line will give you a list of all the employees:

$ curl -X GET http://localhost:8080/employees
[
{
"id": 1,
"firstName": "Foo",
"lastName": "Bar"
},
{
"id": 2,
"firstName": "Baz",
"lastName": "Qux"
}
]

Let’s understand the program we have written:

  • import “github.com/astaxie/beego": Here, we imported Beego.
  • type FirstController struct { beego.Controller }: Here, we defined the FirstController struct type, which contains an anonymous struct field of type beego.Controller because of which FirstController automatically acquires all the methods of beego.Controller.
  • func (this *FirstController) GetEmployees() { this.Ctx.ResponseWriter.WriteHeader(200) this.Data["json"] = employees this.ServeJSON() } : Here, we defined the GetEmployees handler, which will execute for every GET request for the URL pattern /employees.
In Go, functions or handlers that start with a capital letter are exported functions, which means they are public and can be used outside the program. That’s the reason we have defined all the functions in our program using a capital letter rather than in camel case.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.15.214.155