如何将多个字符串传递到http get中的url中?

时间:2018-08-21 16:13:01

标签: http go

这是我当前的代码:

var dek string = "dk"
resp,err := c.Get("https://google."VALUEHERE"")

如果我需要一堆不同的字符串,我希望能够将不同的字符串传递到我的网址中。

所以理想的情况是:

resp,err := c.Get("https://google.dk/value1=%v&value2=%v", value1, value2)

这有可能吗?

1 个答案:

答案 0 :(得分:0)

使用net/url package构建不需要编码的字符串:

interface test {
  req: string
  opt: string
  [k: string]: any
}
type FirstHalf<T> = {
  [K in keyof T]: string extends K ? never : number extends K ? never : K
}

type ValuesOf<T> = T extends { [_ in keyof T]: infer U } ? U : never
// or equivalently, since T here, and T in FirstHalf have the same keys,
// we can use T from FirstHalf instead:
type SecondHalf<First, T> = First extends { [_ in keyof T]: infer U } ? U : never;

type a = FirstHalf<test>
//Output:
type a = {
    [x: string]: never;
    req: "req";
    opt?: "opt" | undefined;
}
type a2 = ValuesOf<a> //  "req" | "opt" // Success!
type a2b = SecondHalf<a, test> //  "req" | "opt" // Success!

// Substituting, to create a single type definition, we get @ferdaber's solution:
type KnownKeys<T> = {
  [K in keyof T]: string extends K ? never : number extends K ? never : K
} extends { [_ in keyof T]: infer U } ? U : never;
// type b = KnownKeys<test> //  "req" | "opt" // Absolutely glorious!

使用enter image description here构建URL,以便对其进行正确编码:

hostname := fmt.Sprintf("google.%s", "dk")
// => "google.dk"