Getting started
This only work for Beego 1.x. If you are using Beego 2.x, please change language to EN-US.
Installation
Beego contains sample applications to help you learn and use the Beego app framework.
You will need a functioning Go 1.1 installation for this to work.
You will need to install or upgrade Beego and the Bee dev tool:
$ go get -u github.com/astaxie/beego$ go get -u github.com/beego/bee
For convenience, you should add $GOPATH/bin to your $PATH environment variable. Please make sure you have already set the $GOPATH environment variable.
# if you havn't set $GOPATH$ echo 'export GOPATH="$HOME/go"' >> ~/.profile # or ~/.zshrc, ~/.cshrc, whatever shell you use# if you have already set $GOPATH$ echo 'export PATH="$GOPATH/bin:$PATH"' >> ~/.profile # or ~/.zshrc, ~/.cshrc, whatever shell you use$ exec $SHELL
Want to quickly see how it works? Then just set things up like this:
$ cd $GOPATH/src$ bee new hello$ cd hello$ bee run
Windows users:
> cd %GOPATH%/src> bee new hello> cd hello> bee run hello
These commands help you:
- Install Beego into your
$GOPATH. - Install the Bee tool in your computer.
- Create a new application called
hello. - Start hot compile.
Once it’s running, open a browser to http://localhost:8080/.
Simple example
The following example prints Hello world to your browser, it shows how easy it is to build a web application with beego.
package mainimport ("github.com/astaxie/beego")type MainController struct {beego.Controller}func (this *MainController) Get() {this.Ctx.WriteString("hello world")}func main() {beego.Router("/", &MainController{})beego.Run()}
Save file as hello.go, build and run it:
$ go build -o hello hello.go$ ./hello
Open http://127.0.0.1:8080 in your browser and you will see hello world.
What is happening in the scenes of the above example?
- We import package
github.com/astaxie/beego. As we know, Go initializes packages and runs init() in every package (more details), so Beego initializes theBeeAppapplication at this time. - Define the controller. We define a struct called
MainControllerwith an anonymous fieldbeego.Controller, so theMainControllerhas all methods thatbeego.Controllerhas. - Define some RESTful methods. Due to the anonymous field above,
MainControlleralready hasGet,Post,Delete,Putand other methods, these methods will be called when user sends a corresponding request (e.g. thePostmethod is called to handle requests using POST. Therefore, after we overloaded theGetmethod inMainController, all GET requests will use that method inMainControllerinstead of inbeego.Controller. - Define the main function. All applications in Go use
mainas their entry point like C does. - Register routers. This tells Beego which controller is responsible for specific requests. Here we register
MainControllerfor/, so all requests to/will be handed byMainController. Be aware that the first argument is the path and the second one is pointer to the controller you want to register. - Run the application on port 8080 as default, press
Ctrl+cto exit.
Following are shortcut .bat files for Windows users:
Create files step1.install-bee.bat and step2.new-beego-app.bat under %GOPATH%/src.
step1.install-bee.bat:
set GOPATH=%~dp0..go build github.com\beego\beecopy bee.exe %GOPATH%\bin\bee.exedel bee.exepause
step2.new-beego-app.bat:
@echo Set value of APP same as your app folderset APP=coscms.comset GOPATH=%~dp0..set BEE=%GOPATH%\bin\bee%BEE% new %APP%cd %APP%echo %BEE% run %APP%.exe > run.batecho pause >> run.batstart run.batpausestart http://127.0.0.1:8080
Click those two file in order will quick start your Beego tour. And just run run.bat in the future.
