This is a creation in Article, where the information may have evolved or changed.
Package Mainimport "FMT" import "Net/url" import "Strings" Func Main () {//We will parse this URL example, which contains a scheme, authentication information, hostname, port, PATH, Query parameters and fragments. S: = "postgres://user:pass@host.com:5432/path?k=v#f"//resolves this URL and ensures that parsing is not an error. U, err: = URL. Parse (s) if err! = Nil {panic (err)}//Direct access scheme. Fmt. Println (U.scheme)//user contains all the authentication information, username and Password are called here to get the independent values. Fmt. Println (U.user) fmt. Println (U.user.username ()) p, _: = U.user.password () fmt. PRINTLN (p)//host also includes host name and port information, such as the presence of the port, using strings. Split () extracts the port manually from Host. Fmt. PRINTLN (u.host) H: = Strings. Split (U.host, ":") fmt. Println (H[0]) fmt. Println (h[1])//Here we present the path and query fragment information. Fmt. Println (U.path) fmt. PRINTLN (u.fragment)//To get the query parameters in the string k=v this format, you can use the Rawquery function. You can also parse the query parameters into a map. The parsed query parameter map takes the query string as the key, the corresponding value string slice is the value, so how to want only one key corresponding to the first value, the index position is set to [0] on the line. Fmt. Println (U.rawquery) m, _: = URL. Parsequery (u.rawquery) fmt. Println (m) fmt. Println (m["K"][0])}//run our URL resolver, showing all the different chunks of the url we extracted. $ go Run url-parsing.go postgresUser:passuserpasshost.com:5432host.com5432/pathfk=vmap[k:[v]]v