Restful API例子(Golang)

        RESTful API 这玩意不用多说了,用Go做了个很简单的例子:

               服务端在被调用时返回JSON,

               客户端解析得到相关JSON信息.


服务端源码:

[cpp]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. package main  
  2.   
  3. //简单的JSON Restful API演示(服务端)  
  4. //author: Xiong Chuan Liang  
  5. //date: 2015-2-28  
  6.   
  7. import (  
  8.     "encoding/json"  
  9.     "fmt"  
  10.     "net/http"  
  11.     "time"  
  12. )  
  13.   
  14. type Item struct {  
  15.     Seq    int  
  16.     Result map[string]int  
  17. }  
  18.   
  19. type Message struct {  
  20.     Dept    string  
  21.     Subject string  
  22.     Time    int64  
  23.     Detail  []Item  
  24. }  
  25.   
  26. func getJson() ([]byte, error) {  
  27.     pass := make(map[string]int)  
  28.     pass["x"] = 50  
  29.     pass["c"] = 60  
  30.     item1 := Item{100, pass}  
  31.   
  32.     reject := make(map[string]int)  
  33.     reject["l"] = 11  
  34.     reject["d"] = 20  
  35.     item2 := Item{200, reject}  
  36.   
  37.     detail := []Item{item1, item2}  
  38.     m := Message{"IT""KPI", time.Now().Unix(), detail}  
  39.     return json.MarshalIndent(m, """")  
  40. }  
  41.   
  42. func handler(w http.ResponseWriter, r *http.Request) {  
  43.     resp, err := getJson()  
  44.     if err != nil {  
  45.         panic(err)  
  46.     }  
  47.     fmt.Fprintf(w, string(resp))  
  48. }  
  49.   
  50. func main() {  
  51.     http.HandleFunc("/", handler)  
  52.     http.ListenAndServe("localhost:8085", nil)  
  53. }  
 服务端源码运行后,可在浏览器中执行看下效果:

  

 说明返回是正常的。再用Go写个调用程序测试下:

 

[cpp]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. package main  
  2.   
  3. //简单的JSON Restful API演示(调用端)  
  4. //author: Xiong Chuan Liang  
  5. //date: 2015-2-28  
  6.   
  7. import (  
  8.     "encoding/json"  
  9.     "fmt"  
  10.     "io/ioutil"  
  11.     "net/http"  
  12.     "time"  
  13. )  
  14.   
  15. type Item struct {  
  16.     Seq    int  
  17.     Result map[string]int  
  18. }  
  19.   
  20. type Message struct {  
  21.     Dept    string  
  22.     Subject string  
  23.     Time    int64  
  24.     Detail  []Item  
  25. }  
  26.   
  27. func main() {  
  28.     url := "http://localhost:8085"  
  29.     ret, err := http.Get(url)  
  30.   
  31.     if err != nil {  
  32.         panic(err)  
  33.     }  
  34.     defer ret.Body.Close()  
  35.   
  36.     body, err := ioutil.ReadAll(ret.Body)  
  37.     if err != nil {  
  38.         panic(err)  
  39.     }  
  40.   
  41.     var msg Message  
  42.     err = json.Unmarshal(body, &msg)  
  43.     if err != nil {  
  44.         panic(err)  
  45.     }  
  46.   
  47.     strTime := time.Unix(msg.Time, 0).Format("2006-01-02 15:04:05")  
  48.     fmt.Println("Dept:", msg.Dept)  
  49.     fmt.Println("Subject:", msg.Subject)  
  50.     fmt.Println("Time:", strTime, "\n", msg.Detail)  
  51. }  
  52.   
  53. /* 
  54. //运行结果: 
  55.  
  56. Dept: IT 
  57. Subject: KPI 
  58. Time: 2015-02-28 16:43:11 
  59.  [{100 map[c:60 x:50]} {200 map[d:20 l:11]}] 
  60.  
  61. */  

  从运行结果中可以看到,已正确得到相关JSON信息。 


from : http://blog.csdn.net/xcltapestry/article/category/1728845/1

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值