一.本地调试
1.dlv debug source file
dlv debug meng.go
2.dlv exec binary file
go build -gcflags="all=-N -l" -o meng.out meng.go //windows: -o meng.exe
dlv exec meng.out
3.dlv attach pid
go build -gcflags="all=-N -l" -o meng.out meng.go
./meng.out
查找pid(1234): ps aux |grep "meng.out"
dlv attach 1234 //pid: 1234
二.远程调试
1).环境
server: 192.168.68.2
client: 192.168.68.10
2).server:
1.dlv debug source file
dlv --listen=:65381 --headless=true --api-version=2 debug meng.go
2.dlv exec binary file
go build -gcflags="all=-N -l" -o meng.out meng.go //windows: -o meng.exe
dlv --listen=:65381 --headless=true --api-version=2 exec meng.out
3.dlv attach pid
go build -gcflags="all=-N -l" -o meng.out meng.go
./meng.out
ps aux |grep "meng.out" //查找pid(1234):
dlv --listen=:65381 --headless=true --api-version=2 attach 1234 //pid: 1234
//本方法非常适合远程调试需求。
3).client:
dlv connect 192.168.68.2:65381
进入调试环境:
b meng.go:48
c
....
三.附meng.go
1 package main
2
3 import (
4 "fmt"
5 "time"
6 "reflect"
7 )
8
9 type Ball struct {
10 Id int32 `json:"id"`
11 Name string `json:"name"`
12 }
13
14 type FootBall struct {
15 *Ball
16 price int32
17 }
18
19 type PinponBall struct {
20 *Ball
21 price int32
22 }
23
24 func (ball *Ball) SetId(id int32) {
25 ball.Id = id
26 fmt.Printf("ball: id=%d.\n", ball.Id)
27 }
28
29 func (ball Ball) GetId() int32 {
30 return ball.Id
31 }
32
33 func main() {
34 fmt.Printf("Hello world!\n")
35
36 buf := &Ball{Id: 100, Name:"football"}
37 v := reflect.ValueOf(buf)
38 kind := v.Kind()
39 if kind == reflect.Ptr || kind == reflect.Interface {
40 v = v.Elem()
41 }
42 t := v.Type()
43 id := t.Field(0).Tag.Get("json")
44 name := t.Field(1).Tag.Get("json")
45 fmt.Printf("id=%s, name=%s\n", id, name)
46 i := 0
47 for i = 0; i < 1000; i+=1 {
48 fmt.Printf("i=%d\n", i)
49 time.Sleep(10*time.Second)
50 }
51 }