“ godef:必须至少包含一个包含该文件的软件包”,即使该文件确实具有软件包?

时间:2019-09-24 19:38:00

标签: go visual-studio-code

我正在使用带有Go扩展名的VS Code,但是我注意到当我右键单击某个方法并选择“转到定义”时,找不到任何定义。例如,

enter image description here

这是在我使用Cobra生成的示例应用程序中:

cobra init myCobraApp --pkg-name=github.com/khpeek/myCobraApp

我希望它能正常工作,因为myCobraApp目录的组织方式类似于

.
├── LICENSE
├── cmd
│   └── root.go
└── main.go

cmd/root.go所在的地方

package cmd

import (
  "fmt"
  "os"
  "github.com/spf13/cobra"

  homedir "github.com/mitchellh/go-homedir"
  "github.com/spf13/viper"

)


var cfgFile string


// rootCmd represents the base command when called without any subcommands
var rootCmd = &cobra.Command{
  Use:   "myCobraApp",
  Short: "A brief description of your application",
  Long: `A longer description that spans multiple lines and likely contains
examples and usage of using your application. For example:

Cobra is a CLI library for Go that empowers applications.
This application is a tool to generate the needed files
to quickly create a Cobra application.`,
  // Uncomment the following line if your bare application
  // has an action associated with it:
  //    Run: func(cmd *cobra.Command, args []string) { },
}

// Execute adds all child commands to the root command and sets flags appropriately.
// This is called by main.main(). It only needs to happen once to the rootCmd.
func Execute() {
  if err := rootCmd.Execute(); err != nil {
    fmt.Println(err)
    os.Exit(1)
  }
}

我也尝试从命令行调用godef,但收到以下错误消息:

~/g/s/g/k/myCobraApp> godef -f main.go "cmd.Execute()"
godef: There must be at least one package that contains the file

我不明白此错误消息:没有包含该文件的软件包吗?

2 个答案:

答案 0 :(得分:1)

事实证明,通过在我的GO111MODULE中将on环境变量设置为~/.config/fish/config.fish破坏了此功能。当我删除它时,“转到定义”按钮再次起作用。

答案 1 :(得分:0)

错误:godef:必须至少有一个包含该文件的包。

enter image description here

似乎在 go 1.16 环境变量 (GO111MODULE) 中设置了 auto/on

您可以通过简单地导出环境变量来关闭 GO111MODULE。

export GO111MODULE=off