gpt4 book ai didi

go - 为什么Golang URL lib总是插入 '://'?

转载 作者:行者123 更新时间:2023-12-03 10:09:49 24 4
gpt4 key购买 nike

在URL中,授权应该是可选的,这意味着诸如mailto:John.Doe@example.com之类的URL是有效的。
在Golang 1.15.2中,如果使用net/url类创建类似上述的URL,则它似乎不允许未经授权创建URL。
例如。

package main

import (
"fmt"
"net/url"
)

func main() {
var theURL = url.URL{}
theURL.Scheme = "mailto"
theURL.Path = "John.Doe@example.com"
fmt.Println(theURL.String()) // should be mailto:John.Doe@example.com, but is mailto://John.Doe@example.com
}
我在这里缺少什么吗?或者从技术上讲这是一个错误吗?

最佳答案

使用theURL.Opaque而不是theURL.Path。参见https://golang.org/pkg/net/url/#URL

URLs that do not start with a slash after the scheme are interpreted as:

scheme:opaque[?query][#fragment]

Go Playground中的工作代码: https://play.golang.org/p/TFATDQu4PHc

关于go - 为什么Golang URL lib总是插入 '://'?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64564966/

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