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

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

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

  1. (dlv) display
  2. 0: gns = []string len: 2, cap: 2, [...]
  3. 1: chGnMap = map[string]int [...]
  4. (dlv) p gns
  5. []string len: 2, cap: 2, ["ecam","site"]
  6. (dlv) p chGnMap
  7. map[string]int [
  8. "ecam": 2,
  9. "site": 2,
  10. ]
  11. (dlv) config -list
  12. aliases map[]
  13. substitute-path []
  14. max-string-len 1024
  15. max-array-values 1024
  16. max-variable-recurse 10
  17. disassemble-flavor <not defined>
  18. show-location-expr false
  19. source-list-line-color <nil>
  20. source-list-arrow-color ""
  21. source-list-keyword-color ""
  22. source-list-string-color ""
  23. source-list-number-color ""
  24. source-list-comment-color ""
  25. source-list-line-count <not defined>
  26. debug-info-directories [/usr/lib/debug/.build-id]
  27. (dlv) exit
  28. # dlv version
  29. Delve Debugger
  30. Version: 1.7.2

答案1

得分: 2

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

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

请参考以下步骤:

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

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

  1. (dlv) args
  2. handler = (*main.ProduceHandler)(0x14000112d10)
  3. wri = net/http.ResponseWriter(*net/http.response) 0x14000193708
  4. req = (*net/http.Request)(0x14000182000)
  5. (dlv) display -a wri.w.wr.res.handlerHeader["Content-Type"][0]
  6. 0: wri.w.wr.res.handlerHeader["Content-Type"][0] = 错误找不到键
  7. (dlv) print %T wri.w.wr.res.handlerHeader
  8. net/http.Header []
  9. (dlv) n
  10. > main.(*ProduceHandler).ServeHTTP() ./api.go:144 (PC: 0x100984480)
  11. 139: switch req.Method {
  12. 140: case http.MethodGet:
  13. 141: if len(req.URL.Query()["code"]) == 0 {
  14. 142: log.Println("发送整个产品数据库")
  15. 143: wri.Header().Add("Content-Type", "application/json")
  16. => 144: wri.WriteHeader(http.StatusOK)
  17. 145: json.NewEncoder(wri).Encode(handler.DB)
  18. 146: return
  19. 147: }
  20. 148:
  21. 149: c := req.URL.Query()["code"][0]
  22. 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.

  1. (dlv) args
  2. handler = (*main.ProduceHandler)(0x14000112d10)
  3. wri = net/http.ResponseWriter(*net/http.response) 0x14000193708
  4. req = ("*net/http.Request")(0x14000182000)
  5. (dlv) display -a wri.w.wr.res.handlerHeader["Content-Type"][0]
  6. 0: wri.w.wr.res.handlerHeader["Content-Type"][0] = error key not found
  7. (dlv) print %T wri.w.wr.res.handlerHeader
  8. net/http.Header []
  9. (dlv) n
  10. > main.(*ProduceHandler).ServeHTTP() ./api.go:144 (PC: 0x100984480)
  11. 139: switch req.Method {
  12. 140: case http.MethodGet:
  13. 141: if len(req.URL.Query()["code"]) == 0 {
  14. 142: log.Println("Sending entire produce database")
  15. 143: wri.Header().Add("Content-Type", "application/json")
  16. => 144: wri.WriteHeader(http.StatusOK)
  17. 145: json.NewEncoder(wri).Encode(handler.DB)
  18. 146: return
  19. 147: }
  20. 148:
  21. 149: c := req.URL.Query()["code"][0]
  22. 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:

确定