duanjiao5723 2019-09-24 19:38
浏览 343

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

I'm using VS Code with the Go extension, but I noticed that when I right-click on a method and select "Go to definition", no definition is found. For example,

enter image description here

This is in a sample app I generated using Cobra:

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

I would expect this to work because the myCobraApp directory is organized like

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

where cmd/root.go contains

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)
  }
}

I've also tried to invoke godef from the command line, but I get this error message:

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

I don't understand this error message: isn't there a package that contains the file?

  • 写回答

1条回答 默认 最新

  • duanguai2781 2019-09-24 19:47
    关注

    It turns out this functionality was broken by setting the GO111MODULE environment variable to on in my ~/.config/fish/config.fish. When I removed this, the 'Go-To Definition' button worked again.

    评论

报告相同问题?

悬赏问题

  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3