美文网首页
go mux子路由的基本使用

go mux子路由的基本使用

作者: 十维的想象 | 来源:发表于2021-12-16 09:50 被阅读0次

代码示例:

package main

import (
    "encoding/json"
    "github.com/gorilla/mux"
    "net/http"
)

// 定义Product的结构体
type Product struct {
    Name  string  `json:"name"`
    Price float64 `json:"price"`
    Num   int32
    Desc  string
}

// 定义Shop的结构体
type Shop struct {
    Name    string `json:"name"`
    Address string `json:"address"`
}

// 初始化Product和Shop的切片
var productlist []Product
var shoplist []Shop

// 获取Product列表
func getProductListHandler(w http.ResponseWriter, r *http.Request) {
    // Header设置Content-Type为json格式,输出json格式的结果,看起来更方便
    w.Header().Set("Content-Type", "application/json")
    w.WriteHeader(http.StatusOK)
    json.NewEncoder(w).Encode(productlist)
}

// 获取Shop列表
func getShopListHandler(w http.ResponseWriter, r *http.Request) {
    // Header设置Content-Type为json格式,输出json格式的结果,看起来更方便
    w.Header().Set("Content-Type", "application/json")
    w.WriteHeader(http.StatusOK)
    json.NewEncoder(w).Encode(shoplist)
}

func main() {
    // 初始化ProductList的数据
    productlist = append(productlist, Product{Name: "地球仪", Price: 10, Num: 102, Desc: "世界地球仪,360度旋转"})
    productlist = append(productlist, Product{Name: "鱼缸", Price: 28.8, Num: 2, Desc: "长50cm,宽28cm,高60cm"})
    // 初始化ShopList的数据
    shoplist = append(shoplist, Shop{Name: "苹果太古里店", Address: "成都太古里"})
    shoplist = append(shoplist, Shop{Name: "优衣库三里屯店", Address: "北京三里屯"})

    // 初始化一个新的路由
    r := mux.NewRouter()

    // Product的子路由
    p := r.PathPrefix("/products").Subrouter()
    p.HandleFunc("/list", getProductListHandler).Methods("GET")
    // Shop的子路由
    s := r.PathPrefix("/shop").Subrouter()
    s.HandleFunc("/list", getShopListHandler).Methods("GET")
    // 监听8001端口
    http.ListenAndServe(":8001", r)
}

运行程序,通过postman或其它http请求工具发送请求,下面是测试的结果。
1、GET请求http://localhost:8001/products/list,结果:
[
    {
        "name": "地球仪",
        "price": 10,
        "Num": 102,
        "Desc": "世界地球仪,360度旋转"
    },
    {
        "name": "鱼缸",
        "price": 28.8,
        "Num": 2,
        "Desc": "长50cm,宽28cm,高60cm"
    }
]
2、GET请求http://localhost:8001/shop/list,结果:
[
    {
        "name": "苹果太古里店",
        "address": "成都太古里"
    },
    {
        "name": "优衣库三里屯店",
        "address": "北京三里屯"
    }
]

以上就是mux子路由的基本使用,git地址:https://gitee.com/sunzf/test-go/blob/master/src/mux/Products.go

相关文章

  • go mux子路由的基本使用

    代码示例: 运行程序,通过postman或其它http请求工具发送请求,下面是测试的结果。 1、GET请求http...

  • mux 源码分析

    前言 mux 是go实现的轻量的路由,可以基于host,path,query匹配。 使用示例: 源码实现 Rout...

  • mux源码解读

    mux简介 go语言中一个强大的web路由,在这里我准备理解其原理后实现一个python版的mux,同时也为了进一...

  • golang路由mux的介绍及基本使用

    github地址: https://github.com/gorilla/mux#matching-routes[...

  • 三、GO Web编程示例 - 路由(使用gorilla/mux)

    前言 Go的net/http包为HTTP协议提供了许多功能。但有一件事做得不是很好,那就是复杂的请求路由,例如将请...

  • Rboot 文档--路由处理器

    简介 rboot 内置了一个简单的路由处理器,可以帮助开发者统一管理路由,它的底层使用的是 gorilla/mux...

  • mux路由中间件的使用

    mux路由中间件的使用 代码示例: 运行程序,通过postman或其它http请求工具发送请求,下面是测试的结果。...

  • Golang 第三方库学习 · mux

    本文为转载,原文:Golang 第三方库学习 · mux 介绍 mux 是一个用来执行http请求的路由和分发的第...

  • 2018-11-22

    路由的基本使用 1 路由的基本使用2 再路由注册再跟实例的时候我们可以全局的访问路由,this.$router或者...

  • vue-router

    前端路由的基本原理 vue-router的基本使用 命名路由 路由参数 嵌套路由

网友评论

      本文标题:go mux子路由的基本使用

      本文链接:https://www.haomeiwen.com/subject/jpnmfrtx.html