func (vm *VersionManager) Parse(r *http.Request) (*Version, error) {
	h := r.Header.Get(vm.header())
	q := r.URL.Query().Get(vm.query())
	if h == "" && q == "" {
		return nil, ErrNoVersionSupplied
	}
	hDate, qDate := time.Time{}, time.Time{}
	var err error
	if h != "" {
		hDate, err = time.Parse(vm.layout(), h)
		if err != nil {
			return nil, ErrInvalidVersion
		}
	}
	if q != "" {
		qDate, err = time.Parse(vm.layout(), q)
		if err != nil {
			return nil, ErrInvalidVersion
		}
	}
	t := hDate
	if hDate.Before(qDate) {
		t = qDate
	}
	v, err := vm.getVersionByTime(t)
	if err != nil {
		return nil, err
	}
	if v.Deprecated {
		return v, ErrVersionDeprecated
	}
	return v, nil
}
Parse evaluates an http.Request object to determine an API version.
It inspects the query parameters and request headers. Whichever
is most recent is the version to use.
package pinned
import (
	"errors"
	"net/http"
	"reflect"
	"sort"
	"time"
)
var (
	ErrInvalidVersion = errors.New("invalid version")
	ErrNoVersionSupplied = errors.New("no version supplied")
	ErrVersionDeprecated = errors.New("version is deprecated")
)
const (
	defaultLayout = "2006-01-02"
	defaultHeader = "Version"
	defaultQuery  = "v"
)
type VersionManager struct {
	Layout string
	Header string
	Query  string
	versions []*Version
}
func (vm *VersionManager) Add(v *Version) error {
	var err error
	v.layout = vm.layout()
	v.date, err = time.Parse(vm.layout(), v.Date)
	if err != nil {
		return err
	}
	vm.versions = append(vm.versions, v)
	sort.Sort(sort.Reverse(versions(vm.versions)))
	return nil
}
func (vm *VersionManager) Latest() *Version {
	if len(vm.versions) == 0 {
		return nil
	}
	return vm.versions[0]
}