慕工程0101907
2023-07-10 14:25:25
我正在使用 VS 代碼編輯器在 go 中編寫 CLI。我無法弄清楚如何調(diào)試代碼部分。我的目錄結(jié)構(gòu)是:- test - main.go - cmd - login.go - root.go我已在 login.go 中設(shè)置斷點,但如果我在此文件中運行“開始調(diào)試”,則會出現(xiàn)錯誤Can not debug non-main packageProcess exiting with code: 1我嘗試在 main.go 中運行調(diào)試器,但調(diào)試器不會轉(zhuǎn)到 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"}請指導(dǎo)我如何在調(diào)試模式下查看變量值,例如變量名稱。雖然使用 Println 很好,但是這個源代碼是一個更大項目的一部分,所以我想看看如何使用調(diào)試器?
2 回答

小怪獸愛吃肉
TA貢獻1852條經(jīng)驗 獲得超1個贊
修改你的launch.json如下:
{
"version": "0.2.0",
"configurations": [
{
"name": "Launch",
"type": "go",
"request": "launch",
"mode": "debug",
"program": "${workspaceRoot}",
"env": {},
"args": [],
"port": 8080,
"host": "127.0.0.1"
}
]
}
您會發(fā)現(xiàn)與您的存在一些差異。
...
"mode": "debug",
"program": "${workspaceRoot}",
...

HUH函數(shù)
TA貢獻1836條經(jīng)驗 獲得超4個贊
"args": []
您可以在 vscode 設(shè)置中向數(shù)組添加標(biāo)志,如下所示:
"args": ["login", "-u", "username", "-p", "password"]
這將確保當(dāng)您運行調(diào)試時,您最終會進入帶有給定標(biāo)志的登錄命令。
- 2 回答
- 0 關(guān)注
- 155 瀏覽
添加回答
舉報
0/150
提交
取消