From 27fd86a8ae247da84a6eb034790da63c31dd788d Mon Sep 17 00:00:00 2001 From: linfuchuan Date: Thu, 4 Jul 2024 21:44:44 +0800 Subject: [PATCH] =?UTF-8?q?go1.18=E4=BB=A5=E4=B8=8A=E7=89=88=E6=9C=AC?= =?UTF-8?q?=E9=80=9A=E8=BF=87go=20list=20-m=20-f=20xxx=20=E6=96=B9?= =?UTF-8?q?=E5=BC=8F=E4=BC=9A=E5=8F=97go.work=E5=BD=B1=E5=93=8D=E8=8E=B7?= =?UTF-8?q?=E5=8F=96=E4=B8=8D=E5=88=B0=E6=AD=A3=E7=A1=AE=E7=9A=84=E6=96=87?= =?UTF-8?q?=E4=BB=B6=E8=B7=AF=E5=BE=84=20=E5=AF=BC=E8=87=B4=E4=BA=A7?= =?UTF-8?q?=E7=94=9F=20exit=20status=20127=20=E9=94=99=E8=AF=AF=EF=BC=8C?= =?UTF-8?q?=E6=9C=AC=E6=AC=A1=E5=AF=B9go.work=20=E7=9A=84=E6=83=85?= =?UTF-8?q?=E5=86=B5=E5=81=9A=E4=BA=86=E5=88=A4=E6=96=AD=20=E9=80=9A?= =?UTF-8?q?=E8=BF=87go=20env=20GOMOD=E8=8E=B7=E5=8F=96=20module=20path?= =?UTF-8?q?=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- go.mod | 1 + go.sum | 2 ++ util/dir.go | 19 ++++++++++++++++--- util/version.go | 19 +++++++++++++++++++ 4 files changed, 38 insertions(+), 3 deletions(-) create mode 100644 util/version.go diff --git a/go.mod b/go.mod index 726bc61..a2fc35e 100644 --- a/go.mod +++ b/go.mod @@ -10,5 +10,6 @@ require ( require ( github.com/google/go-cmp v0.5.7 // indirect + github.com/hashicorp/go-version v1.7.0 // indirect golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 // indirect ) diff --git a/go.sum b/go.sum index 2d69353..dac1904 100644 --- a/go.sum +++ b/go.sum @@ -2,6 +2,8 @@ github.com/golang/protobuf v1.5.4 h1:i7eJL8qZTpSEXOPTxNKhASYpMn+8e5Q6AdndVa1dWek github.com/golang/protobuf v1.5.4/go.mod h1:lnTiLA8Wa4RWRcIUkrtSVa5nRhsEGBg48fD6rSs7xps= github.com/google/go-cmp v0.5.7 h1:81/ik6ipDQS2aGcBfIN5dHDB36BwrStyeAQquSYCV4o= github.com/google/go-cmp v0.5.7/go.mod h1:n+brtR0CgQNWTVd5ZUFpTBC8YFBDLK/h/bpaJ8/DtOE= +github.com/hashicorp/go-version v1.7.0 h1:5tqGy27NaOTB8yJKUZELlFAS/LTKJkrmONwQKeRZfjY= +github.com/hashicorp/go-version v1.7.0/go.mod h1:fltr4n8CU8Ke44wwGCBoEymUuxUHl09ZGVZPK5anwXA= github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= diff --git a/util/dir.go b/util/dir.go index 5c64aab..626c322 100644 --- a/util/dir.go +++ b/util/dir.go @@ -18,13 +18,26 @@ package util import ( + "path/filepath" "strings" ) func GetModuleDir() (string, error) { - output, err := Exec("go list -m -f '{{.Dir}}'", "./") - if err != nil { - output, err = Exec("go list -m -f '{{.Module.Dir}}'", "./") + var ( + output string + err error + ) + if IsVersionGreaterThan118() { + output, err = Exec("go env GOMOD", "./") + if err != nil { + return "", err + } + output = filepath.Dir(output) + } else { + output, err = Exec("go list -m -f '{{.Dir}}'", "./") + if err != nil { + output, err = Exec("go list -m -f '{{.Module.Dir}}'", "./") + } } if err != nil { diff --git a/util/version.go b/util/version.go new file mode 100644 index 0000000..831a199 --- /dev/null +++ b/util/version.go @@ -0,0 +1,19 @@ +// @Package: util +// @Author: linfuchuan +// @Date: 2024/7/4 20:40 + +package util + +import ( + "github.com/hashicorp/go-version" + "runtime" +) + +// IsVersionGreaterThan118 +// @Description: 是否运行时go版本大于1.18 +// @return bool +func IsVersionGreaterThan118() bool { + version1_18, _ := version.NewVersion("1.18") + v, _ := version.NewVersion(runtime.Version()[2:]) + return v.GreaterThanOrEqual(version1_18) +}