Golang net/url.URL.ResolveReference() function example
package net/url
Golang net/url.URL.ResolveReference() function usage example
package main
import (
"fmt"
"net/url"
)
func main() {
baseURL := "http://search.com/?a=b"
parsedURL, err := url.Parse(baseURL)
if err != nil {
panic(err)
}
relativeURL, _ := url.Parse("?q=socketloop.com")
fmt.Println(parsedURL)
fmt.Println("Resolved Reference - Relative : ", parsedURL.ResolveReference(relativeURL))
absoluteURL, _ := url.Parse("c=d")
fmt.Println("Resolved Reference - Absolute : ", parsedURL.ResolveReference(absoluteURL))
absoluteURL2, _ := url.Parse("http://search.com?c=d")
fmt.Println("Resolved Reference - Absolute : ", parsedURL.ResolveReference(absoluteURL2)) // see the diff? use carefully
}
Output :
http://search.com/?a=b
Resolved Reference - Relative : http://search.com/?q=socketloop.com
Resolved Reference - Absolute : http://search.com/c=d
Resolved Reference - Absolute : http://search.com?c=d
References :
Advertisement
Something interesting
Tutorials
+13.9k Golang : How to determine if a year is leap year?
+31.5k Golang : bufio.NewReader.ReadLine to read file line by line
+29.5k Golang : How to create new XML file ?
+20.2k Golang : Determine if directory is empty with os.File.Readdir() function
+6k Golang : Convert Chinese UTF8 characters to Pin Yin
+6.8k Golang : Join lines with certain suffix symbol example
+10.3k Golang : Wait and sync.WaitGroup example
+6.8k Golang : Calculate pivot points for a cross
+48.1k Golang : How to convert JSON string to map and slice
+14.3k Golang : Recombine chunked files example