package main import ( "fmt" "net/url" ) func main() { u, err := url.Parse("https://www.google.com/search?q=golang") if err != nil { panic(err) } fmt.Println(u.Scheme) fmt.Println(u.Host) fmt.Println(u.Path) fmt.Println(u.RawQuery) }
https www.google.com /search q=golang
package main import ( "fmt" "net/url" ) func main() { u, err := url.Parse("https://www.google.com/search?q=golang") if err != nil { panic(err) } u.Scheme = "http" u.Host = "localhost:8080" u.Path = "/search" u.RawQuery = "q=go" fmt.Println(u.String()) }
http://localhost:8080/search?q=go
package main import ( "fmt" "net/url" ) func main() { u := &url.URL{ Scheme: "https", Host: "www.google.com", Path: "/search", } q := u.Query() q.Set("q", "golang") u.RawQuery = q.Encode() fmt.Println(u.String()) }
https://www.google.com/search?q=golangTherefore, the `net/url` package provides functionality for parsing, modifying and generating URLs.