O'Reilly logo

Go Web Development Cookbook by Arpit Aggarwal

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

How to do it…

  1. Move to $GOPATH/src/my-first-beego-project/controllers and create firstcontroller.go, as follows:
package controllersimport "github.com/astaxie/beego"type FirstController struct {  beego.Controller}type Employee struct {  Id int `json:"id"`  FirstName string `json:"firstName"`  LastName string `json:"lastName"`}type Employees []Employeevar employees []Employeefunc init() {  employees = Employees  {    Employee{Id: 1, FirstName: "Foo", LastName: "Bar"},    Employee{Id: 2, FirstName: "Baz", LastName: "Qux"},  }}func (this *FirstController) GetEmployees() {  this.Ctx.ResponseWriter.WriteHeader(200)  this.Data["json"] = employees  this.ServeJSON()}
  1. Move to $GOPATH/src/my-first-beego-project/routers and edit router.go to add GET mapping /employees ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required