登录
首页 >  Golang >  Go问答

在 VS Code 中调试除 main.go 之外的文件

来源:stackoverflow

时间:2024-03-14 11:57:29 395浏览 收藏

小伙伴们对Golang编程感兴趣吗?是否正在学习相关知识点?如果是,那么本文《在 VS Code 中调试除 main.go 之外的文件》,就很适合你,本篇文章讲解的知识点主要包括。在之后的文章中也会多多分享相关知识点,希望对大家的知识积累有所帮助!

问题内容

我正在使用 vs 代码编辑器在 go 中编写 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 中运行调试器,但调试器不会转到 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}",
...

本篇关于《在 VS Code 中调试除 main.go 之外的文件》的介绍就到此结束啦,但是学无止境,想要了解学习更多关于Golang的相关知识,请关注golang学习网公众号!

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>