urlStr := "https://google.com/search?q=golang" parsedUrl, _ := url.Parse(urlStr) fmt.Println(parsedUrl.Scheme) // prints "https" fmt.Println(parsedUrl.Host) // prints "google.com" fmt.Println(parsedUrl.Path) // prints "/search" queryVals := parsedUrl.Query() fmt.Println(queryVals.Get("q")) // prints "golang"
base, _ := url.Parse("https://google.com") relativeUrl, _ := url.Parse("/search?q=golang") fullUrl := base.ResolveReference(relativeUrl) fmt.Println(fullUrl.String()) // prints "https://google.com/search?q=golang"In this example, we create a base URL using url.Parse() and a relative URL using the same method. We then use the ResolveReference() method to construct a full URL from the base and relative URLs. Package Library: net/url.