Go入门到入土(一):开发环境安装与调试
1. 安装 Go
按照以下步骤安装 Go:
- 在 Web 浏览器中,转到 “go.dev/doc/install”。
- 下载操作系统的版本。
- 下载后,运行安装程序。
- 打开命令提示符,然后运行 go version 以确认已安装 Go。
2. 安装Visual Studio Code
按照以下步骤安装Visual Studio Code:
- 在 Web 浏览器中,转到 “code.visualstudio.com”。
- 下载操作系统的版本,支持 Windows、Linux 和 macOS。
- 下载后,运行安装程序。
3. 安装 Go 扩展
- 在“Visual Studio Code”中,单击活动栏中的“扩展”图标,打开“扩展”视图。 或者 (Ctrl+Shift+X) 使用键盘快捷方式。
- 搜索 Go 扩展,然后选择“安装”。
4. 更新 Go 工具
- 在Visual Studio Code中,打开命令面板的“帮助>显示所有命令”。 或者使用键盘快捷方式 (Ctrl+Shift+P)
- Go: Install/Update tools搜索,然后从托盘运行命令。
- 出现提示时,选择所有可用的 Go 工具,然后单击“确定”。
- 等待 Go 工具完成更新。
过程中可能需要科学。
5. 编写示例 Go 程序
- 在Visual Studio Code中,打开将在其中创建 Go 应用程序的根目录的文件夹。 若要打开文件夹,请单击活动栏中的“资源管理器”图标,然后单击“ 打开文件夹”。
- 单击“资源管理器”面板中的“ 新建文件夹 ”,然后为名为“Go”的示例应用程序创建根控制器 sample-app
- 单击资源管理器面板中的“ 新建文件 ”,然后为文件命名 main.go
- 打开终端 “终端 > ”“新建终端”,然后运行 命令 go mod init sample-app 以初始化示例 Go 应用。
- 将以下代码复制到 文件中 main.go 。
package main
import "fmt"
func main() {
name := "Go Developers"
fmt.Println("Azure for", name)
}
6. 运行调试器
- 通过在编号行左侧单击左侧,在行 7 上创建断点。 或者将光标放在第 7 行并点击 F9。
- 单击Visual Studio Code一侧的活动栏中的调试图标,打开“调试”视图。 或者 (Ctrl+Shift+D) 使用键盘快捷方式。
- 单击“ 运行并调试”,或按 F5 运行调试器。 然后将鼠标悬停在第 7 行上的变量 name 上以查看其值。 单击调试器栏上的“ 继续 ”或按 F5 退出调试器。
7. 可能遇见的问题
dlv: failed to install dlv
Installing 1 tool at C:\Users\xxx\go\bin in module mode.
dlv
Installing github.com/go-delve/delve/cmd/dlv@latest FAILED
{
"killed": false,
"code": 1,
"signal": null,
"cmd": "C:\\Program Files (x86)\\Go\\bin\\go.exe install -v github.com/go-delve/delve/cmd/dlv@latest",
"stdout": "",
"stderr": "C:\\Users\\xxx\\go\\pkg\\mod\\github.com\\go-delve\\[email protected]\\service\\debugger\\debugger.go:32:2: found packages native (proc.go) and your_windows_architecture_is_not_supported_by_delve (support_sentinel_windows.go) in C:\\Users\\xxx\\go\\pkg\\mod\\github.com\\go-delve\\[email protected]\\pkg\\proc\\native\n"
}
1 tools failed to install.
dlv: failed to install dlv(github.com/go-delve/delve/cmd/dlv@latest): Error: Command failed: C:\Program Files (x86)\Go\bin\go.exe install -v github.com/go-delve/delve/cmd/dlv@latest
C:\Users\xxx\go\pkg\mod\github.com\go-delve\[email protected]\service\debugger\debugger.go:32:2: found packages native (proc.go) and your_windows_architecture_is_not_supported_by_delve (support_sentinel_windows.go) in C:\Users\xxx\go\pkg\mod\github.com\go-delve\[email protected]\pkg\proc\native
原因:
your windows architecture is not supported by delve。
delve不支持您的Windows体系结构。
解决方法:卸载并安装正确的go版本。
Couldn't start dlv dap:Error: timed out while waiting for DAP server to start
解决方法:在配置文件中加上, "debugAdapter": "legacy",