$ go build go: finding module for package golang.org/x/text/encoding/ianaindex go: finding module for package golang.org/x/text/message go: finding module for package golang.org/x/text/encoding go: finding module for package golang.org/x/text/language go: finding module for package github.com/golang/snappy go: found github.com/golang/snappy in github.com/golang/snappy v0.0.4 go: found golang.org/x/text/encoding in golang.org/x/text v0.3.6
$ gdb ./dmtest 0x0000000000ace1ae in dm.(*Properties).GetTrimString (g=0x0, key=..., def=..., ~r2=...) at /opt/go1.14.3/src/dm/zv.go:80 80 value, ok := g.innerProps[strings.ToLower(key)] (gdb) p g $2 = (dm.Properties *) 0x0 (gdb) bt #0 0x0000000000ace1ae in dm.(*Properties).GetTrimString (g=0x0, key=..., def=..., ~r2=...) at /opt/go1.14.3/src/dm/zv.go:80 #1 0x0000000000a4f76f in dm.(*DmConnector).mergeConfigs (c=0xc00007a000, dsn=..., ~r1=...) at /opt/go1.14.3/src/dm/n.go:766 #2 0x0000000000a54f00 in dm.(*DmDriver).openConnector (d=0xc000110e80, dsn=..., ~r1=0x0, ~r2=...) at /opt/go1.14.3/src/dm/p.go:78 #3 0x0000000000a54b8d in dm.(*DmDriver).OpenConnector (d=0xc000110e80, dsn=..., ~r1=..., ~r2=...) at /opt/go1.14.3/src/dm/p.go:62 #4 0x00000000006c4b8b in database/sql.Open (driverName=..., dataSourceName=..., ~r2=0x0, ~r3=...) at /opt/go1.14.3/src/database/sql/sql.go:754 #5 0x0000000000b03183 in main.connect (driverName=..., dataSourceName=..., ~r2=0x0, ~r3=...) at /home/japin/Codes/gocode/src/dmtest/main.go:40 #6 0x0000000000b02cde in main.main () at /home/japin/Codes/gocode/src/dmtest/main.go:19
可以看到这是由于 GetTrimString() 函数中调用 g.innerProps[strings.ToLower(key)] 导致的,因为变量 g 为空。那么 g 是什么呢?通过查看上层调用栈 #1,可以得到这里的 g 其实就是 GlobalProperties。
1 2 3 4
(gdb) up #1 0x0000000000a4f76f in dm.(*DmConnector).mergeConfigs (c=0xc00007a000, dsn=..., ~r1=...) at /opt/go1.14.3/src/dm/n.go:766 766 addressRemapStr = GlobalProperties.GetTrimString(AddressRemapKey, "") (gdb)