gocmd

package module
v1.0.29 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 4, 2024 License: MIT Imports: 13 Imported by: 0

README

gocmd

Go Reference

In the Go world, we can install several versions of Go without **env by using wrapper program.

$ go install golang.org/dl/go1.19@latest
$ go1.19 download

With IDEs like Visual Studio Code or GoLand, we can choose the version of "go" command and use it without typing "go1.19". Because their embedded terminal emulators set PATH environment variable automatically.

But in other environments, we have to type "go1.19" to use go command of the version "go1.19". When creating devtools using go command, this differed behavior is not preferable.

So, in order to use an expected version of go, following utilities are needed.

Validate Go version

All released version is read from here.

err := ValidateVersion("go1.19")
// err == nil

err = ValidateVersion("unknown")
// err == ErrInvalidVersion

Check the version of "go" command whether it matches to the version written in "go.mod"

Get the version of "go" command in your environment and the version written in go.mod, and compare them.

module m

go 1.18
err := ValidModuleGoVersion("go1.18.5")
// err == nil

err = ValidModuleGoVersion("go1.17")
// err == ErrUnexpectedVersion

Get the path of "go" executable that has the given version

$ go env GOVERSION
go1.19
$ which go1.18.5
/Users/me/go/bin/go1.18.5
$ which go1.17.5
go1.17.5 not found
path, err := Lookup("go1.19")
// path == "go"
// err == nil

path, err = Lookup("go1.18.5")
// path == "/Users/me/go/bin/go1.18.5"
// err == nil

path, err = Lookup("go1.17.5")
// path == ""
// err == ErrNotFound

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrInvalidVersion = errors.New("invalid version")
View Source
var ErrNotFound = exec.ErrNotFound
View Source
var ErrUnexpectedGoVersion = errors.New("unexpected go version in go.mod")

Functions

func CurrentVersion added in v0.0.3

func CurrentVersion() (string, error)

CurrentVersion returns the version of "go" command.

func Determine added in v1.0.0

func Determine(version string, mode Mode) (path, ver string, err error)

Determine go command with given version, and return its path and actual version. Following mode is available.

  • ModeExact determines command by using Lookup
  • ModeLatest determines command by using LookupLatest
  • ModeFallback determines command by using LookupLatest, but if no command was found, fallbacks to "go" command

func DetermineFromModuleGoVersion added in v1.0.0

func DetermineFromModuleGoVersion(mode Mode) (path, ver string, _ error)

DetermineFromModuleGoVersion determines go command with the version from go.mod, and returns its path and actual version. Every mode uses LookupLatest. In ModeFallback, if no command was found, fallbacks to "go"command.

func Lookup

func Lookup(version string) (string, error)

Lookup finds a go executable having exact given version. Firstly, it checks the given version with ValidVersion, and returns ErrInvalidVersion if the version is invalid. After that, it checks versions of "go" and specific executable(golang.org/dl/go1.N). When an executable with GOVERSION={given version} exists, it returns the executable's path. If no executable exists, it returns ErrNotFound.

func LookupLatest

func LookupLatest(version string) (string, error)

LookupLatest finds a go executable having the given version. Behavior is similar to Lookup, but it collects versions that have the same major version. This finds the executable that has the latest version in the collected list. If "go" command has the same major version, it is prioritized.

func MajorVersion added in v0.0.4

func MajorVersion(version string) string

MajorVersion returns major version of the given version. If the given version is invalid, returned value is an empty string.

func ModuleGoVersion added in v0.0.2

func ModuleGoVersion() (string, error)

ModuleGoVersion reads module's go version from "go.mod" with the path from `go env GOMOD`.

func StableVersion

func StableVersion(version string) (bool, error)

StableVersion returns whether the given go version exists and is stable. The source of correctness is the following URL:

https://go.dev/dl/?mode=json&include=all

func ValidModuleGoVersion

func ValidModuleGoVersion(version string) error

ValidModuleGoVersion compares the given version and module's Go version. Go version of the module will be read from "go.mod" with the path from `go env GOMOD`.

func ValidVersion

func ValidVersion(version string) error

ValidVersion returns whether the given go version exists. The source of correctness is the following URL:

https://go.dev/dl/?mode=json&include=all

Types

type Mode added in v1.0.0

type Mode uint8
const (
	ModeExact Mode = 1 << iota
	ModeLatest
	ModeFallback
)

Directories

Path Synopsis
cmd
Code generated by genvers.
Code generated by genvers.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL