Golang中下载golang.org/x/exp/constraints包遇到的问题
Golang中下载golang.org/x/exp/constraints包遇到的问题
对 Go 模块的使用还比较新。我正在学习一门课程,其中提到了 constraints 包。我找到了代码和有效的 go.mod 文件:
module golang.org/x/exp
go 1.23.0
require (
github.com/google/go-cmp v0.6.0
golang.org/x/mod v0.24.0
golang.org/x/tools v0.33.0
)
require golang.org/x/sync v0.14.0 // indirect
我可以为所需的包执行 go mod download,但当我尝试执行:
go get golang.org/x/exp/contstraints
这应该能工作,但我收到了一个错误:
go: golang.org/x/exp/contstraints: no matching versions for query "upgrade"
我还尝试下载不同的提交:
go get golang.org/x/exp/contstraints@2b6e20a6d8b6a7fa47ce7f245c422b7abb361048
但我收到了各种不同的错误,从:
golang.org/x/exp/contstraints@v0.0.0-20250506013429-2b6e20a6d8b6: invalid version: missing golang.org/x/exp/contstraints/go.mod at revision 2b6e20a6d8b6
到:
go: module golang.org/x/exp@2b6e20a6d8b6a7fa47ce7f245c422b7abb361048 found (v0.0.0-20250506013429-2b6e20a6d8b6), but does not contain package golang.org/x/exp/contstraints
我知道这是实验性代码,没有任何兼容性保证。我更想知道我是否做错了什么,以及我是否应该能够这样做。我运行的是 go 1.24.2:
dan@workstation-DBDGVD3:~/Class/002-Learning_Go-Take2/023-Generics/186-Package_constraints$ go env
AR='ar'
CC='gcc'
CGO_CFLAGS='-O2 -g'
CGO_CPPFLAGS=''
CGO_CXXFLAGS='-O2 -g'
CGO_ENABLED='1'
CGO_FFLAGS='-O2 -g'
CGO_LDFLAGS='-O2 -g'
CXX='g++'
GCCGO='gccgo'
GO111MODULE=''
GOAMD64='v1'
GOARCH='amd64'
GOAUTH='netrc'
GOBIN=''
GOCACHE='/home/dan/.cache/go-build'
GOCACHEPROG=''
GODEBUG=''
GOENV='/home/dan/.config/go/env'
GOEXE=''
GOEXPERIMENT=''
GOFIPS140='off'
GOFLAGS=''
GOGCCFLAGS='-fPIC -m64 -pthread -Wl,--no-gc-sections -fmessage-length=0 -ffile-prefix-map=/home/dan/tmp/go-build3918362781=/tmp/go-build -gno-record-gcc-switches'
GOHOSTARCH='amd64'
GOHOSTOS='linux'
GOINSECURE=''
GOMOD='/home/dan/Class/002-Learning_Go-Take2/023-Generics/186-Package_constraints/go.mod'
GOMODCACHE='/home/dan/go/pkg/mod'
GONOPROXY=''
GONOSUMDB=''
GOOS='linux'
GOPATH='/home/dan/go'
GOPRIVATE=''
GOPROXY='direct'
GOROOT='/usr/lib/golang'
GOSUMDB='off'
GOTELEMETRY='local'
提前感谢。
更多关于Golang中下载golang.org/x/exp/constraints包遇到的问题的实战教程也可以访问 https://www.itying.com/category-94-b0.html
pekim:
go get golang.org/x/exp/constraints
这招管用了,非常感谢,@pekim ++
更多关于Golang中下载golang.org/x/exp/constraints包遇到的问题的实战系列教程也可以访问 https://www.itying.com/category-94-b0.html
go get golang.org/x/exp/contstraints
假设你指的是 golang.org/x/exp/constraints,你的命令里多了一个 ‘t’。我认为你原本想执行的是这个命令。
go get golang.org/x/exp/constraints
是的,这只是个打字错误,但我仍然收到:
go: golang.org/x/exp/constraints: no matching versions for query "upgrade"
或者当我添加标签时,我得到:
go: golang.org/x/constraints@latest: unrecognized import path "golang.org/x/constraints": reading https://golang.org/x/constraints?go-get=1: 404 Not Found
server response: 404 page not found
尽管我正看着这些文件。
我甚至尝试了指定版本的方法,但得到了相同的结果
dan@workstation-DBDGVD3:~/Class/002-Learning_Go-Take2/023-Generics/186-Package_constraints$ go get golang.org/x/constraints@v0.0.0-20250506013437-ce4c2cf36ca6
go: golang.org/x/constraints@v0.0.0-20250506013437-ce4c2cf36ca6: unrecognized import path "golang.org/x/constraints": reading https://golang.org/x/constraints?go-get=1: 404 Not Found
server response: 404 page not found
尽管我明明就在看着这些文件,
dancoe77:
go get golang.org/x/constraints@v0.0.0-20250506013437-ce4c2cf36ca6
你确定这不是另一个拼写错误吗?看起来你漏掉了模块路径中的 exp 部分。
重新加上 exp 对我来说工作正常。
$ mkdir constraints-question
$ cd constraints-question
$ go mod init constraints-question
go: creating new go.mod: module constraints-question
$ go get golang.org/x/exp/constraints@v0.0.0-20250506013437-ce4c2cf36ca6
go: added golang.org/x/exp v0.0.0-20250506013437-ce4c2cf36ca6
$ cat go.mod
module constraints-question
go 1.24.0
require golang.org/x/exp v0.0.0-20250506013437-ce4c2cf36ca6 // indirect
$
以及不指定明确版本的情况。
$ mkdir constraints-question
$ cd constraints-question
$ go mod init constraints-question
go: creating new go.mod: module constraints-question
$ go get golang.org/x/exp/constraints
go: added golang.org/x/exp v0.0.0-20250506013437-ce4c2cf36ca6
$ cat go.mod
module constraints-question
go 1.24.0
require golang.org/x/exp v0.0.0-20250506013437-ce4c2cf36ca6 // indirect
$
你是否没有任何可能与获取模块相关的配置,例如 GOPROXY、GOPRIVATE 或 GONOPROXY 环境变量?
你的问题在于拼写错误。constraints 包的正确路径是 golang.org/x/exp/constraints,而不是 golang.org/x/exp/contstraints(注意你写的是 contstraints,少了一个 r)。
正确的命令应该是:
go get golang.org/x/exp/constraints
如果你需要指定特定版本,可以使用:
go get golang.org/x/exp/constraints@latest
或者指定具体的版本号:
go get golang.org/x/exp@v0.0.0-20250506013429-2b6e20a6d8b6
在你的 go.mod 文件中,应该能看到类似这样的依赖:
require golang.org/x/exp v0.0.0-20250506013429-2b6e20a6d8b6
然后在代码中可以这样使用:
package main
import (
"fmt"
"golang.org/x/exp/constraints"
)
func Min[T constraints.Ordered](a, b T) T {
if a < b {
return a
}
return b
}
func main() {
fmt.Println(Min(3, 5)) // 输出: 3
fmt.Println(Min(3.14, 2.71)) // 输出: 2.71
fmt.Println(Min("a", "b")) // 输出: a
}
注意:从 Go 1.21 开始,constraints 包中的大部分功能已经迁移到了标准库的 cmp 包中。对于新的项目,建议使用标准库的替代方案:
package main
import (
"cmp"
"fmt"
)
func Min[T cmp.Ordered](a, b T) T {
return cmp.Min(a, b)
}
func main() {
fmt.Println(Min(3, 5)) // 输出: 3
fmt.Println(Min(3.14, 2.71)) // 输出: 2.71
fmt.Println(Min("a", "b")) // 输出: a
}

