Hello everyone, my name is Xie Wei, I am a programmer.

In the near future, the built-in library will be synchronized to learn, the main reference official documents and source code

The topic of this section: urls

This is actually a relatively small built-in function, mainly used for network requests, probably the most use is to handle network request parameters. Of course, if you often write restful apis in your projects, you probably use them too.

Outline:

  • The principle of knowledge
  • Basic usage
  • What you learned

1. Principle knowledge

URL: Uniform Resource Location, called Uniform Resource locator.

The background is: For example, if you want to find something at home, first of all, do you know the attribution analysis of things, for example, a kitchen knife, you are most likely to go to the kitchen, so that you can find it with a high probability. The same is true of resources on the network. In order to accurately find resources on the server, there is a URL.

So what do we know about urls?

  • The meaning of represents
  • Component: You want to know what the specific form of url is
  • grammar

1.1 Meaning

Uniform Resource Locator (URL) uniquely locates resources on the network.

1.2 Components

Here’s an example:


https://www.google.com

Copy the code
  • Scheme: Indicates the protocol used, such as HTTP and FTP
  • Server address: You can use either an IP address or a domain name, so there is a mapping between IP addresses and domain names
  • Resource path: This part is for the network specific resource address

This is easy to understand, and our daily home address, the same meaning of the company address, first locate the province, and then locate the city, and then continue to locate, until find your address.

The resources on the network are basically borrowed from this set of ideas: first locate the address on the server, and then locate the address of specific resources. That’s what a URL means.

1.3 Syntax

In order to regulate the addresses of these resources on the network, there needs to be a specification. What does this syntax contain?

  • Scheme: specifies the protocol used to access resources on the server
  • User: Some protocols, such as FTP, can pass in a plaintext user name and password to obtain resources
  • password
  • Host: server address, which can be an IP address or a domain name
  • Port: a string of numbers
  • Path: resource paths, separated by slashes (/)
  • Arguments: example=one&hello=world similar to this key-value pair
  • Query: The identifier is? Use with parameters
  • Fragment: The identifier is “#”

Ok, hard to understand, here’s an example:

https://godoc.org/net/url#example-Values
Copy the code
  • scheme: https
  • User: no
  • Password: no
  • Host: godoc.org
  • Port: no
  • Path: the.net/url
  • Parameters: no
  • Query: no
  • Clip: example – Values

Some are optional, so in the end, these concepts are commonly used:

  • Scheme (protocol)
  • Host (server address)
  • Port (Server port)
  • Path (path)
  • Params (parameters)
  • Fragments (pieces)

There was another problem with the request: encoding, used to represent various insecure characters in the URL

Common encoding:

character The sample
~ % 7
The blank space % 20
% % 25

2. Basic usage

As explained above, we know what a URL is, but in the end it is really a string, but at the network resource request level, this string is given more meaning.

Putting aside the use of the official built-in library, we want to implement it ourselves first. How does that work?

Depending on the composition of the URL, we might design something like this


type Url struct {
	Scheme   string 
	User     string
	Password string
	Host     string
	Port     string
	Path     string
	Params   map[string] []string
	Fragment string
}

Copy the code

Ok, so let’s say we design this, and we convert a string to a Url of the type that we define, how do we get the parts?

https://godoc.org/net/url#example-Values
Copy the code

According to each meaning, then we should think about the processing of this string of characters, such as :,//,/,# and so on to get the content we need.

The above is our own thinking. If you are interested, you can implement it on your own. Think about: what public methods would you offer? What auxiliary functions will be designed?

See the official implementation below:

Example:

package main

import (
	"fmt"
	"net/url"
)

var urlCollection struct {
	urlOne   string
	urlTwo   string
	urlThree string
	urlFour  string
	urlFive  string
}

func init(a) {
	urlCollection.urlOne = "https://www.google.com"
	urlCollection.urlTwo = "http://localhost:8887/v1/api/cloud_api/fetcher? [email protected]"
	/ / delete the area at https://developer.readsense.cn/docs/retail/retailv2/regions.html#
	urlCollection.urlThree = "https://developer.readsense.cn/docs/retail/retailv2/regions.html#%E5%88%A0%E9%99%A4%E5%8C%BA%E5%9F%9F"
	urlCollection.urlFour = "https://joe:[email protected]/share_info.txt"
	urlCollection.urlFive = "https://godoc.org/net/url#example-Values"
}

