我正在使用 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 很好,但是这个源代码是一个更大项目的一部分,所以我想看看如何使用调试器?
2 回答
![?](http://img1.sycdn.imooc.com/545847d40001cbef02200220-100-100.jpg)
小怪兽爱吃肉
TA贡献1852条经验 获得超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"
}
]
}
您会发现与您的存在一些差异。
...
"mode": "debug",
"program": "${workspaceRoot}",
...
![?](http://img1.sycdn.imooc.com/5458626a0001503602200220-100-100.jpg)
HUH函数
TA贡献1836条经验 获得超4个赞
"args": []
您可以在 vscode 设置中向数组添加标志,如下所示:
"args": ["login", "-u", "username", "-p", "password"]
这将确保当您运行调试时,您最终会进入带有给定标志的登录命令。
- 2 回答
- 0 关注
- 109 浏览
添加回答
举报
0/150
提交
取消