Posted on 2014-02-10 08:43:22 golang
Golang有自带的net/rpc和net/rpc/jsonrpc,前者是基于Gob编码协议的RPC,后者在前者基础上使用JSON编码。
在Python的世界,RESTful的框架比比皆是。由于Golang标准库没有自带,但有许多优秀的第三方库,例如下面要介绍的这个库,go-json-rest:
net/http
类型的Handler来实现,可以方便的和其他Handler合并。下面给出一个例子中的代码:
/* Demonstrate how to use rest.RouteObjectMethod
rest.RouteObjectMethod helps create a Route that points to
an object method instead of just a function.
The Curl Demo:
curl -i -d '{"Name":"Antoine"}' http://127.0.0.1:8080/users
curl -i http://127.0.0.1:8080/users/0
curl -i -X PUT -d '{"Name":"Antoine Imbert"}' http://127.0.0.1:8080/users/0
curl -i -X DELETE http://127.0.0.1:8080/users/0
curl -i http://127.0.0.1:8080/users
*/
package main
import (
"fmt"
"github.com/ant0ine/go-json-rest"
"net/http"
)
func main() {
users := Users{
Store: map[string]*User{},
}
handler := rest.ResourceHandler{
EnableRelaxedContentType: true,
}
handler.SetRoutes(
rest.RouteObjectMethod("GET", "/users", &users, "GetAllUsers"),
rest.RouteObjectMethod("POST", "/users", &users, "PostUser"),
rest.RouteObjectMethod("GET", "/users/:id", &users, "GetUser"),
rest.RouteObjectMethod("PUT", "/users/:id", &users, "PutUser"),
rest.RouteObjectMethod("DELETE", "/users/:id", &users, "DeleteUser"),
)
http.ListenAndServe(":8080", &handler)
}
type User struct {
Id string
Name string
}
type Users struct {
Store map[string]*User
}
func (self *Users) GetAllUsers(w *rest.ResponseWriter, r *rest.Request) {
users := make([]*User, len(self.Store))
i := 0
for _, user := range self.Store {
users[i] = user
i++
}
w.WriteJson(&users)
}
func (self *Users) GetUser(w *rest.ResponseWriter, r *rest.Request) {
id := r.PathParam("id")
user := self.Store[id]
if user == nil {
rest.NotFound(w, r)
return
}
w.WriteJson(&user)
}
func (self *Users) PostUser(w *rest.ResponseWriter, r *rest.Request) {
user := User{}
err := r.DecodeJsonPayload(&user)
if err != nil {
rest.Error(w, err.Error(), http.StatusInternalServerError)
return
}
id := fmt.Sprintf("%d", len(self.Store)) // stupid
user.Id = id
self.Store[id] = &user
w.WriteJson(&user)
}
func (self *Users) PutUser(w *rest.ResponseWriter, r *rest.Request) {
id := r.PathParam("id")
if self.Store[id] == nil {
rest.NotFound(w, r)
return
}
user := User{}
err := r.DecodeJsonPayload(&user)
if err != nil {
rest.Error(w, err.Error(), http.StatusInternalServerError)
return
}
user.Id = id
self.Store[id] = &user
w.WriteJson(&user)
}
func (self *Users) DeleteUser(w *rest.ResponseWriter, r *rest.Request) {
id := r.PathParam("id")
delete(self.Store, id)
}
You really saved my skin with this intfomaoirn. Thanks!
Wonderful extnalapion of facts available here. http://cizshndcb.com [url=http://trfwjan.com]trfwjan[/url] [link=http://rseiwyklgsf.com]rseiwyklgsf[/link]
http://erfolgreichekreditanfrage.pw/kredit-zinseszinsrechner-download-kostenlos-chip.html
Zhheherhrh eg egemtr hrggwe wes egemtrghdfbsgd rwtwrqw
by prednisone w not prescription
coupons for sildenafil viagra pills non prescription viagra viagra best buy sildenafil tablets uk sildenafil tablets 150mg
softtabs cialis
hair loss with tamoxifen Most fertility centers have a criteria that determines when they will cancel the IVF process before egg retrieval.
244 anaesth or anesth or ante natal or antenatal or bleeding or caesarean or cesarean or caesarian or cesarian or caesarien or cesarien or cirrho or eclamp or epidural or fetal or foetal or fetus or foetus or gestational hypertens or glaucom or interocular or intraocular or intracran or intuba or lactation or neonat or new born or newborn or obstetr or ocular or ophthalm or peri partum or peripartum or portal hypertens or portopulmonary or pre eclamp or preeclamp or pregnancies or pregnancy or pregnant or pre natal or prenatal or pulmonary hypertens or pulmonary arterial hypertens or secondary hypertens or spinal long term use of clomid in males 8 and 5 females 15
online gambling casino paypal online casino blackjack game casino games sites best slot machine apps for android keno online indonesia keno online betting internet casino keno online casinos deposit methods