使用Visual Studio Code调试Go代码

I recently wrote about My Top VSCode Tips and Tricks and among them was Debugging.

我最近写了有关My Top VSCode技巧和窍门的文章 ,其中包括Debugging。

In this article, I'll cover how to debug your Go code, so it goes without saying that a prerequisite for this article is that you understand the basics of writing Go.

在本文中,我将介绍如何调试Go代码,因此不用说,本文的前提条件是您了解编写Go的基础。

If you do not have experience writing Go, and would like to learn, here's a really short short list of the best resource to start your Go journey.

如果您没有编写Go语言的经验,并且想学习,那么这里是开始您的Go语言之旅的最佳资源的简短简短清单。

建立 ( Setup )

You need have the following requirements met.

您需要满足以下要求。

  1. You know a little about Go to write a simple program, or even a small http server.

    您对Go编写一个简单的程序甚至一个小型的http服务器有所了解。
  2. You have go installed in your machine and the environment variable $GOPATH is set. (It defaults to ~/go)

    您已经安装了计算机,并设置了环境变量$ GOPATH。 (默认为〜/ go)
  3. The path $GOPATH/bin is exposed to your PATH.

    $GOPATH/bin路径公开给您的PATH。
  4. You have Visual Studio Code installed.

    您已经安装了Visual Studio Code。
  5. You should have the VSCode-Go plugin installed.

    您应该已经安装了VSCode-Go插件

Once you have this installed, when open any .go files in VSCode, you will be prompted on the bottom right of the status bar to Install Analysis Tools, and you should click on that link to install the necessary Go packages for the plugin to work efficiently.

一旦你安装了这个,当打开任何.go在VSCode文件,你会在状态栏右下角的提示Install Analysis Tools ,你应该是点击链接安装必要去包插件工作有效率的。

We finally need to install a debugger for Go, called Delve. It's an open source project. To do this, there are detailed installation instructions for specific platforms. I'm using a Mac, so it's as straight forward as.

最后,我们需要为Go安装一个名为Delve的调试器。 这是一个开源项目。 为此,有针对特定平台的详细安装说明 。 我使用的是Mac,因此非常简单。

brewinstall go-delve/delve/delve

例子 ( Examples )

We'll use two examples to debug our Go code.

我们将使用两个示例来调试我们的Go代码。

  • A simple go program that generates a JSON.

    一个生成JSON的简单go程序。
  • We'll write a function, write the test and see how we debug tests in VS Code

    我们将编写一个函数,编写测试并查看如何在VS Code中调试测试

示例1:复仇者 (Example 1: Avengers)

Here's the source code for the first example. Create a file main.go.

这是第一个示例的源代码。 创建一个文件main.go

package main

import (
    "encoding/json"
    "fmt"
    "log"
)

// Avenger represents a single hero
type Avenger struct {
    
    RealName string `json:"real_name"`
    HeroName string `json:"hero_name"`
    Planet   <
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Visual Studio Code是一款非常受欢迎的开源代码编辑器,特别是在Go语言开发领域。根据Go开发人员调查,Visual Studio Code是最常用的Go编译器之一。使用Visual Studio Code进行Go语言开发可以提供IntelliSense智能代码补全、代码导航和高级调试等功能。 要使用Visual Studio Code进行Go语言开发,首先需要安装Go环境[1]。可以从官方网站(https://golang.org/dl/)下载适合您操作系统的Go安装包,并按照说明进行安装。 安装完Go环境后,接下来需要安装Visual Studio Code本身。您可以从官方网站(https://code.visualstudio.com/)下载适合您操作系统的安装包,并按照说明进行安装。 安装完成后,打开Visual Studio Code。您可以在插件市场中搜索并安装Go扩展,该扩展将为您提供Go开发所需的功能和特性。在Visual Studio Code中,按下Ctrl+Shift+X(Windows/Linux)或Cmd+Shift+X(Mac)打开扩展面板,然后搜索"Go"并安装Go扩展。 安装完Go扩展后,您就可以开始在Visual Studio Code中编写、运行和调试Go程序了。您可以创建一个新的Go文件,并使用Go语言的语法进行代码编写。Visual Studio Code的IntelliSense功能将会根据您的代码自动提供智能的代码补全和建议。 当您编写完代码后,您可以按下F5键或通过菜单中的调试选项来启动调试Visual Studio Code内置了强大的调试功能,可以帮助您快速定位和解决问题。 总结起来,要在Visual Studio Code使用Go语言进行开发,您需要安装Go环境和Visual Studio Code本身,并安装Go扩展。然后,您就可以开始编写、运行和调试Go程序了。 希望这些信息对您有所帮助!如果您还有其他问题,请随时提问。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值