簡體   English   中英

VS Code中main.go以外的調試文件

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

我正在使用VS代碼編輯器編寫CLI。 我無法弄清楚如何調試代碼段。

我的目錄結構是:

- test
  - main.go
  - cmd
    - login.go
    - root.go
  1. 我已經在login.go中設置了斷點,但是如果我在此文件中運行“開始調試”,則會收到錯誤消息
Can not debug non-main package
Process exiting with code: 1
  1. 我嘗試在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文件
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"]

這樣可以確保在運行debug時,您最終獲得帶有給定標志的login命令。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM