首頁  >  文章  >  後端開發  >  Visual Studio Code 的偵錯器可以使用 Go 程式的建置標籤嗎?

Visual Studio Code 的偵錯器可以使用 Go 程式的建置標籤嗎?

Patricia Arquette
Patricia Arquette原創
2024-10-24 13:18:02199瀏覽

Can Visual Studio Code's Debugger Use Build Tags for Go Programs?

在Visual Studio Code 和Delve 中進行調試標記

使用構建標記調試Go 程式可能是一個挑戰,尤其是在使用Visual Studio Code 和Delve 調試器時。

原問題:

偵錯器可以配置為在編譯 Go 程式時使用指定的建置標籤嗎?

答案:

Visual Studio Code 的 Go 插件現在支援 launch.json 中的 buildFlags 鍵。這允許開發人員使用“-tags Tag”格式指定建置標籤。

設定:

使用標籤建立二進位檔案的任務:

<code class="json">{
  "version": "0.1.0",
  "command": "bash",
  "isShellCommand": true,
  "args": [""],
  "showOutput": "always",
  "tasks": [
    {
      "taskName": "buildBinWithTag",
      "command": "go",
      "args": ["build", "-o", "BinaryName", "-tags", "THISISATAG"],
      "isShellCommand": true
    }
  ]
}</code>

啟動設定:>

<code class="json">{
  "version": "0.2.0",
  "configurations": [
    {
      "name": "DebugBinWithTag",
      "type": "go",
      "request": "launch",
      "mode": "exec",
      "remotePath": "",
      "port": 2345,
      "host": "127.0.0.1",
      "program": "${workspaceRoot}/BinaryName",
      "env": {},
      "args": [],
      "showLog": true,
      "preLaunchTask": "buildBinWithTag"
    }
  ]
}</code>
啟動設定:

範例:

<code class="go">//+build THISISAFLAG

package main</code>

使用建置標籤「build THISISAFLAG」 :

<code class="json">{
  "version": "0.2.0",
  "configurations": [
    {
      "name": "DebugWithTag",
      "type": "go",
      "request": "launch",
      "mode": "exec",
      ...
      "buildFlags": "-tags THISISAFLAG",
      ...
    }
  ]
}</code>
啟動設定: 🎜>

此配置將確保偵錯器在編譯期間使用指定的建置標記,從而允許執行和偵錯具有不同標籤相關配置的Go 程式。

以上是Visual Studio Code 的偵錯器可以使用 Go 程式的建置標籤嗎?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn