作者:mobiledu2502881447 | 来源:互联网 | 2023-09-11 17:44
使用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)
}
解决办法
- 使用包管理工具govendor,用于将
go build
时的应用路径搜索调整成为当前项目目录/vendor
目录方式 推荐 - 在
$GOPATH
目录下只保留一份golang.org/x/net/trace
,删除所有项目vendor
目录中的golang.org/x/net/trace
不推荐 - 独立工程目录,每个项目设置
GOPATH
不推荐