Golang安装包时遇到未实例化的泛型类型iradix.Tree[T any]问题

Golang安装包时遇到未实例化的泛型类型iradix.Tree[T any]问题 大家好,

有人能帮我解决这个问题吗?任何帮助都将不胜感激!

我正尝试使用以下包完全访问 Consul API:

github.com/hashicorp/consul/api”

我在我的 Windows 机器上安装了它,并将其导入到我的 Go 脚本中,但它似乎依赖于以下包:

github.com/armon/go-metrics”

每当我运行脚本(这本应正常工作)或运行命令 “go get -u github.com/armon/go-metrics” 来更新包的版本时,都会收到以下消息:

# “github.com/armon/go-metrics” “go\src\github.com\armon\go-metrics\start.go:37:17: cannot use generic type iradix.Tree[T any] without instantiation” “go\src\github.com\armon\go-metrics\metrics.go:166:23: cannot infer T (“go\src\github.com\hashicorp\go-immutable-radix\iradix.go:32:10”)”

以下是我的导入部分:

import (
	"encoding/json"
	"fmt"
	"github.com/go-git/go-git"
	"github.com/go-git/go-git/plumbing/transport/http"
	"github.com/hashicorp/consul/api"
	"github.com/hashicorp/hcl/hclsimple"
)

这是我创建 API 客户端的函数(尚未完成!):

func createACLToken(file_name string) {
	// Create a new api.Client object to connect to the Consul server
	client, err := api.NewClient(api.DefaultConfig())
	if err != nil {
		log.Fatal(err)
	}
	// Read the ACL token configuration from the .hcl file
	var token_config api.ACLToken
	err = hclsimple.DecodeFile(fmt.Sprintf("%s.hcl", file_name), nil, &token_config)
	if err != nil {
		log.Fatal(err)
	}
	acl := client.ACL()

	/// Create a new policy with the same name as the ACL token
	policy := api.ACLPolicy{Name: file_name, Rules: token_config.Rules}

	createdPolicy, writeMeta, err := acl.PolicyCreate(&policy, nil)
	if err != nil {
		log.Fatal(err)
	}
	fmt.Printf("Policy created: %v\nWriteMeta: %v\n", createdPolicy, writeMeta)

	// // Create a new role with the same name as the ACL token
	// role := api.ACLRole{Name: file_name, Policies: []string{file_name}}
	// _, err = acl.RoleCreate(&role, nil)
	// if err != nil {
	// 	log.Fatal(err)
	// }
}

我还检查了 GOPATH 目录中的这些包,它们都存在:

“…go\src\github.com\armon\go-metrics” “…go\src\github.com\armon\go-radix” “…go\src\github.com\hashicorp\consul\api” “…go\src\github.com\hashicorp\hcl\hclsimple”


更多关于Golang安装包时遇到未实例化的泛型类型iradix.Tree[T any]问题的实战教程也可以访问 https://www.itying.com/category-94-b0.html

4 回复

你使用的是 Go 模块还是旧版的工作区项目?

对我来说,这看起来像是你使用了不兼容的库版本。

更多关于Golang安装包时遇到未实例化的泛型类型iradix.Tree[T any]问题的实战系列教程也可以访问 https://www.itying.com/category-94-b0.html


我认为正如诺伯特所说,库版本信息有误。据我理解,Go支持两种将模块升级到版本2及以上的方式:一种是在源代码控制中为你的模块创建一个“v2”分支(而不是“master”或“main”分支);另一种是在你的“master”或“main”分支内创建一个“v2”子目录来存放v2版本的代码。看起来hashicorp两种方式都没采用,而是仅使用标签来标识版本号。

当你运行 go get 时,即使你的 go.mod 文件要求的是v1版本,但由于 github.com/hashicorp/go-immutable-radix 的v2版本(包含泛型 Tree 类型)与v1版本“混”在了一起,你实际获取到的是v2版本而非v1版本,因此会得到这个错误。

你可以尝试分叉v1版本,然后 go get 你自己的仓库,而不是hashicorp的仓库。

你好,感谢你的建议!我实际上已经明白我的错误了。我创建了一个模块和几个自定义包。现在我已经没有那个问题了!!

我关于我的脚本还有另一个问题。我需要通过Consul API在一个Go项目中自动生成ACL令牌。为此,我有一个策略配置文件作为创建令牌的输入:

	key_prefix "" {
		  policy = "deny"
		}

		key_prefix "apps/app_name_1/" {
		  policy = "list"
		}

		key_prefix "apps/app_name_1/" {
		  policy = "read"
		}

在我收到的需求中,我不需要在创建令牌之前先创建一个策略,但是根据我对Consul API文档的理解,这实际上是必需的。

然而,需求是只需在Go脚本中应用这个.hcl文件来创建具有给定规则的令牌并将其推送到Consul。策略规则应作为权限。这确实是我收到这个任务的方式,但我不明白如何通过仅应用hcl配置文件来以这种方式创建令牌。你能给我一些示例代码来说明它是如何工作的吗?

这个问题是由于 github.com/armon/go-metrics 包使用了未实例化的泛型类型 iradix.Tree[T any] 导致的。需要更新相关依赖到支持泛型的版本。

首先尝试清理并重新获取依赖:

go clean -modcache
go mod tidy

如果问题仍然存在,可以尝试手动更新 go-immutable-radix 包到最新版本:

go get -u github.com/hashicorp/go-immutable-radix@latest

或者指定一个已知兼容的版本:

go get github.com/hashicorp/go-immutable-radix@v1.3.1

如果还是不行,可以尝试在 go.mod 文件中添加 replace 指令来使用修复了泛型问题的分支:

// 在 go.mod 文件中添加
replace github.com/hashicorp/go-immutable-radix => github.com/hashicorp/go-immutable-radix v1.3.1

然后运行:

go mod tidy

最后重新构建项目:

go build

如果使用的是旧版本的 Go(低于 1.18),需要升级到支持泛型的版本(1.18 或更高):

# 下载最新版 Go
go version
# 确认版本至少是 1.18
回到顶部