This is a creation in Article, where the information may have evolved or changed.
Getting Started with API application development
Go is great for developing API apps, and I think it's the biggest advantage of go relative to other dynamic languages. Beego provides a very powerful and fast tool for developing API applications, allowing users to quickly build API application prototypes and concentrate on business logic.
Quickly build prototypes
The Bee Rapid development tool provides an API application built-in tool that allows you to quickly build an API application by executing the following command in any directory under GOPATH/SRC:
Bee API Beeapi
The directory structure of the app
The directory structure of the app is as follows:
├──conf│ └──app.conf├──controllers│ └──default.go├──models│ └──object.go└──main.go
SOURCE parsing
- The main API configuration for app.conf is as follows:
Autorender = False//API application does not require template rendering, so turn off automatic rendering
Copyrequestbody = True//restful when the app sends the message it is the raw body, not the normal form form, so you need to read the body information extra
- Main.go files are primarily for restful routing registrations
Beego. Restrouter ("/object", &controllers. obejctcontroller{})
This route can match the following rules
| URL |
HTTP Verb |
functionality |
| /object |
POST |
Creating Objects |
| /object/objectid |
GET |
Retrieving Objects |
| /object/objectid |
PUT |
Updating Objects |
| /object |
GET |
Queries |
| /object/objectid |
DELETE |
Deleting Objects |
- The Obejctcontroller implements the corresponding method:
Type Obejctcontroller struct { beego. Controller}func (This *obejctcontroller) Post () {}func (this *obejctcontroller) Get () {}func (this *obejctcontroller) Put () {}func (this *obejctcontroller) Delete () {}
- Models inside to achieve the corresponding operation of the object of adding and removing and other operations
Test
Add an object:
Curl-x post-d ' {"score": 1337, "playername": "Sean Plott"} ' Http://127.0.0.1:8080/object
Returns a corresponding objectid:astaxie1373349756660423900
Querying an Object
Curl-x GET http://127.0.0.1:8080/object/astaxie1373349756660423900
Querying all of the objects
Curl-x GET Http://127.0.0.1:8080/object
Update an Object
Curl-x put-d ' {"Score": 10000} ' http://127.0.0.1:8080/object/astaxie1373349756660423900
Delete an Object
Curl-x DELETE http://127.0.0.1:8080/object/astaxie1373349756660423900