golang ParseQuery url给了我错误的输出

golang ParseQuery url给了我错误的输出,go,Go,我有以下代码: func init() { today := time.Now() // If ENDPOINT is empty, It'll use this hardcoded endpoint. The ENDPOINT variable should not contain any text after "ModifiedDate gt". The actual date is currentDay-1 if ENDPOINT == "" {

我有以下代码:

func init() {
    today := time.Now()

    // If ENDPOINT is empty, It'll use this hardcoded endpoint. The ENDPOINT variable should not contain any text after "ModifiedDate gt". The actual date is currentDay-1
    if ENDPOINT == "" {
        ENDPOINT = "http://localhost:8000/Contacts/Export/?$select=Firstname,Lastname,Email,SubaccountId&$filter=EEA eq '' and ModifiedDate gt"
    }

    // Append CurrentDay-1 in YYY`enter code here`Y-MM-DDTHH:MM:SSZ format.
    // The time is NOT in UTC. It's the local time of the machine on which lambda function was running
    ENDPOINT = fmt.Sprintf("%s %s", ENDPOINT, today.AddDate(0, 0, -1).Format("2006-01-02T15:04:05Z"))

    var err error
    // parse the url
    PARSED_ENDPOINT, err = url.Parse(ENDPOINT)
    if err != nil {
        log.Fatalln("Invalid $ENDPOINT", err)
    }

    // parse the query parameters
    parsedQueryParams, err := url.ParseQuery(PARSED_ENDPOINT.RawQuery)
    if err != nil {
        log.Fatalln("error in parsing query parameters", err)
    }

    // URLEncode query parameters
    PARSED_ENDPOINT.RawQuery = parsedQueryParams.Encode()
}
当我输出URL时,我得到:

'http://localhost:8000/Contacts/Export/?%24filter=EEA+eq+%27%27+and+ModifiedDate+gt+2018-10-22T08%3A45%3A45Z&%24select=Email%2CFirstname%2CLastname%2CSubaccountId%2CEEA'
如何返回:

'http://localhost:8000/Contacts/Export/?$filter=EEA%20eq%20%27%27%20and%20ModifiedDate%20gt%202018-10-22T00:00:00Z&$select=Email,Firstname,Lastname,SubaccountId,EEA'

非常感谢任何建议

Golang提供url包来管理此问题,并将带有键值的查询字符串传递给浏览器,并在编码字符串后对其进行相应解析,这将解决特殊字符问题:

package main

import (
    "fmt"
    "net/url"
)

func main() {
    query := make(url.Values)
    query.Add("key", "value")
    url := &url.URL{RawQuery: query.Encode(), Host: "foo", Scheme: "http"}
    fmt.Println(url)
}

避免使用字符串查询和使用fmt package Sprintf方法添加值。这不是管理查询字符串和创建动态url的正确方法。

这是因为您的查询包含编码时在%20中转换的空格。如果我使用postman来测试我的查询,它会正确转换它,逗号也被改为
%2C
是的,邮递员很聪明。您应该在url内传递查询。Query@khinesteryou应该删除dupl只是为了提供一些反馈:Go的标准库使用camel case over snake case或all caps const等,这是声明变量的推荐方法:此外,您可能希望在代码中使用
main()
,而不是
init()
as主要用于确保程序执行前的初始状态。