问题描述
我使用名为的配置库
在我的主要中,我有这样的:
viper.SetConfigName(development)
viper.AddConfigPath (config / settings /)
err:= viper.ReadInConfig()
if err!= nil {
fmt.Println(viper config read error%v,err)
}
然后我有一个以viper作为参数的结构:
类型MyConfig结构{
v * viper.Viper
}
在我的主目录中,我有一个函数返回这个MyConfig,如下所示:
func NewMyConfig(v * viper.Viper)* MyConfig {
return& MyConfig {v:v}
}
我得到这个错误:
./ main.go:55:use没有选择器的包装毒蛇
不知道我该怎么做?
当您像
的软件包时 importgithub.com/spf13/viper
包名称(在这种情况下为 viper
)将作为新标识符提供给您。您可以使用此标识符构建以引用(标识符以大写字母开头)。
包名本身本身不能使用。
myConfig = NewMyConfig(& viper)
您使用包名称 viper
,而无需指定要从包中引用的导出标识符。
你想用你的 NewMyConfig()
函数来获得一个指向你的 MyConfig
struct。您的 NewMyConfig()
函数的值为 * viper.Viper
。由于 viper.Viper
struct包含未导出的字段,因此您可以像& viper.Viper {}
那样创建它,但是 viper
包会导出一个函数,它可以用来获得一个指向新的,初始化的 viper.Viper
值。您可以像这样使用它:
vp:= viper.New()
myConfig = NewMyConfig(vp)
请注意, viper
包声明了一个内部全局,未导出 viper.Viper
实例。有很多导出的函数匹配 viper.Viper
类型的方法。这些匹配功能适用于全球未引用的 viper.Viper
实例。因此,您可以选择使用 viper
包的所有导出全局函数,或者创建自己的 Viper
实例,然后继续使用它的方法。
I'm using this config library called Viper
In my main I have this:
viper.SetConfigName("development")
viper.AddConfigPath("config/settings/")
err := viper.ReadInConfig()
if err != nil {
fmt.Println("viper config read error %v", err)
}
I then have a struct that takes a viper as parameter:
type MyConfig struct {
v *viper.Viper
}
In my main I have a function that returns this MyConfig like:
func NewMyConfig(v *viper.Viper) *MyConfig {
return &MyConfig{v: v}
}
I am getting this error:
./main.go:55: use of package viper without selector
Not sure what I should be doing?
When you import a package like
import "github.com/spf13/viper"
the package name (which is viper
in this case) will be available to you as a new identifier. You may use this identifier to construct qualified identifiers to refer to exported identifiers of the package (identifiers that start wtih an uppercase letter).
The package name itself cannot be used by itself. The line that gives you error:
myConfig = NewMyConfig(&viper)
You used package name viper
without specifying what exported identifier you want to refer to from the package.
You want to use your NewMyConfig()
function to obtain a pointer to a new value of your MyConfig
struct. Your NewMyConfig()
function expects a value of *viper.Viper
. Since the viper.Viper
struct contains unexported fields, you can just create it like &viper.Viper{}
, but the viper
package exports a function viper.New()
which can be used to obtain a pointer to a new, initialized viper.Viper
value. You may use it like:
vp := viper.New()
myConfig = NewMyConfig(vp)
Note that the viper
package declares an internal, global, unexported viper.Viper
"instance". There are many exported functions that match methods of the viper.Viper
type. These "matching" functions work on the global, unexported viper.Viper
instance. So you may choose to use all the exported global functions of the viper
package, or create your own Viper
instance and then keep using its methods afterwards.
这篇关于无需选择器错误即可使用软件包的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!