我正在使用 VS 代碼編輯器在 go 中編寫 CLI。我無法弄清楚如何調試代碼部分。我的目錄結構是:- test - main.go - cmd - login.go - root.go我已在 login.go 中設置斷點,但如果我在此文件中運行“開始調試”,則會出現錯誤Can not debug non-main packageProcess exiting with code: 1我嘗試在 main.go 中運行調試器,但調試器不會轉到 login.go 文件,因為我們沒有明確編寫test loginAPI server listening at: 127.0.0.1:48423A longer description that spans multiple lines and likely containsexamples and usage of using your application. For example:cd .Cobra is a CLI library for Go that empowers applications.This application is a tool to generate the needed filesto quickly create a Cobra application.Usage: test [command]Available Commands: help Help about any command login A brief description of your commandFlags: --config string config file (default is $HOME/.test.yaml) -h, --help help for test -t, --toggle Help message for toggleUse "test [command] --help" for more information about a command.main.go文件package mainimport "test/cmd"func main() { cmd.Execute()}login.go文件package cmdimport ( "fmt" "github.com/spf13/cobra")// loginCmd represents the login commandvar loginCmd = &cobra.Command{ Run: func(cmd *cobra.Command, args []string) { fmt.Println("login called") name, _ := cmd.Flags().GetString("username") pwd, _ := cmd.Flags().GetString("password") userInfo := name + ":" + pwd },}func init() { rootCmd.AddCommand(loginCmd) // Here you will define your flags and configuration settings. loginCmd.Flags().StringP("username", "u", "", "Specifies the user") loginCmd.Flags().StringP("password", "p", "", "Specifies the password for the user") loginCmd.Flags().StringP("manager", "m", "", "Specifies the environement where user wants to login")}settings.json{ "go.gopath":"/Users/deepakpatankar/go"}請指導我如何在調試模式下查看變量值,例如變量名稱。雖然使用 Println 很好,但是這個源代碼是一個更大項目的一部分,所以我想看看如何使用調試器?
在 VS Code 中調試除 main.go 之外的文件
慕工程0101907
2023-07-10 14:25:25