You are an expert in writing code in many different languages. Your goal is to perform code completion for the following code, keeping in mind the rest of the code and the file meta data.
    Metadata details:
    filename: cache.go
    path of file: ./repos/under-the-hood/gosrc/cmd/go/internal/modfetch
    
    the code of the file until where you have to start completion: // Copyright 2018 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 modfetch

import (
	"bytes"
	"encoding/json"
	"fmt"
	"io"
	"io/ioutil"
	"os"
	"path/filepath"
	"strings"

	"cmd/go/internal/base"
	"cmd/go/internal/cfg"
	"cmd/go/internal/lockedfile"
	"cmd/go/internal/modfetch/codehost"
	"cmd/go/internal/par"
	"cmd/go/internal/renameio"

	"golang.org/x/mod/module"
	"golang.org/x/mod/semver"
)

var PkgMod string // $GOPATH/pkg/mod; set by package modload

func cacheDir(path string) (string, error) {
	if PkgMod == "" {
		return "", fmt.Errorf("internal error: modfetch.PkgMod not set")
	}
	enc, err := module.EscapePath(path)
	if err != nil {
		return "", err
	}
	return filepath.Join(PkgMod, "cache/download", enc, "/@v"), nil
}

func CachePath(m module.Version, suffix string) (string, error) {
	dir, err := cacheDir(m.Path)
	if err != nil {
		return "", err
	}
	if !semver.IsValid(m.Version) {
		return "", fmt.Errorf("non-semver module version %q", m.Version)
	}
	if module.CanonicalVersion(m.Version) != m.Version {
		return "", fmt.Errorf("non-canonical module version %q", m.Version)
	}
	encVer, err := module.EscapeVersion(m.Version)
	if err != nil {
		return "", err
	}
	return filepath.Join(dir, encVer+"."+suffix), nil
}

// DownloadDir returns the directory to which m should be downloaded.
// Note that the directory may not yet exist.
func DownloadDir(m module.Version) (string, error) {
	if PkgMod == "" {
		return "", fmt.Errorf("internal error: modfetch
    
func rewriteVersionList(dir string) {
	if filepath.Base(dir) != "@v" {
		base.Fatalf("go: internal error: misuse of rewriteVersionList")
	}

	listFile := filepath.Join(dir, "list")

	// We use a separate lockfile here instead of locking listFile itself because
	// we want to use Rename to write the file atomically. The list may be read by
	// a GOPROXY HTTP server, and if we crash midway through a rewrite (or if the
	// HTTP server ignores our locking and serves the file midway through a
	// rewrite) it's better to serve a stale list than a truncated one.
	unlock, err := lockedfile.MutexAt(listFile + ".lock").Lock()
	if err != nil {
		base.Fatalf("go: can't lock version list lockfile: %v", err)
	}
	defer unlock()

	infos, err := ioutil.ReadDir(dir)
	if err != nil {
		return
	}
	var list []string
	for _, info := range infos {
		// We look for *.mod files on the theory that if we can't supply
		// the .mod file then there's no point in listing that version,
		// since it's unusable. (We can have *.info without *.mod.)
		// We don't require *.zip files on the theory that for code only
		// involved in module graph construction, many *.zip files
		// will never be requested.
		name := info.Name()
		if strings.HasSuffix(name, ".mod") {
			v := strings.TrimSuffix(name, ".mod")
			if v != "" && module.CanonicalVersion(v) == v {
				list = append(list, v)
			}
		}
	}
	SortVersions(list)

	var buf bytes.Buffer
	for _, v := range list {
		buf.WriteString(v)
		buf.WriteString("\n")
	}
	old, _ := renameio.ReadFile(listFile)
	if bytes.Equal(buf.Bytes(), old) {
		return
	}

	if err := renameio.WriteFile(listFile, buf.Bytes(), 0666); err != nil {
		base.Fatalf("go: failed to write version list: %v", err)
	}
}