urlString := "https://example.com/search?q=golang#top" parsedUrl, _ := url.Parse(urlString) fmt.Println(parsedUrl.Scheme) //"https" fmt.Println(parsedUrl.Host) // "example.com" fmt.Println(parsedUrl.Path) // "/search" fmt.Println(parsedUrl.Fragment) //"top" fmt.Println(parsedUrl.Query().Get("q")) // "golang"
urlString := "https://example.com/search?q=golang#top" parsedUrl, _ := url.Parse(urlString) parsedUrl.Path = "/articles" parsedUrl.RawQuery = "q=go%20programming" newUrl := parsedUrl.String() fmt.Println(newUrl) // "https://example.com/articles?q=go%20programming"
urlStruct := &url.URL{ Scheme: "https", Host: "example.com", Path: "/articles", RawQuery: "q=go%20programming", Fragment: "top", } newUrl := urlStruct.String() fmt.Println(newUrl) // "https://example.com/articles?q=go%20programming#top"