简体   繁体   English

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

[英]Debug file other than main.go in VS Code

I am writing a CLI in go using VS code editor. 我正在使用VS代码编辑器编写CLI。 I am not able to figure out how to debug a code section. 我无法弄清楚如何调试代码段。

My directory structure is : 我的目录结构是:

- test
  - main.go
  - cmd
    - login.go
    - root.go
  1. I have set breakpoints in login.go but if I run "Start Debugging" in this file, I get error 我已经在login.go中设置了断点,但是如果我在此文件中运行“开始调试”,则会收到错误消息
Can not debug non-main package
Process exiting with code: 1
  1. I tried running debugger in main.go but the debugger won't go to login.go file as I we have not explicitly written test login 我尝试在main.go中运行调试器,但由于我们未明确编写test login因此调试器不会进入login.go文件
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 file main.go文件
package main

import "test/cmd"

func main() {
  cmd.Execute()
}
  1. login.go file 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": []
        }
    ]
}

Please guide me how I can see the variable values in debug mode like for variable name. 请指导我如何在调试模式下查看变量值,例如变量名。 Though using Println is fine, but this source code is part of a bigger project, so I want to see how I can use the debugger ? 尽管使用Println很好,但是此源代码是更大项目的一部分,所以我想看看如何使用调试器?

You can add flags to the "args": [] array in your vscode settings like this: 您可以在vscode设置中将标志添加到"args": []数组中,如下所示:

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

This will make sure when you run debug you end up in the login command with the given flags. 这样可以确保在运行debug时,您最终获得带有给定标志的login命令。

Modify your launch.json as below: 如下修改您的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"
        }
    ]
}

You'll learn that some differences are there from yours. 您将了解到与您之间存在一些差异。

...
"mode": "debug",
"program": "${workspaceRoot}",
...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM