首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Go语言实现复杂Restful Api

Go语言实现复杂Restful Api

作者头像
用户1081422
发布2020-04-08 10:03:56
8580
发布2020-04-08 10:03:56
举报
文章被收录于专栏:T客来了T客来了

章节

  • 程序运行结果
  • 什么是Restful Api?
  • go 实现复杂 Restful Api
  • 感想

0.程序运行结果

1.什么是Restful Api

关于Restful Api 的概念,请参考维基百科 https://zh.wikipedia.org/wiki/%E8%A1%A8%E7%8E%B0%E5%B1%82%E7%8A%B6%E6%80%81%E8%BD%AC%E6%8D%A2

2. go 实现复杂 Restful Api

2.1 go http server 端代码 httpServer.go 实现
package restApi

import (
    "fmt"
    "github.com/gorilla/mux"
    "html"
    "net/http"
)

func fooHandler(w http.ResponseWriter, r *http.Request) {
    fmt.Fprintf(w, "hello,%q", html.EscapeString(r.URL.Path))
}

func doGetApiParam(w http.ResponseWriter, r *http.Request) {
    vars := mux.Vars(r)
    toDoId := vars["toDoId"]
    fmt.Fprintf(w, "toDoId is: "+toDoId)
}

//开启http服务器
func FooHandler() {
    router := mux.NewRouter().StrictSlash(true)
    router.HandleFunc("/foo", fooHandler)
    router.HandleFunc("/hello", fooHandler)
    router.HandleFunc("/todo/{toDoId}", doGetApiParam)
    //返回 api json 数据
    router.HandleFunc("/users", restApiHandler)
    //返回 productsList 数据
    router.HandleFunc("/productsList", productsList)
    //开启http服务,并引入路由管理器
    http.ListenAndServe(":8082", router)
}
2.2 复杂 Api handler 端代码 complexApi.go 实现
package restApi

import (
    "encoding/json"
    "net/http"
)

/**
   1.需求
     编写返回商品列表数据的Api,返回数据对应的 struct (go 中struct为值类型)如下所示
 */

//定义接口返回的核心数据 如商品详情信息
type ProductInfo struct {
    Name         string `json:"name"`         //商品名称
    Des          string `json:"des"`          //商品信息描述
    UseMethod    string `json:"useMethod"`    //使用方法
    UseRange     string `json:"useRange"`     //使用范围
    ValidDate    string `json:"validDate"`    //有效期
    RpbStatement string `json:"rpbStatement"` //权责声明
}

type Data struct {
    ProductsInfo []ProductInfo `json:"productsInfo"`
}

//定义接口返回的data数据
type ApiData struct {
    //[结构体变量名 | 变量类型 | json 数据 对应字段名]
    ErrCode int    `json:"errCode"` //接口响应状态码
    Msg     string `json:"msg"`     //接口响应信息
    Data    Data   `json:"data"`
}

//请求路由(映射)至 productsList() handler
func productsList(w http.ResponseWriter, r *http.Request) {
    //1.接口状态码
    errorCode := 0
    //2.接口返回信息
    msg := "get products list success!"

    product1 := ProductInfo{
        "柿饼",
        "来自陕西富平的特产",
        "开箱即食",
        "全国",
        "2018-10-20 至 2018-12-31",
        "与xx公司无关",
    }

    product2 := ProductInfo{
        "金枪鱼",
        "来自深海的美味",
        "红烧、清蒸均可",
        "全国",
        "2018-10-20 至 2018-12-31",
        "与xx公司无关",
    }

    product3 := ProductInfo{
        "鲶鱼",
        "来自淡水的美味",
        "红烧、清蒸均可",
        "全国",
        "2018-10-20 至 2018-12-31",
        "与xx公司无关",
    }

    //3.省略对应 DB(关系、非关系、搜索引擎) 操作,以 hard code 代替
    data := Data{[]ProductInfo{product1, product2, product3}}
    //4.组装接口返回的数据
    apiData := ApiData{errorCode, msg, data}
    //5.接口响应复杂json数据
    json.NewEncoder(w).Encode(apiData)
}

感想

简单的东西需要记录,刻意练习才能保持精进!

本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2018-10-24,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 T客来了 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 章节
    • 0.程序运行结果
      • 1.什么是Restful Api
        • 2. go 实现复杂 Restful Api
          • 2.1 go http server 端代码 httpServer.go 实现
          • 2.2 复杂 Api handler 端代码 complexApi.go 实现
        • 感想
        领券
        问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档