gpt4 book ai didi

url - 如何将方案和路径添加到 url.URL

转载 作者:IT王子 更新时间:2023-10-29 02:32:13 28 4
gpt4 key购买 nike

我正在尝试通过以下方式更新已解析的 URL:

u, _ := url.Parse(s)
if u.Scheme == "" {
u.Scheme = "https"
}
if u.Path == "" {
u.Path = "api"
}

但事实证明,如果初始字符串缺少 URL 方案(例如 example.com),该字符串将被解析为 URL.Path 而不是 URL .主机。看这个Go playground link .

如何将这个 example.com 变成这个 https://example.com/api

最佳答案

您可以添加支票:

if !strings.HasPrefix(s, "http://") && !strings.HasPrefix(s, "https://") {
s = "https://" + s
}

https://play.golang.org/p/dDK0nx-8x-

关于url - 如何将方案和路径添加到 url.URL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46719948/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com