我試圖更新解析URL的方式如下:如何添加方案和路徑url.URL
u, _ := url.Parse(s)
if u.Scheme == "" {
u.Scheme = "https"
}
if u.Path == "" {
u.Path = "api"
}
但事實證明,如果初始字符串缺少URL方案(例如example.com
)該字符串被解析爲URL.Path
而不是URL.Host
。看到這個Go playground link。
如何將此example.com
轉換爲此https://example.com/api
?