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"//parse the URL and make sure there is no error parsing. U, err: =URL. Parse (s)ifErr! =Nil {panic (err)}//Direct access to scheme. FMT. Println (U.scheme)//User contains all of the authentication information, which calls username and Password to get the independent value. FMT. Println (U.user) fmt. Println (U.user.username ()) p, _:=U.user.password () fmt. PRINTLN (P)//host also includes hostname and port information, if the port exists, use 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 to show all the different chunks of the url we extracted. $ go Run url-parsing.go postgresuser:passuserpasshost.com:5432host.com5432/PATHFK=Vmap[k:[v]]v