Go入門到入土(一):開發環境安裝與調試

Go入門到入土(一):開發環境安裝與調試

1. 安裝 Go

按照以下步驟安裝 Go:

  1. 在 Web 瀏覽器中,轉到 “go.dev/doc/install”。
  2. 下載操作系統的版本。
  3. 下載後,運行安裝程序。
  4. 打開命令提示符,然後運行 go version 以確認已安裝 Go。

2. 安裝Visual Studio Code

按照以下步驟安裝Visual Studio Code:

  1. 在 Web 瀏覽器中,轉到 “code.visualstudio.com”。
  2. 下載操作系統的版本,支持 Windows、Linux 和 macOS。
  3. 下載後,運行安裝程序。

3. 安裝 Go 擴展

  1. 在“Visual Studio Code”中,單擊活動欄中的“擴展”圖標,打開“擴展”視圖。 或者 (Ctrl+Shift+X) 使用鍵盤快捷方式。1
  2. 搜索 Go 擴展,然後選擇“安裝”。2

4. 更新 Go 工具

  1. 在Visual Studio Code中,打開命令面板的“幫助>顯示所有命令”。 或者使用鍵盤快捷方式 (Ctrl+Shift+P)1
  2. Go: Install/Update tools搜索,然後從托盤運行命令。1
  3. 出現提示時,選擇所有可用的 Go 工具,然後單擊“確定”。1
  4. 等待 Go 工具完成更新。1

過程中可能需要科學。

5. 編寫示例 Go 程序

  1. 在Visual Studio Code中,打開將在其中創建 Go 應用程序的根目錄的文件夾。 若要打開文件夾,請單擊活動欄中的“資源管理器”圖標,然後單擊“ 打開文件夾”。1
  2. 單擊“資源管理器”面板中的“ 新建文件夾 ”,然後爲名爲“Go”的示例應用程序創建根控制器 sample-app2
  3. 單擊資源管理器面板中的“ 新建文件 ”,然後爲文件命名 main.go3
  4. 打開終端 “終端 > ”“新建終端”,然後運行 命令 go mod init sample-app 以初始化示例 Go 應用。4
  5. 將以下代碼複製到 文件中 main.go 。5
package main

import "fmt"

func main() {
    name := "Go Developers"
    fmt.Println("Azure for", name)
}

6. 運行調試器

  1. 通過在編號行左側單擊左側,在行 7 上創建斷點。 或者將光標放在第 7 行並點擊 F9。1
  2. 單擊Visual Studio Code一側的活動欄中的調試圖標,打開“調試”視圖。 或者 (Ctrl+Shift+D) 使用鍵盤快捷方式。2
  3. 單擊“ 運行並調試”,或按 F5 運行調試器。 然後將鼠標懸停在第 7 行上的變量 name 上以查看其值。 單擊調試器欄上的“ 繼續 ”或按 F5 退出調試器。3

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

img

解決方法:在配置文件中加上, "debugAdapter": "legacy",

img

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章