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,
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?