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
![Admin](/assets/logo_square_200-5d0d61d6853298bd2a4fe063103715b4daf2819fc21225efa21dfb93e61952ea.png)
Author by
Admin
Updated on July 07, 2022Comments
-
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 over 11 yearsOr even the shorter:
url := r.URL.String()
? -
Daniel over 11 years@jdl: True. Thanks for the suggestion. :-)
-
DADi590 almost 5 yearsHow do we do the opposite? (convert from string to an URL)
-
Aspekt almost 5 years@DADi590
u, err := url.Parse(urlString)
-
DADi590 almost 5 years@Aspekt Oh I forgot to put here when I found out how, but thank you anyway! Might help more people!