登录
首页 >  Golang >  Go问答

调用 BindPFlag 时为什么会收到 nil 指针错误?

来源:stackoverflow

时间:2024-03-25 09:18:36 162浏览 收藏

在使用 Cobra 和 Viper 库时,调用 `config.BindPFlag` 时可能会收到 `nil` 指针错误。原因是 `cmd.flags().lookup("name")` 返回 `nil`,因为持久标志尚未在 `cmd.newcmdroot()` 级别初始化。要解决此问题,需要使用 `cmd.PersistentFlags().Lookup("name")` 来查找持久标志,而不是 `cmd.flags().lookup("name")`。

问题内容

我最近刚刚开始使用 go,并且遇到了一些问题 我不确定我是否理解与 cobra 和 viper 一起工作的行为。

这是您获得的示例代码的稍微修改版本 运行 cobra init。在 main.go 我有:

package main

import (
    "github.com/larsks/example/cmd"
    "github.com/spf13/cobra"
)

func main() {
    rootcmd := cmd.newcmdroot()
    cobra.checkerr(rootcmd.execute())
}

cmd/root.go 中我有:

package cmd

import (
    "fmt"
    "os"

    "github.com/spf13/cobra"

    "github.com/spf13/viper"
)

var cfgfile string

func newcmdroot() *cobra.command {
    config := viper.new()

    var cmd = &cobra.command{
        use:   "example",
        short: "a brief description of your application",
        persistentprerun: func(cmd *cobra.command, args []string) {
            initconfig(cmd, config)
        },
        run: func(cmd *cobra.command, args []string) {
            fmt.printf("this is a test\n")
        },
    }

    cmd.persistentflags().stringvar(&cfgfile, "config", "", "config file (default is $home/.example.yaml)")
    cmd.persistentflags().string("name", "", "a name")

  // *** if i move this to the top of initconfig
  // *** the code runs correctly.
    config.bindpflag("name", cmd.flags().lookup("name"))

    return cmd
}

func initconfig(cmd *cobra.command, config *viper.viper) {
    if cfgfile != "" {
        // use config file from the flag.
        config.setconfigfile(cfgfile)
    } else {
        config.addconfigpath(".")
        config.setconfigname(".example")
    }

    config.automaticenv() // read in environment variables that match

    // if a config file is found, read it in.
    if err := config.readinconfig(); err == nil {
        fmt.fprintln(os.stderr, "using config file:", config.configfileused())
    }

  // *** this line triggers a nil pointer reference.
    fmt.printf("name is %s\n", config.getstring("name"))
}

此代码将在最终调用时因 nil 指针引用而崩溃 fmt.printf

panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x50 pc=0x6a90e5]

如果我将调用从 newcmdroot 移至 config.bindpflag 函数到 initconfig 命令的顶部,一切都运行 没有问题。

这是怎么回事?根据 viper 文档有关使用 bindpflags

与 bindenv 一样,绑定方法时不设置该值 被调用,但是当它被访问时。这意味着您可以尽早绑定 你想要的,即使是在 init() 函数中。

这几乎正是我在这里所做的。当我打电话的时候 config.bindpflagconfig 为非零,cmd 为非零,并且 name 参数已注册。

我认为我在 a 中使用 config 时发生了一些问题 persistentprerun 中的闭包,但我不知道具体原因 导致此失败。


正确答案


如果我使用 cmd.persistentflags().lookup("name"),我不会遇到任何问题。

// *** if i move this to the top of initconfig
    // *** the code runs correctly.
    pflag := cmd.persistentflags().lookup("name")
    config.bindpflag("name", pflag)

考虑到您刚刚注册了 persistent flags(该标志将适用于它分配给的命令以及该命令下的每个命令),调用 cmd.persistentflags().lookup("name") 更安全,而不是cmd.flags().lookup("名称")

后者返回 nil,因为只有在调用 rootcmd.execute() 时才会调用 persistentprerun,即在 cmd.newcmdroot() 之后
cmd.newcmdroot() 级别,标志尚未初始化,即使在某些标志被声明为“持久”之后也是如此。

我认为这很有趣,所以我做了一些挖掘和 found your exact problem documented in an issue。有问题的行是这样的:

config.bindpflag("name", cmd.flags().lookup("name"))
//                           ^^^^^^^

您创建了一个持久标志,但将该标志绑定到 flags 属性。如果您更改代码以绑定到 persistentflags,即使使用 newcmdroot 中的这一行,一切也会按预期工作:

config.BindPFlag("name", cmd.PersistentFlags().Lookup("name"))

理论要掌握,实操不能落!以上关于《调用 BindPFlag 时为什么会收到 nil 指针错误?》的详细介绍,大家都掌握了吧!如果想要继续提升自己的能力,那么就来关注golang学习网公众号吧!

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>