duanjiao5723 2019-09-24 11:38
浏览 347

“ 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 11: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.

    评论
    编辑
    预览

    报告相同问题?

    手机看
    程序员都在用的中文IT技术交流社区

    程序员都在用的中文IT技术交流社区

    专业的中文 IT 技术社区,与千万技术人共成长

    专业的中文 IT 技术社区,与千万技术人共成长

    关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

    关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

    客服 返回
    顶部