SexxLuthor
SexxLuthor

Reputation: 4556

Is there a built-in method to get a URL minus the query string?

Is there a built-in method to get the portion of a URL minus the query string? Like http://example.com/ from http://example.com/?search=test?

It's trivial to assemble from the fields of the URL struct (or even by splitting on the question mark char) so I'm not looking for sample code. This is just a simple question to see if it's there in the source/docs and I'm missing it. Thanks!

Upvotes: 0

Views: 86

Answers (2)

SexxLuthor
SexxLuthor

Reputation: 4556

For others who are searching for this information, the correct answer is no, there isn't a built-in method for this, and it can be handled as I described in my question, or as demonstrated above.

Upvotes: 0

fabmilo
fabmilo

Reputation: 48310

No. There is no convenience function for your exact use case.

but, you can use the net/url package to create one:

http://play.golang.org/p/Kk3EPBXMsm

package main

import (
    "fmt"
    "log"
    "net/url"
)

func main() {
    result, err := url.Parse("http://example.com/?search=test?")
    if err != nil {
        log.Fatal("Invalid url", err)
    }
    fmt.Println(result.Scheme+"://"+result.Host+result.Path)
    // or
    result.RawQuery = ""

    fmt.Println(result)
}

Upvotes: 2

Related Questions