简体   繁体   English

在Visual Studio代码中运行Golang

[英]Running Golang in Visual Studio Code

Can anyone tell me how can I run and use debugger in Visual Studio for a simple go program in Windows step by step? 任何人都可以告诉我如何在Windows中运行并使用调试器逐步在Windows中进行简单的go程序?

I took a reference from this page: 我从这个页面中引用了一个参考:

https://marketplace.visualstudio.com/items?itemName=lukehoban.Go https://marketplace.visualstudio.com/items?itemName=lukehoban.Go

But I was not able to run the code. 但我无法运行代码。 My current GOPATH is C:\\dev\\go. 我目前的GOPATH是C:\\ dev \\ go。 Please let me know if any other details are required. 如果需要任何其他细节,请告诉我。

First of all, debuggers are not part of Go. 首先,调试器不是Go的一部分。 C#, F# and other managed languages under the Microsoft stack have debuggers as that's part of the .NET stack. Microsoft堆栈下的C#,F#和其他托管语言具有调试器,因为它是.NET堆栈的一部分。

Second, Visual Studio Code != Visual Studio. 二,Visual Studio Code!= Visual Studio。 VS Code is a light(er) weight IDE that is geared towards extensibility to support a wide range of languages by creating runners. VS Code是一款轻量级的IDE,旨在通过创建跑步者来扩展以支持各种语言。 But that's the thing: someone else needs to write the runners and hopefully they created a seamless experience with a Debugger (if available). 但事情就是这样:其他人需要编写跑步者,希望他们使用调试器(如果可用)创建无缝体验。 This is why you have multiple versions of language runners. 这就是为什么你有多个版本的语言运行器。

In other words: if you want a VSCode-compatible Debugger+Runner for X language, read up on X language about how to debug it. 换句话说: 如果你想要一个兼容VSCode的Debugger + Runner for X语言,请阅读X语言,了解如何调试它。

Go is no exception. 围棋也不例外。 You must read the language spec, and specifically I recommend Effective Go as it explains why you don't need a Debugger . 您必须阅读语言规范,特别是我推荐Effective Go,因为它解释了为什么您不需要调试器

-- -

Now with all of that said, the community has come together and created somewhat of a debugger for GoLang. 现在所有这些都说,社区已经聚集在一起,为GoLang创建了一些调试器。 It is called Delve . 它被称为Delve

Learning how to install it for VS Code is beyond this post. 学习如何为VS Code安装它超出了这篇文章。 I recommend finding a VSCode package that supports Go coding with Delve (there is at least one out there, as I have used it). 我建议找一个支持Go编码的VSCode包用Delve(至少有一个,因为我已经使用过它)。

Opinion: it's an Ok experience in VSCode to debug Go. 意见:在VSCode中调试Go是一种很好的体验。 I've experimented with it. 我已经试过了。 While visually pleasing, I went back to Atom for it's large package support of many other Go utilities and Linters - most of which is missing in VSCode (and some packages didn't allow me to modify the config to exclude certain Go workflows). 虽然视觉上令人愉悦,但我回到了Atom,因为它支持许多其他Go实用程序和Linters - 大多数在VSCode中缺少(有些软件包不允许我修改配置以排除某些Go工作流程)。

EDIT 2018: After a few years, VSCode has matured nicely! 编辑2018年:几年后,VSCode已经成熟了! I've since switched 100% to VSCode as my primary editor. 我已经100%切换到VSCode作为我的主编辑。

One possible option to install Debugger for Go language on Windows is: 在Windows上安装Debugger for Go语言的一个可能选项是:

go get github.com/derekparker/delve/cmd/dlv

After that the Visual Studio Code (vscode) will be able to run (debug) launch configuration. 之后,Visual Studio Code(vscode)将能够运行(调试)启动配置。

By default, file launch.json points to the root of the project: 默认情况下,文件launch.json指向项目的根目录:

"program": "${workspaceRoot}"

If you want configure it to the another location (eg. your/package/foo/dir) then edit it as the following: 如果要将其配置到另一个位置(例如,您的/ package / foo / dir),请按以下方式对其进行编辑:

"program": "${workspaceRoot}/foo/dir"

In case you want to debug a Go module running inside an application server you can have a look at debugging golang appengine module with visual studio code . 如果您想调试在应用程序服务器内运行的Go模块,您可以查看使用visual studio代码调试golang appengine模块

Explanations are for Appengine server, but you can easily understand how to do remote debugging from vscode using Delve. 解释是针对Appengine服务器的,但您可以使用Delve轻松了解如何使用vscode进行远程调试。

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

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