* url.URL类型没有字段或方法ParseRequestURI

here's my code:

director := func(req *http.Request) {
    fmt.Println(req.URL)

    regex, _ := regexp.Compile(`^/([a-zA-Z0-9_-]+)/(\S+)$`)
    match := regex.FindStringSubmatch(req.URL.Path)
    bucket, filename := match[1], match[2]
    method := "GET"
    expires := time.Now().Add(time.Second * 60)

    signedUrl, err := storage.SignedURL(bucket, filename, &storage.SignedURLOptions{
        GoogleAccessID: user.GoogleAccessID,
        PrivateKey: []byte(user.PrivateKey),
        Method: method,
        Expires: expires,
    })
    if err != nil {
        fmt.Println("Error " + err.Error())
    }
    fmt.Println(signedUrl)
    req.URL.ParseRequestURI(signedUrl)
}

I want to parse signedUrl to req.URL using ParseRequestURI method https://golang.org/pkg/net/url/#ParseRequestURI

But when compiling, it throws an error: req.URL.ParseRequestURI undefined (type *url.URL has no field or method ParseRequestURI)

So I tried req.URL.Parse and it works. https://golang.org/pkg/net/url/#Parse

These two functions are close with each other in the documentation. I can't find any significant difference(s) between them. So I don't know why one works and the other doesn't.

How can I make ParseRequestURI work? And why one works but the other doesn't?

As you mentioned following function call is not working:

req.URL.ParseRequestURI(signedUrl)

because:

func ParseRequestURI(rawurl string) (*URL, error)

is defined under net/url package as a package level function (reference), thus cannot be called using a type. Though the correct way to call it as below:

url.ParseRequestURI(signedUrl) // Here 'url' is from package name i.e. 'net/url'

On the other hand, as you mentioned you are successfully able to call req.URL.Parse because Parse is defined both at package level i.e. at 'net/url' (reference) as well as at type level for type *URL (reference).

Parse at package net/url is defined as:

func Parse(rawurl string) (*URL, error)

Parse parses rawurl into a URL structure.

The rawurl may be relative (a path, without a host) or absolute (starting with a scheme). Trying to parse a hostname and path without a scheme is invalid but may not necessarily return an error, due to parsing ambiguities.

Parse for the type *URL is defined as:

func (u *URL) Parse(ref string) (*URL, error)

Parse parses a URL in the context of the receiver. The provided URL may be relative or absolute. Parse returns nil, err on parse failure, otherwise its return value is the same as ResolveReference.

I hope this helps you.