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

go - “godef: There must be at least one package that contains the file”即使该文件确实具有包?-LMLPHP

这是在我使用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

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

最佳答案

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

关于go - “godef: There must be at least one package that contains the file”即使该文件确实具有包?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/58087134/

10-10 19:12