VS Code 中 main.go 以外的调试文件

Debug file other than main.go in VS Code

我正在使用 VS 代码编辑器在 go 中编写 CLI。我不知道如何调试代码部分。

我的目录结构是:

- test
  - main.go
  - cmd
    - login.go
    - root.go
  1. 我在 login.go 中设置了断点,但是如果我在此文件中 运行 "Start Debugging",我会得到错误
Can not debug non-main package
Process exiting with code: 1
  1. 我在 main.go 中尝试了 运行ning 调试器,但调试器不会转到 login.go 文件,因为我没有明确编写 test login
API server listening at: 127.0.0.1:48423
A longer description that spans multiple lines and likely contains
examples 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 files
to quickly create a Cobra application.

Usage:
  test [command]

Available Commands:
  help        Help about any command
  login       A brief description of your command

Flags:
      --config string   config file (default is $HOME/.test.yaml)
  -h, --help            help for test
  -t, --toggle          Help message for toggle

Use "test [command] --help" for more information about a command.
  1. main.go 文件
package main

import "test/cmd"

func main() {
  cmd.Execute()
}
  1. login.go 文件
package cmd

import (
    "fmt"
    "github.com/spf13/cobra"
)

// loginCmd represents the login command
var 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")
}
  1. settings.json
{
    "go.gopath":"/Users/deepakpatankar/go"
}
  1. launch.json
{
    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Launch",
            "type": "go",
            "request": "launch",
            "mode": "auto",
            "program": "${fileDirname}",
            "env": {},
            "args": []
        }
    ]
}

请指导我如何在调试模式下查看变量值,例如变量名。虽然使用 Println 很好,但是这个源代码是一个更大项目的一部分,所以我想看看如何使用调试器?

您可以在 vscode 设置中向 "args": [] 数组添加标志,如下所示:

"args": ["login", "-u", "username", "-p", "password"]

这将确保当您 运行 调试时,您最终会进入带有给定标志的登录命令。

修改您的 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}",
...