这是一段代码:
package main
import (
"fmt"
)
type TestType struct {
a int
b int
}
func main() {
var testType TestType = TestType{1, 2}
fmt.Println(testType)
}
这是gdb调试输出:
(gdb) r
Starting program: /home/bzhang/common/src/go/src/test/testBinary
Breakpoint 1, main.main () at /home/bzhang/common/src/go/src/test/main.go:14
14 fmt.Println(testType)
(gdb) p testType
$1 = {a = 1, b = 2}
(gdb) p &testType
$2 = (main.TestType *) 0xc820059ee8
(gdb) p ('main.TestType'*) 0xc820059ee8
A syntax error in expression, near `) 0xc820059ee8'.
(gdb) p ('TestType'*) 0xc820059ee8
A syntax error in expression, near `) 0xc820059ee8'.
(gdb) whatis testType
type = main.TestType
(gdb)
当然,我知道我可以直接打印testType。但是,如果它是局部变量,则有时无法直接将其值打印出来,而只能使用其地址。因此,我想用其类型的指示来打印其值。但它似乎无法正常工作。
感谢您的帮助!
最佳答案
delve工具比gdb更好。 https://github.com/go-delve/delve
go get -u github.com/go-delve/delve/cmd/dlv
cd $GOPATH/src/YOUPROJECT/main.go
$GOPATH/bin/delve debug main.go
Type 'help' for list of commands.
(dlv) b main.main
Breakpoint 1 set at 0x4a7bbf for main.main() main.go:12
(dlv) c
> main.main() main.go:12 (hits goroutine(1):1 total:1) (PC: 0x4a7bbf)
7: type TestType struct {
8: a int
9: b int
10: }
11:
=> 12: func main() {
13: var testType TestType = TestType{1, 2}
14: fmt.Println(testType)
15: }
(dlv) b 14
Breakpoint 2 set at 0x4a7bf0 for main.main() main.go:14
(dlv) c
> main.main() main.go:14 (hits goroutine(1):1 total:1) (PC: 0x4a7bf0)
9: b int
10: }
11:
12: func main() {
13: var testType TestType = TestType{1, 2}
=> 14: fmt.Println(testType)
15: }
(dlv) locals
testType = main.TestType {a: 1, b: 2}
最后,使用goland,vscode更加方便调试。