duanhao4909 2015-10-19 21:45
浏览 41
已采纳

如何在Gorilla Mux中列出所有变量?

When you create a funcHandler in Golang and use Gorilla Mux, I know that you can access a specific input variable by calling Mux.Vars. However, I'm not sure how that works when you have data stored in JSON format, and part of that is because I'm not sure how Mux.Vars() works. So, I'd like to know how to list all variables stored by Mux.Vars() when you enter a funcHandler and how to parse JSON stored in a URL (ie, /data?name="bill"&value="red", where I would want to find the value of the name and value keys).

  • 写回答

1条回答 默认 最新

  • douchi0028 2015-10-20 11:11
    关注

    For list all Gorilla Mux:

    for k, v := range mux.Vars(request) {
        log.Printf("key=%v, value=%v", k, v)
    }
    

    The Vars function return a map, for range loop help you read all the items like I show you.
    But I think your question is a bit different, if you want to read the JSON data or other kid of data that send with the request, you need to read the request Body (req.Body). Note that request Body is a Reader interface not a string. An example if you expect the input in JSON format:
    Handling JSON Post Request in Go

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 unity第一人称射击小游戏,有demo,在原脚本的基础上进行修改以达到要求
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)