On Thu, Oct 17, 2024 at 10:31 PM shan...@gmail.com <shane....@gmail.com>
wrote:

> I have a need to (programatically) check what version of `goimports`
> someone has installed on their system.
>

It might help if you told us why you need to do that.

The answer by Axel Wagner is somewhat helpful but the output of "go version
-m ..." is meant for human consumption and hard to reliably parse by a
program. On my system (where I haven't performed any updates in several
months):

> go version -m /opt/bin/goimports
/opt/bin/goimports: go1.23.0
path golang.org/x/tools/cmd/goimports
mod golang.org/x/tools v0.24.0
h1:J1shsA93PJUEVaUSaay7UXAyE8aimq3GW0pjlolpa24=
dep golang.org/x/mod v0.20.0 h1:utOm6MM3R3dnawAiJgn0y+xvuYRsm1RKM/4giyfDgV0=
dep golang.org/x/sync v0.8.0 h1:3NFvSEYkUoMifnESzZl15y791HH1qU2xm6eCJU5ZPXQ=
build -buildmode=exe
build -compiler=gc
build
DefaultGODEBUG=asynctimerchan=1,gotypesalias=0,httplaxcontentlength=1,httpmuxgo121=1,httpservecontentkeepheaders=1,panicnil=1,tls10server=1,tls3des=1,tlskyber=0,tlsrsakex=1,tlsunsafeekm=1,winreadlinkvolume=0,winsymlink=0,x509keypairleaf=0,x509negativeserial=1
build CGO_ENABLED=1
build CGO_CFLAGS=
build CGO_CPPFLAGS=
build CGO_CXXFLAGS=
build CGO_LDFLAGS=
build GOARCH=arm64
build GOOS=darwin
build GOARM64=v8.0


Apparently my current version of goimports is v0.24.0. After updating my Go
related CLI tools (via "go install golang.org/x/tools/...@latest") running
"go version -m /opt/bin/goimports" reports

/opt/bin/goimports: go1.23.0
path golang.org/x/tools/cmd/goimports
mod golang.org/x/tools v0.26.0
h1:v/60pFQmzmT9ExmjDv2gGIfi3OqfKoEP6I5+umXlbnQ=
...


It's not obvious to me how to reliably extract the relevant version from
the "go version -m" output for a specific CLI tool. Should there be a
reliable way to programmatically do so? I'm inclined to answer "yes" but
it's not obvious that should be done by something like "go version -m ..."
or something like "/path/to/command -json -version". Obviously all of the
approaches discussed involve running an external command and parsing its
output. Which has its own set of problems regardless of the output produced
by that command. And, obviously, how do you handle a command named
"goimports" installed by the user that is not from the golang.org/x/tools
repository.

-- 
Kurtis Rader
Caretaker of the exceptional canines Junior and Hank

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/CABx2%3DD-uBkK%2B%2B8BSvdxxmYOQFxvGo5N%3D5qzKnvtHh0XwMqyiww%40mail.gmail.com.

Reply via email to