How to convert *url.URL to string in GO, Google App Engine

14,097

The url.URL type has a .String() method.

Try this.

func getURL(w http.ResponseWriter, r *http.Request) {
    url := r.URL.String()
}

http://golang.org/pkg/net/url/#URL.String

Share:
14,097
Admin
Author by

Admin

Updated on July 07, 2022

Comments

  • Admin
    Admin almost 2 years

    I would like to get the URL and convert it to string. I have to following code:

    func getURL(w http.ResponseWriter, r *http.Request) {
        var url string = r.URL
    }
    

    I get this:

    "cannot convert r.URL (type *url.URL) to type string"

    This is working well:

    fmt.Fprint(w,r.URL)
    

    But I would like to use it, not just print it.

    What should I do?

  • jdi
    jdi over 11 years
    Or even the shorter: url := r.URL.String() ?
  • Daniel
    Daniel over 11 years
    @jdl: True. Thanks for the suggestion. :-)
  • DADi590
    DADi590 almost 5 years
    How do we do the opposite? (convert from string to an URL)
  • Aspekt
    Aspekt almost 5 years
    @DADi590 u, err := url.Parse(urlString)
  • DADi590
    DADi590 almost 5 years
    @Aspekt Oh I forgot to put here when I found out how, but thank you anyway! Might help more people!