tul
tul

Reputation: 1779

Why goes Golang URL lib always insert '://'?

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?

Upvotes: 0

Views: 241

Answers (1)

blami
blami

Reputation: 7401

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

Upvotes: 5

Related Questions