func main(a) {
	OpUrl(urlCollection.urlOne)
	OpUrl(urlCollection.urlTwo)
	OpUrl(urlCollection.urlThree)
	OpUrl(urlCollection.urlFour)
	OpUrl(urlCollection.urlFive)

}
func OpUrl(urlString string) {

	URL, _ := url.Parse(urlString)
	fmt.Println("user", URL.User)
	fmt.Println("scheme", URL.Scheme)
	fmt.Println("host", URL.Host)
	fmt.Println("port", URL.Port())
	fmt.Println("rawQuery", URL.RawQuery)
	fmt.Println("rawPath", URL.RawPath)
	fmt.Println("path", URL.Path)
	fmt.Println("forceQuery", URL.ForceQuery)
	fmt.Println("fragment", URL.Fragment)

}
Copy the code

As can be seen: url. Parse the string can be converted into a url object, the object contains: the User, the Scheme, Host, Path, RawPath, ForceQuery, fragments field and some of the methods.

Look at the source code to see how the URL type object is defined.

type URL struct {
Scheme     string
Opaque     string    // encoded opaque data
User       *Userinfo // username and password information
Host       string    // host or host:port
Path       string    // path (relative paths may omit leading slash)
RawPath    string    // encoded path hint (see EscapedPath method)
ForceQuery bool      // append a query ('? ') even if RawQuery is empty
RawQuery   string    // encoded query values, without '? '
Fragment   string    // fragment for references, without '#'
}
Copy the code

It doesn’t look very different from what we expected, but the author thought much deeper than we did, such as taking into account the encoding, so there will be RawQuery, RawPath, etc.

Keep checking:

func PathEscape(s string) string
func PathUnescape(s string) (string, error)
func QueryEscape(s string) string
func QueryUnescape(s string) (string, error)
type Error
func (e *Error) Error(a) string
func (e *Error) Temporary(a) bool
func (e *Error) Timeout(a) bool
type EscapeError
func (e EscapeError) Error(a) string
type InvalidHostError
func (e InvalidHostError) Error(a) string
type URL
func Parse(rawurl string) (*URL, error)
func ParseRequestURI(rawurl string) (*URL, error)
func (u *URL) EscapedPath(a) string
func (u *URL) Hostname(a) string
func (u *URL) IsAbs(a) bool
func (u *URL) MarshalBinary(a) (text []byte, err error)
func (u *URL) Parse(ref string) (*URL, error)
func (u *URL) Port(a) string
func (u *URL) Query(a) Values
func (u *URL) RequestURI(a) string
func (u *URL) ResolveReference(ref *URL) *URL
func (u *URL) String(a) string
func (u *URL) UnmarshalBinary(text []byte) error
type Userinfo
func User(username string) *Userinfo
func UserPassword(username, password string) *Userinfo
func (u *Userinfo) Password(a) (string.bool)
func (u *Userinfo) String(a) string
func (u *Userinfo) Username(a) string
type Values
func ParseQuery(query string) (Values, error)
func (v Values) Add(key, value string)
func (v Values) Del(key string)
func (v Values) Encode(a) string
func (v Values) Get(key string) string
func (v Values) Set(key, value string)
Copy the code

As can be seen, important uses are:

  • There is a method for converting a string into a URL object, which gets the corresponding component
  • The parameter Values in the URL is very important, especially when we’re writing restful apis, and we’re thinking about that, request parameters. Her bottom ismap[string][]string, so you canAdd, Del, Get,SetAnd so on. So this is something to keep in mind, and we’ll analyze it next timenet/httpAn important part of the library is the handling of request parameters

Finally, take a look at how the library handles errors:


type EscapeError string

func (e EscapeError) Error() string {
	return "invalid URL escape " + strconv.Quote(string(e))
}

type InvalidHostError string

func (e InvalidHostError) Error() string {
	return "invalid character " + strconv.Quote(string(e)) + " in host name"
}
Copy the code
  • Define a structure
  • Implement the Error method, which then implements the Error interface

3. What have you learned

  1. Standing in the perspective of the designer, how should I design?
  2. The idea of how to design comes from principles, not random thinking.
  3. Go back and look at the principles in the book

After < >

  • Back end engineer walkthrough iteration in progress