溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶(hù)服務(wù)條款》

VSCode對(duì)Golang程序進(jìn)行調(diào)試時(shí)報(bào)錯(cuò)的解決方法

發(fā)布時(shí)間:2020-06-26 00:47:18 來(lái)源:億速云 閱讀:1901 作者:Leah 欄目:開(kāi)發(fā)技術(shù)

VSCode對(duì)Golang程序進(jìn)行調(diào)試時(shí)報(bào)錯(cuò)的解決方法?針對(duì)這個(gè)問(wèn)題,這篇文章詳細(xì)介紹了相對(duì)應(yīng)的分析和解答,希望可以幫助更多想解決這個(gè)問(wèn)題的小伙伴找到更簡(jiǎn)單易行的方法。

使用VSCode對(duì)Golang程序進(jìn)行調(diào)試時(shí)會(huì)遇到數(shù)據(jù)截?cái)鄦?wèn)題,string只顯示前64個(gè)字符,array只顯示前64個(gè)數(shù)據(jù)。經(jīng)查dlv是支持以參數(shù)方式來(lái)控制的。

發(fā)現(xiàn)VSCode的Golang插件里面有個(gè)叫做go.delveConfig的配置,是可以設(shè)置dlv參數(shù)的。分享一下我的整個(gè)Golang配置:

"go.buildOnSave": "off",
  "go.formatTool": "goimports",
  "go.lintTool": "golangci-lint", //go get -u github.com/golangci/golangci-lint/cmd/golangci-lint
  "go.autocompleteUnimportedPackages": true,
  "go.gotoSymbol.includeImports": true,
  "go.useLanguageServer": true,
  "go.delveConfig": {
    "dlvLoadConfig": {
      "followPointers": true,
      "maxVariableRecurse": 3,
      "maxStringLen": 1024,
      "maxArrayValues": 1024,
      "maxStructFields": -1
    },
  },
  "[go]": {
    "editor.formatOnSave": true,
    "editor.codeActionsOnSave": {
      "source.organizeImports": true
    }
  },

需要改的主要是maxStringLen、maxArrayValues、maxVariableRecurse這三個(gè)字段。

參考:https://stackoverflow.com/questions/52416263/how-do-i-print-the-full-value-of-a-string-variable-in-delve

ps:下面看下Golang dlv 工具debug 調(diào)試注意項(xiàng)

總結(jié)一下關(guān)于Go 的調(diào)試工具dlv:https://github.com/derekparker/delve 的使用注意項(xiàng)。

安裝:

go get -u github.com/go-delve/delve/cmd/dlv

配置:

以Centos為例

export GOROOT=/usr/lib/golang
export GOPATH=$HOME/go
export PATH=$PATH:$GOPATH/bin

使用

以某go服務(wù)為例:

  • dlv debug xxx.go 指定需要debug的文件
  • 進(jìn)入dlv交互式窗口后,b <filename>:<line> 指定斷點(diǎn)
  • r arg 指定運(yùn)行參數(shù)
  • n 執(zhí)行一行
  • c 運(yùn)行至斷點(diǎn)或程序結(jié)束
dlv debug /home/xxx/server.go
(dlv) b /home/xxx/server.go:258
(dlv) r 1
(dlv) n
(dlv) c

注意: b <filename>:<line> 指定斷點(diǎn)時(shí),若該行號(hào)對(duì)應(yīng)的代碼內(nèi)容為無(wú)具體語(yǔ)義的代碼(括號(hào)、注釋等),則會(huì)報(bào)錯(cuò):

Command failed: could not find /home/xxx/server.go:258

此時(shí)可用list 命令先查看上下文代碼,避免將無(wú)具體語(yǔ)義的代碼設(shè)為斷點(diǎn)。

命令集

The following commands are available:
    args ------------------------ Print function arguments.
    break (alias: b) ------------ Sets a breakpoint.
    breakpoints (alias: bp) ----- Print out info for active breakpoints.
    call ------------------------ Resumes process, injecting a function call (EXPERIMENTAL!!!)
    clear ----------------------- Deletes breakpoint.
    clearall -------------------- Deletes multiple breakpoints.
    condition (alias: cond) ----- Set breakpoint condition.
    config ---------------------- Changes configuration parameters.
    continue (alias: c) --------- Run until breakpoint or program termination.
    deferred -------------------- Executes command in the context of a deferred call.
    disassemble (alias: disass) - Disassembler.
    down ------------------------ Move the current frame down.
    edit (alias: ed) ------------ Open where you are in $DELVE_EDITOR or $EDITOR
    exit (alias: quit | q) ------ Exit the debugger.
    frame ----------------------- Set the current frame, or execute command on a different frame.
    funcs ----------------------- Print list of functions.
    goroutine ------------------- Shows or changes current goroutine
    goroutines ------------------ List program goroutines.
    help (alias: h) ------------- Prints the help message.
    list (alias: ls | l) -------- Show source code.
    locals ---------------------- Print local variables.
    next (alias: n) ------------- Step over to next source line.
    on -------------------------- Executes a command when a breakpoint is hit.
    print (alias: p) ------------ Evaluate an expression.
    regs ------------------------ Print contents of CPU registers.
    restart (alias: r) ---------- Restart process.
    set ------------------------- Changes the value of a variable.
    source ---------------------- Executes a file containing a list of delve commands
    sources --------------------- Print list of source files.
    stack (alias: bt) ----------- Print stack trace.
    step (alias: s) ------------- Single step through program.
    step-instruction (alias: si)  Single step a single cpu instruction.
    stepout --------------------- Step out of the current function.
    thread (alias: tr) ---------- Switch to the specified thread.
    threads --------------------- Print out info for every traced thread.
    trace (alias: t) ------------ Set tracepoint.
    types ----------------------- Print list of types
    up -------------------------- Move the current frame up.
    vars ------------------------ Print package variables.
    whatis ---------------------- Prints type of an expression.

關(guān)于VSCode對(duì)Golang程序進(jìn)行調(diào)試時(shí)報(bào)錯(cuò)的解決方法問(wèn)題的解答就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,如果你還有很多疑惑沒(méi)有解開(kāi),可以關(guān)注億速云行業(yè)資訊頻道了解更多相關(guān)知識(shí)。

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI