相关文章推荐
乐观的灭火器  ·  【历史文化】跟着总书记学历史 | ...·  6 月前    · 
英俊的哑铃  ·  迪士尼拟斥资逾671亿收购Hulu剩余33% ...·  7 月前    · 
英勇无比的跑步鞋  ·  专家:太阳风暴是高技术时代的一种富贵病 ――中新网·  1 年前    · 
豪爽的热水瓶  ·  Apple TV+ ...·  1 年前    · 
活泼的足球  ·  湖北咸宁:交通建设提速增效 ...·  1 年前    · 
Code  ›  Go语言实现复杂Restful Api开发者社区
api go语言 restful
https://cloud.tencent.com/developer/article/1610332
憨厚的大脸猫
2 年前
作者头像
用户1081422
0 篇文章

Go语言实现复杂Restful Api

前往专栏
腾讯云
开发者社区
文档 意见反馈 控制台
首页
学习
活动
专区
工具
TVP
文章/答案/技术大牛
发布
首页
学习
活动
专区
工具
TVP
返回腾讯云官网
社区首页 > 专栏 > T客来了 > Go语言实现复杂Restful Api

Go语言实现复杂Restful Api

作者头像
用户1081422
发布 于 2020-04-08 10:03:56
722 0
发布 于 2020-04-08 10:03:56
举报

章节

  • 程序运行结果
  • 什么是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"
     编写返回商品列表数据的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 代替
 
推荐文章
乐观的灭火器  ·  【历史文化】跟着总书记学历史 | 苏门三父子:文章传千古 家风润神州 _家风家训_家风家训家谱_四川省情网
6 月前
英俊的哑铃  ·  迪士尼拟斥资逾671亿收购Hulu剩余33%股权_交易成功_卡斯特_股份
7 月前
英勇无比的跑步鞋  ·  专家:太阳风暴是高技术时代的一种富贵病 ――中新网
1 年前
豪爽的热水瓶  ·  Apple TV+ 憑藉《男孩、鼴鼠、狐狸與馬》榮獲奧斯卡金像獎 - Apple (台灣)
1 年前
活泼的足球  ·  湖北咸宁:交通建设提速增效 区域联通路网升级|咸宁市_新浪财经_新浪网
1 年前
今天看啥   ·   Py中国   ·   codingpro   ·   小百科   ·   link之家   ·   卧龙AI搜索
删除内容请联系邮箱 2879853325@qq.com
Code - 代码工具平台
© 2024 ~ 沪ICP备11025650号