当调用一个目录变量时出现错误。

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

Variable for a directory but get error when calling it

问题

我正在尝试为一个变量分配一个读取目录的值,但是我遇到了这个错误:

"无法将 &home(类型为 *[]fs.FileInfo)作为字符串值用于 viper.AddConfigPath 的参数"

我还尝试了在 viper.AddConfigPath(&home) 中使用指针,但也没有成功。

以下是代码片段:

  1. // 查找主目录
  2. home, err := ioutil.ReadDir("../db-dump")
  3. cobra.CheckErr(err)
  4. // 在主目录中搜索名为 ".config"(无扩展名)的配置文件
  5. viper.AddConfigPath(home)

我希望将 type *[]fs.FileInfo 转换为字符串,以便 viper.AddConfigPath() 可以读取它。

更新

我现在尝试了以下方式,编译时没有错误,但由于运行时错误,我无法真正测试代码:

  1. home := "工作目录"
  2. fileSystem := os.DirFS(home)
  3. fs.WalkDir(fileSystem, ".", func(path string, d fs.DirEntry, err error) error {
  4. if err != nil {
  5. log.Fatal(err)
  6. }
  7. fmt.Println(path)
  8. return nil
  9. })
  10. // 在主目录中搜索名为 ".config"(无扩展名)的配置文件
  11. viper.AddConfigPath(home)
英文:

I'm trying to assign a variable to read a directory but I get this error:

> "cannot use &home (value of type *[]fs.FileInfo) as string value in argument to viper.AddConfigPath"

I also tried the pointer inside viper.AddConfigPath(&home) but to no avail as well

Here is a snippet of the code:

  1. // Find home directory
  2. home, err := ioutil.ReadDir("../db-dump")
  3. cobra.CheckErr(err)
  4. // Search config in home directory with name ".config" (without extension)
  5. viper.AddConfigPath(home)

I'm looking to transform the type *[]fs.FileInfo into a string so viper.AddConfigPath()can read it.

Update

I've tried now this way and I'm getting no error compiling, but I can't really test the code yet because of runtime errors:

  1. home := "working directory"
  2. fileSystem := os.DirFS(home)
  3. fs.WalkDir(fileSystem, ".", func(path string, d fs.DirEntry, err error) error {
  4. if err != nil {
  5. log.Fatal(err)
  6. }
  7. fmt.Println(path)
  8. return nil
  9. })
  10. // Search config in home directory with name ".config" (without extension)
  11. viper.AddConfigPath(home)

答案1

得分: 1

你的错误是类型不匹配:home 是类型为 []fs.FileInfo参考链接),而 viper.AddConfigPath() 需要一个 string 类型的参数(参考链接)。

英文:

Your mistake is that you have type mismatch: home is of type []fs.FileInfo (reference) while viper.AddConfigPath() expects a string (reference).

答案2

得分: 1

  1. 你可以将错误消息解读为:

无法使用 &home,因为插入的是 *[]fs.FileInfo 类型的值,而不是 string 类型的值...

这意味着 viper.AddConfigPath 需要一个 string 类型的变量,而不是一个指向 fs.FileInfo 数组的指针。

  1. 在这里进行了快速搜索,列出了另一个主题,可能有相同的问题,并可能提供了对你的问题的提示。
英文:

1. You can read the error message as:

cannot use &home because value of type *[]fs.FileInfo is inserted instead of as string value ...

Meaning that viper.AddConfigPath requires a variable of type string and not a pointer to an array of fs.FileInfo.

2. A quick search here listed this another topic that may have the same issue and probably a tip to your question.

答案3

得分: -1

首先创建一个新的配置文件(仅用于测试代码):

  1. echo "key1: value" > AppName.yaml

然后运行以下代码:

  1. package main
  2. import (
  3. "errors"
  4. "fmt"
  5. "log"
  6. "github.com/spf13/viper"
  7. )
  8. func main() {
  9. v := viper.GetViper()
  10. if err := readConfig(v, ""); err != nil {
  11. log.Fatal(err)
  12. }
  13. fmt.Println(v.AllKeys())
  14. }
  15. func readConfig(v *viper.Viper, configPath string) error {
  16. var err error
  17. if configPath != "" {
  18. v.SetConfigFile(configPath)
  19. if err := v.ReadInConfig(); err != nil {
  20. return fmt.Errorf("无法读取应用程序配置=%q:%w", configPath, err)
  21. }
  22. return nil
  23. }
  24. // 在当前目录中查找 .<AppName>.yaml
  25. v.AddConfigPath(".")
  26. v.SetConfigName("AppName")
  27. if err = v.ReadInConfig(); err == nil {
  28. return nil
  29. } else if !errors.As(err, &viper.ConfigFileNotFoundError{}) {
  30. return fmt.Errorf("无法解析配置=%q:%w", v.ConfigFileUsed(), err)
  31. }
  32. return nil
  33. }

输出结果为:

  1. [key1]

你可以通过路径加载配置:

  1. if err := readConfig(v, "app.yml"); err != nil {
  2. log.Fatal(err)
  3. }
英文:

first create new config file (just for test code):

  1. echo &quot;key1: value&quot; &gt; AppName.yaml

and run the code

  1. package main
  2. import (
  3. &quot;errors&quot;
  4. &quot;fmt&quot;
  5. &quot;log&quot;
  6. &quot;github.com/spf13/viper&quot;
  7. )
  8. func main() {
  9. v := viper.GetViper()
  10. if err := readConfig(v, &quot;&quot;); err != nil {
  11. log.Fatal(err)
  12. }
  13. fmt.Println(v.AllKeys())
  14. }
  15. func readConfig(v *viper.Viper, configPath string) error {
  16. var err error
  17. if configPath != &quot;&quot; {
  18. v.SetConfigFile(configPath)
  19. if err := v.ReadInConfig(); err != nil {
  20. return fmt.Errorf(&quot;unable to read application config=%q : %w&quot;, configPath, err)
  21. }
  22. return nil
  23. }
  24. // look for .&lt;AppName&gt;.yaml (in the current directory)
  25. v.AddConfigPath(&quot;.&quot;)
  26. v.SetConfigName(&quot;AppName&quot;)
  27. if err = v.ReadInConfig(); err == nil {
  28. return nil
  29. } else if !errors.As(err, &amp;viper.ConfigFileNotFoundError{}) {
  30. return fmt.Errorf(&quot;unable to parse config=%q: %w&quot;, v.ConfigFileUsed(), err)
  31. }
  32. return nil
  33. }

out:

  1. [key1]

You can load config by path:

  1. if err := readConfig(v, &quot;app.yml&quot;); err != nil {
  2. log.Fatal(err)
  3. }

huangapple
  • 本文由 发表于 2021年12月29日 19:34:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/70518849.html
匿名

发表评论

匿名网友

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

确定