Exploring the code of the net/url package

On a macOS machine that uses Homebrew, you can find the Go code of the net/url package at /usr/local/Cellar/go/1.9.2/libexec/src/net/url/url.go. If for some reason you cannot locate the source code of the net/url package on your Unix machine, you can execute the following command and wait for its output:

$ find / -name url.go 2>/dev/null

The output of the preceding command on a Debian Linux machine is as follows:

/usr/share/go/src/pkg/net/url/url.go
/usr/share/go/src/pkg/html/template/url.go

Apart from the net/url package, there is also an url.go file that belongs to the html/template standard Go package. However, what interests us is the /usr/share/go/src/pkg/net/url/url.go file. Now that you have located the url.go file, we can start playing with it.

The preamble of the net/url package is as follows:

// Copyright 2009 The Go Authors. All rights reserved. 
// Use of this source code is governed by a BSD-style 
// license that can be found in the LICENSE file. 
 
// Package url parses URLs and implements query escaping. 
package url 

More or less, all packages in the standard Go library have a similar preamble. Now, imagine that you want to get a list of all the functions in the net/url package. Standard Unix command line utilities such as grep(1) are here to help you:

$ grep '^func' /usr/local/Cellar/go/1.9.2/libexec/src/net/url/url.go
func (e *Error) Error() string { return e.Op + " " + e.URL + ": " + e.Err.Error() }
func (e *Error) Timeout() bool {
func (e *Error) Temporary() bool {
func ishex(c byte) bool {
func unhex(c byte) byte {
func (e EscapeError) Error() string {
...
func (u *URL) MarshalBinary() (text []byte, err error) {
func (u *URL) UnmarshalBinary(text []byte) error { 

Now let's say that you want to look at the implementation of one of the functions found in net/url, and the function that interests you is, say, User(). Its implementation is as follows:

// User returns a Userinfo containing the provided username 
// and no password set. 
func User(username string) *Userinfo { 
        return &Userinfo{username, "", false} 
} 

The documentation of the User() function states that it returns a Userinfo, which is a structure defined in net/url:

// The Userinfo type is an immutable encapsulation of username and 
// password details for a URL. An existing Userinfo value is 
// guaranteed to have a username set (potentially empty, 
// as allowed by RFC 2396), and optionally a password. 
type Userinfo struct { 
        username    string 
        password    string 
        passwordSet bool 
} 

Now things are becoming clearer. If you continue delving into the code of net/url, you will find more in-depth information about it.

The general advice here is that if you have doubts about the inner workings of a package in which you are interested, and its documentation does not help you very much, you can always look at its implementation to clarify things.

The next subsection will look into the Go code of a more interesting package.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.143.4.181