https://github.com/daichitakahashi/gocmd
Get expected version of "go" command
https://github.com/daichitakahashi/gocmd
Last synced: 5 months ago
JSON representation
Get expected version of "go" command
- Host: GitHub
- URL: https://github.com/daichitakahashi/gocmd
- Owner: daichitakahashi
- License: mit
- Created: 2022-08-17T14:46:32.000Z (almost 3 years ago)
- Default Branch: main
- Last Pushed: 2024-11-09T01:09:30.000Z (7 months ago)
- Last Synced: 2024-11-09T02:20:26.714Z (7 months ago)
- Language: Go
- Size: 37.1 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# gocmd
[](https://pkg.go.dev/github.com/daichitakahashi/gocmd)
In the Go world, we can install several versions of Go without **\*\*env** by using [wrapper program](https://cs.opensource.google/go/dl).
```shell
$ 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](https://go.dev/dl/?mode=json&include=all).
```go
err := ValidateVersion("go1.19")
// err == nilerr = 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 mgo 1.18
```
```go
err := ValidModuleGoVersion("go1.18.5")
// err == nilerr = ValidModuleGoVersion("go1.17")
// err == ErrUnexpectedVersion
```## Get the path of "go" executable that has the given version
```shell
$ 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
```
```go
path, err := Lookup("go1.19")
// path == "go"
// err == nilpath, err = Lookup("go1.18.5")
// path == "/Users/me/go/bin/go1.18.5"
// err == nilpath, err = Lookup("go1.17.5")
// path == ""
// err == ErrNotFound
```