Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/url/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Golang:将URL作为GET参数传递_Url_Go_Get_Parameter Passing_Url Parameters - Fatal编程技术网

Golang:将URL作为GET参数传递

Golang:将URL作为GET参数传递,url,go,get,parameter-passing,url-parameters,Url,Go,Get,Parameter Passing,Url Parameters,我想获取一个URL作为获取参数 例如:example.com?domain=site.come?a=val&b=val 当我使用 query := r.URL.Query() domain := query.Get("domain") 要获得域名,只需domain=site.come?a=val 我想,因为当r.URL.Quices()满足和时,它被认为是一个新的参数 有人知道我如何解决这个问题吗 提前谢谢。您需要对查询字符串进行URL编码: 输出domain=example.com%3Ff

我想获取一个URL作为获取参数

例如:example.com?domain=site.come?a=val&b=val

当我使用

query := r.URL.Query()
domain := query.Get("domain") 
要获得域名,只需domain=site.come?a=val

我想,因为当r.URL.Quices()满足<强>和<强>时,它被认为是一个新的参数

有人知道我如何解决这个问题吗


提前谢谢。

您需要对查询字符串进行URL编码:

输出
domain=example.com%3Ffoo%3Dbar

您可以将该字符串设置为一个值的
RawQuery
,然后如果您像以前那样访问该查询,它将具有正确的值

如果URL编码正确,则您应该能够使用URL值运行,并获得正确的结果:

package main

import (
    "fmt"
    "net/url"
)

func main() {
    query := make(url.Values)
    query.Add("domain", "example.com?foo=bar&abc=123&jkl=qwe")

    url := &url.URL{RawQuery: query.Encode(), Host: "domain.com", Scheme: "http"}
    fmt.Println(url.String())

    abc := url.Query().Get("domain")
    fmt.Println(abc)
}
这张照片是:

http://domain.com?domain=example.com%3Ffoo%3Dbar%26abc%3D123%26jkl%3Dqwe
(带有编码参数“domain”的完整URI)


(所述参数的解码值)

我不是发送Url的人,我只是从另一个服务获得的,然后你必须使用你拥有的内容(剪切
domain=site.come?a=val
)或者打电话给另一个人,告诉他修复他的代码,因为他的查询字符串无效。他做了更改,对url进行了编码,但当我得到它给我的域时仍然存在相同的问题,只有domain=site。来吧?a=val没有&b=valI我编辑了我的答案。另一个家伙可能是手工编码的,忘了编码
&
。这是可行的,但我用了另一个,他只是把域放在[]之间,它工作得很好
http://domain.com?domain=example.com%3Ffoo%3Dbar%26abc%3D123%26jkl%3Dqwe
example.com?foo=bar&abc=123&jkl=qwe