如何使用Go Delve调试器(dlv)的”display”命令来显示切片或映射的所有值

huangapple go评论76阅读模式
英文:

how to get Go Delve debugger (dlv) 'display' command to show all values of a slice or map

问题

我正在尝试使用Delve(dlv)的"display"命令来显示切片和映射的值。"print"命令显示完整的值,但"display"命令只显示"[...]"。

对比下面的"display"和"print"输出:

(dlv) display
0: gns = []string len: 2, cap: 2, [...]
1: chGnMap = map[string]int [...]
(dlv) p gns
[]string len: 2, cap: 2, ["ecam","site"]
(dlv) p chGnMap
map[string]int [
"ecam": 2,
"site": 2,
]
(dlv) config -list
aliases map[]
substitute-path []
max-string-len 1024
max-array-values 1024
max-variable-recurse 10
disassemble-flavor
show-location-expr false
source-list-line-color
source-list-arrow-color ""
source-list-keyword-color ""
source-list-string-color ""
source-list-number-color ""
source-list-comment-color ""
source-list-line-count
debug-info-directories [/usr/lib/debug/.build-id]
(dlv) exit

dlv version

Delve Debugger
Version: 1.7.2

英文:

I'm trying to use the Delve (dlv) "display" command to show the values of a slice and a map. The "print" command shows the full value but "display" only ever shows "[...]"

contrast the display and print output below

(dlv) display
0: gns = []string len: 2, cap: 2, [...]
1: chGnMap = map[string]int [...]
(dlv) p gns
[]string len: 2, cap: 2, ["ecam","site"]
(dlv) p chGnMap
map[string]int [
        "ecam": 2, 
        "site": 2, 
]
(dlv) config -list
aliases                   map[]
substitute-path           []
max-string-len            1024
max-array-values          1024
max-variable-recurse      10
disassemble-flavor        <not defined>
show-location-expr        false
source-list-line-color    <nil>
source-list-arrow-color   ""
source-list-keyword-color ""
source-list-string-color  ""
source-list-number-color  ""
source-list-comment-color ""
source-list-line-count    <not defined>
debug-info-directories    [/usr/lib/debug/.build-id]
(dlv) exit
# dlv version
Delve Debugger
Version: 1.7.2

答案1

得分: 2

这并不完全回答你的问题,但是:

当你添加显示变量 display -a ... 时,你可以引用字典中的一个键。

请参考以下步骤:

  1. 使用 display -a 添加带有提供的键的映射。
  2. 显示当前键不存在。
  3. 当程序继续执行时,键会自动添加。

注意:我需要在显示行末尾添加 [0],因为 handlerHeader["Content-Type"] 返回一个字符串切片。

(dlv) args
handler = (*main.ProduceHandler)(0x14000112d10)
wri = net/http.ResponseWriter(*net/http.response) 0x14000193708
req = (*net/http.Request)(0x14000182000)
(dlv) display -a wri.w.wr.res.handlerHeader["Content-Type"][0]
0: wri.w.wr.res.handlerHeader["Content-Type"][0] = 错误找不到键
(dlv) print %T wri.w.wr.res.handlerHeader
net/http.Header []
(dlv) n
> main.(*ProduceHandler).ServeHTTP() ./api.go:144 (PC: 0x100984480)
   139:		switch req.Method {
   140:		case http.MethodGet:
   141:			if len(req.URL.Query()["code"]) == 0 {
   142:				log.Println("发送整个产品数据库")
   143:				wri.Header().Add("Content-Type", "application/json")
=> 144:				wri.WriteHeader(http.StatusOK)
   145:				json.NewEncoder(wri).Encode(handler.DB)
   146:				return
   147:			}
   148:
   149:			c := req.URL.Query()["code"][0]
0: wri.w.wr.res.handlerHeader["Content-Type"][0] = "application/json"
英文:

This doesn't entirely answer your question, but:

When you are adding your display variables display -a ..., you can reference a key in the dictionary.

See steps below:

  1. Add map w/ key supplied using display -a
  2. Show that the key currently doesn't exist
  3. The key is automatically added when the program advances

> Note: I needed to append [0] to the display line because
> handlerHeader["Content-Type"] returns a string slice.

(dlv) args
handler = (*main.ProduceHandler)(0x14000112d10)
wri = net/http.ResponseWriter(*net/http.response) 0x14000193708
req = ("*net/http.Request")(0x14000182000)
(dlv) display -a wri.w.wr.res.handlerHeader["Content-Type"][0]
0: wri.w.wr.res.handlerHeader["Content-Type"][0] = error key not found
(dlv) print %T wri.w.wr.res.handlerHeader
net/http.Header []
(dlv) n
> main.(*ProduceHandler).ServeHTTP() ./api.go:144 (PC: 0x100984480)
   139:		switch req.Method {
   140:		case http.MethodGet:
   141:			if len(req.URL.Query()["code"]) == 0 {
   142:				log.Println("Sending entire produce database")
   143:				wri.Header().Add("Content-Type", "application/json")
=> 144:				wri.WriteHeader(http.StatusOK)
   145:				json.NewEncoder(wri).Encode(handler.DB)
   146:				return
   147:			}
   148:
   149:			c := req.URL.Query()["code"][0]
0: wri.w.wr.res.handlerHeader["Content-Type"][0] = "application/json"

huangapple
  • 本文由 发表于 2021年10月25日 02:29:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/69699656.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定