golang tutorial - Url Parsing In Golang - golang - go programming language



Url Parsing In Golang

  • A Uniform Resource Locator (URL), normally termed a web address, is a reference to a web resource that specifies its location on a computer network and a mechanism for retrieving it. A URL is a specific type of Uniform Resource Identifier (URI), although many people use the two terms interchangeably. URLs occur most commonly to reference web pages (http), but are also used for file transfer (ftp), email (mailto), database access (JDBC), and many other applications.
  • Most web browsers display the URL of a web page above the page in an address bar. A typical URL could have the form
    • http://www.wikitechy.com/index.html,
  • which indicates a
    • protocol (http),
    • a hostname (www.wikitechy.com),
    • and a file name (index.html).
  • Every HTTP URL conforms to the syntax of a generic URI. A generic URI is of the form: scheme:[//[user[:password]@]host[:port]][/path][?query][#fragment] It comprises:
    • The scheme, consisting of a sequence of characters beginning with a letter and followed by any combination of letters, digits, plus (+), period (.), or hyphen (-). Although schemes are case-insensitive, the canonical form is lowercase and documents that specify schemes must with lowercase letters.
    • It is followed by a colon (:). Examples of popular schemes include http(s), ftp, mailto, file, data, and irc.
    • Two slashes (//): This is required by some schemes and not required by some others.
    • An authority part, comprising:
      • An optional authentication section of a user name and password, separated by a colon, followed by an at symbol (@)
      • A "host", consisting of either a registered name (including but not limited to a hostname), or an IP address. IPv4 addresses must be in dot-decimal notation, and IPv6 addresses must be enclosed in brackets
      • An optional port number, separated from the hostname by a colon
      • A path, which contains data, usually organized in hierarchical form, that appears as a sequence of segments separated by slashes.
  • A web browser will usually dereference a URL by performing an HTTP request to the specified host, by default on port number 80.
  • URLs using the https scheme require that requests and responses will be made over a secure connection to the website.
 url parsing in golang

Read Also

Golang Parser

go parsing details

  • Parsing, syntax analysis or syntactic analysis is the process of analysing a string of symbols, either in natural language or in computer languages, conforming to the rules of a formal grammar.
  • The term has slightly different meanings in different branches of linguistics and computer science.
  • Traditional sentence parsing is often performed as a method of understanding the exact meaning of a sentence or word, sometimes with the aid of devices such as sentence diagrams.
  • It usually emphasizes the importance of grammatical divisions such as subject and predicate.
  • Within computational linguistics the term is used to refer to the formal analysis by a computer of a sentence or other string of words into its constituents, resulting in a parse tree showing their syntactic relation to each other, which may also contain semantic and other information.
  • Within computer science, the term is used in the analysis of computer languages, referring to the syntactic analysis of the input code into its component parts in order to facilitate the writing of compilers and interpreters. The term may also be used to describe a split or separation.
 process of url parsing

Example : Parsing the Expression 1 + 2 * 3

Rule Expressions Description
Rule1: E ? E + E Expression is the sum of two expressions.
Rule2: E ? E * E Expression is the product of two expressions.
Rule3: E ? number Expression is a simple number
Rule4: + has less precedence than * + has less precedence than *

url parsing - go program

package main

	import "fmt"
import "net"
import "net/url"

	func main() {
// We’ll parse this example URL, which includes a scheme, authentication info, host, port, path, query params, and query fragment.
	    s := "postgres://user:pass@host.com:5432/path?k=v#f"
// Parse the URL and ensure there are no errors.
	    u, err := url.Parse(s)
    if err != nil {
        panic(err)
    }
// Accessing the scheme is straightforward.
	    fmt.Println(u.Scheme)
// User contains all authentication info; call Username and Password on this for individual values.
	    fmt.Println(u.User)
    fmt.Println(u.User.Username())
    p, _ := u.User.Password()
    fmt.Println(p)

// The Host contains both the hostname and the port, if present. Use SplitHostPort to extract them.
	    fmt.Println(u.Host)
    host, port, _ := net.SplitHostPort(u.Host)
    fmt.Println(host)
    fmt.Println(port)
// Here we extract the path and the fragment after the #.
	    fmt.Println(u.Path)
    fmt.Println(u.Fragment)
// To get query params in a string of k=v format, use RawQuery. You can also parse query params into a map. The parsed query param maps are from strings to slices of strings, so index into [0] if you only want the first value.
	    fmt.Println(u.RawQuery)
    m, _ := url.ParseQuery(u.RawQuery)
    fmt.Println(m)
    fmt.Println(m["k"][0])
}

Output:

// Running our URL parsing program shows all the different pieces that we extracted.
	$ go run url-parsing.go 
postgres
user:pass
user
pass
host.com:5432
host.com
5432
/path
f
k=v
map[k:[v]]
v


Related Searches to Url Parsing In Golang