golang.org/x/net/trace重复错误

在使用gRPC进行开发时遇到启动client时报错,原因是项目中存在golang.org/x/net/trace包的重复副本。错误提示指向该包的某个方法。解决方案包括使用govendor调整依赖路径,或者删除其他项目目录中的重复包,以及采用独立工程目录管理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

使用grpc进行开发时,启动grpc client报错如下:

panic: /debug/requests is already registered. You may have two independent copies of golang.org/x/net/trace in your binary, trying to maintain separate state. This may involve a vendored copy of golang.org/x/net/trace.

错误原因

golang.org/x/net/trace 在整个$GOPATH中有重复的副本,包括其他项目的vendor目录中依赖了golang.org/x/net包。

错误提示

goroutine 1 [running]:
golang.org/x/net/trace.init.0()
	/Users/lcl/go/src/golang.org/x/net/trace/trace.go:123 +0x17e

查看golang.org/x/net/trace/trace.go文件中的init()方法

// HTTP ServeMux paths.
const (
	debugRequestsPath = "/debug/requests"
	debugEventsPath   = "/debug/events"
)

...

func init() {
	_, pat := http.DefaultServeMux.Handler(&http.Request{URL: &url.URL{Path: debugRequestsPath}})
	if pat == debugRequestsPath {
		panic("/debug/requests is already registered. You may have two independent copies of " +
			"golang.org/x/net/trace in your binary, trying to maintain separate state. This may " +
			"involve a vendored copy of golang.org/x/net/trace.")
	}

	// TODO(jbd): Serve Traces from /debug/traces in the future?
	// There is no requirement for a request to be present to have traces.
	http.HandleFunc(debugRequestsPath, Traces)
	http.HandleFunc(debugEventsPath, Events)
}
解决办法
  1. 使用包管理工具govendor,用于将go build时的应用路径搜索调整成为当前项目目录/vendor目录方式 推荐
  2. $GOPATH目录下只保留一份golang.org/x/net/trace,删除所有项目vendor目录中的golang.org/x/net/trace 不推荐
  3. 独立工程目录,每个项目设置GOPATH 不推荐
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值