Golang URL Parsing Common code

Source: Internet
Author: User
Tags url example
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 
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.