类型 clientv3.Config 没有字段或方法 Username。

huangapple go评论96阅读模式
英文:

type clientv3.Config has no field or method Username

问题

代码如下所示:

import (
	"context"
	"crypto/tls"
	"time"

	"go.etcd.io/etcd/clientv3"
)

func main(){
    ...
    config := clientv3.Config{}
    config.Username = u.username
    ...
}

go.mod 文件如下所示:

module github.com/xxx

go 1.17

require (
	github.com/coreos/etcd v2.3.8+incompatible // indirect
	go.etcd.io/etcd v2.3.8+incompatible // indirect
)

出现以下错误信息:

config.Username undefined (type clientv3.Config has no field or method Username)

有没有办法解决这个问题?github.com/coreos/etcd/clientv3go.etcd.io/etcd 有什么区别?

英文:

the code is:

import (
	"context"
	"crypto/tls"
	"time"

	"go.etcd.io/etcd/clientv3"
)
func main(){
    ...
    config := clientv3.Config{}
    config.Username = u.username
    ...
}

go.mod file

module github.com/xxx

go 1.17

require (

	github.com/coreos/etcd v2.3.8+incompatible // indirect
	go.etcd.io/etcd v2.3.8+incompatible // indirect
)

It fails with this message:

config.Username undefined (type clientv3.Config has no field or method Username)

Is there a way to resolve this problem?what's the difference between github.com/coreos/etcd/clientv3 and go.etcd.io/etcd?

答案1

得分: 1

go.etcd.io/etcd/clientv3go.etcd.io/etcd/client/v3是两个有些混淆的不同包,它们都使用了包名clientv3

client/v3.Config类型具有您期望的Username字段,因此您可以更新您的代码以导入该包,而不是另一个包。

import (
	
    "go.etcd.io/etcd/client/v3"
)

(https://play.golang.org/p/2SK2FUNxWs9)


github.com/coreos/etcd/clientv3github.com/coreos/etcd存储库中的包在他们采用Go模块之前的旧名称。当他们转移到模块时,他们将规范的导入路径更改为go.etcd.io/etcd/client/v3,但是(通过Go模块系统的一些怪癖)旧路径和新路径的混合仍然可以解析,使用旧路径的代码但从新的URL提供。您可能只想使用新的规范路径。

英文:

go.etcd.io/etcd/clientv3 and go.etcd.io/etcd/client/v3 are, somewhat confusingly, distinct packages that both use the package name clientv3.

The client/v3.Config type has the Username field you expect, so perhaps you can update your code to import that package instead of the other one?

import (
	
    "go.etcd.io/etcd/client/v3"
)

(https://play.golang.org/p/2SK2FUNxWs9)


github.com/coreos/etcd/clientv3 was an older name for the package from the github.com/coreos/etcd repo before they adopted Go modules. When they moved to modules, they changed the canonical import path to go.etcd.io/etcd/client/v3, but (through some quirks of the Go module system) a hybrid of the old and new paths can still be resolved, using the code from the old path but served from the new URL. You probably want to use only the new canonical path.

huangapple
  • 本文由 发表于 2021年8月26日 21:19:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/68939293.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定