我使用Python工作已经有几年了,最近开始了一个关于GO的调查,主要看作是一个缓解瓶颈的实验,还没有大规模web服务器部署。

我用不同语言写了一个简单的REST服务,使用ab工具检测响应速度。

Python

server.py

01frombottle importroute,
 run0203@route('/')04defhome():05    article ={'name': 'A
 Royal Baby', 'body':'A
 slow news week'}06    returnarticle0708defmain():09    run(host='localhost',
 port=8081)1011if__name__ =='__main__':12    main()

Go

server.go

01package
 main0203import
 (04    "encoding/json"05    "fmt"06    "github.com/emicklei/go-restful"07    "io"08    "net/http"09)1011func
 main() {12    ws
 := new(restful.WebService)13    ws.Route(ws.GET("/").To(hello))14    restful.Add(ws)15    fmt.Print("Server
 starting on port 8080\n")16     http.ListenAndServe(":8080",
 nil)17}1819func
 hello(req *restful.Request, resp *restful.Response) {20    article
 := Article{"A
 Royal Baby", "A
 slow news week"}21    b,
 _ := json.Marshal(article)22    io.WriteString(resp,
 string(b))23}2425type
 Article struct{26    Name
 string27    Body
 string28}