【问题标题】:How to get value from query localhost:8000/example?q={"user":"admin"} in golang如何从查询 localhost:8000/example 中获取价值?q={"user":"admin"} in golang
【发布时间】:2022-01-24 05:23:29
【问题描述】:

我有来自数据库的数据:

[{ 
    "name": "joseph",
    "user": "admin"
},
{   "name": "george",
    "user": "visitor"
},
{
    "name": "thomas",
    "user": "admin"
}]

我想用 url 查找用户,例如:../testing?q={"user":"admin"}

那么结果数据只有管理员。

【问题讨论】:

  • 你试过什么?包括您的代码。你遇到了什么问题?

标签: mongodb go


【解决方案1】:

首先,您的查询字符串应遵循https://en.wikipedia.org/wiki/Query_string 中提到的格式

// r is *http.Request
r.URL.Query().Get("user")  // this will provide the value of user in the query

这对于您的用例应该足够了,但有关更多详细信息,您可以参考https://golangbyexample.com/net-http-package-get-query-params-golang

【讨论】:

    猜你喜欢
    • 2019-12-14
    • 2021-09-13
    • 2013-08-08
    • 2022-12-29
    • 2013-10-22
    • 2020-08-09
    • 2020-07-21
    • 2010-09-10
    • 1970-01-01
    相关资源
    最近更新 更多