0

In an URL, the authority is supposed to be optional, meaning that URLs such as mailto:[email protected] are valid.

In Golang 1.15.2, if using the net/url class to make an URL like above, it does not appear to allow creation of URLs without an authority.

E.g.

package main import ( "fmt" "net/url" ) func main() { var theURL = url.URL{} theURL.Scheme = "mailto" theURL.Path = "[email protected]" fmt.Println(theURL.String()) // should be mailto:[email protected], but is mailto://[email protected] } 

Am I missing something here or is this technically a bug?

1
  • 1
    Definitely suggests it's a bug! Commented Oct 28, 2020 at 0:56

1 Answer 1

5

Use theURL.Opaque instead of theURL.Path. See 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] 

Working code in Go Playground: https://play.golang.org/p/TFATDQu4PHc

Sign up to request clarification or add additional context in comments.

1 Comment

Ah, I can see the logic now. It looks like Path is only useful for schemes where an authority is in use - so a little knowledge of the scheme is needed to know whether to work with Path or Opaque.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.