每当网络状态发生变化时,我都需要观察网络变化并设置一些路由规则(例如,如果接口重新启动,我需要再次设置 ip 路由表 b/c src 是动态的,因此我无法对其进行硬编码)。为了实现这一点,我正在尝试使用networkmanager包,但 WatchState 会出现恐慌。知道为什么吗?package mainimport( "flag" "os" "launchpad.net/ubuntu-push/bus" "launchpad.net/ubuntu-push/bus/networkmanager" "launchpad.net/ubuntu-push/logger")func main(){ flag.Parse() lg := logger.NewSimpleLogger(os.Stderr, "info") endp := bus.SystemBus.Endpoint(networkmanager.BusAddress, lg) nm := networkmanager.New(endp, lg) watchState, cancel, err := nm.WatchState() if err !=nil{ log.Printf("error %v", err) return } defer cancel.Cancel() for state := range watchState{ log.Printf.("state is %v", state) }}恐慌痕迹panic: runtime error: invalid memory address or nil pointer dereference[signal 0xb code=0x1 addr=0x0 pc=0x488788] goroutine 1 [running]: launchpad.net/go-dbus/v1.(*ObjectProxy).WatchSignal(0x0, 0x616cd0, 0x1e, 0x5f0af0, 0xc, 0x7f9733d0e478, 0x0, 0x0) /root/go/src/launchpad.net/go-dbus/v1/dbus.go:97 +0x198 launchpad.net/ubuntu-push/bus.(*endpoint).WatchSignal(0xc20803e0c0, 0x5f0af0, 0xc, 0xc20800a0e0, 0xc20802a530, 0x0, 0x0, 0x0, 0x0) /root/go/src/launchpad.net/ubuntu-push/bus/endpoint.go:133 +0x91 launchpad.net/ubuntu-push/bus/networkmanager.(*networkManager).WatchState(0xc20800a0c0, 0x5bce80, 0x0, 0x0, 0x0, 0x0) /root/go/src/launchpad.net/ubuntu-push/bus/networkmanager/networkmanager.go:108 +0x1cf main.main() /root/go/src/github.com/vinkoo/net/main.go:16 +0x1f9 goroutine 2 [runnable]: runtime.forcegchelper() /usr/local/go/src/runtime/proc.go:90 runtime.goexit() /usr/local/go/src/runtime/asm_amd64.s:2232 +0x1 goroutine 3 [runnable]: runtime.bgsweep() /usr/local/go/src/runtime/mgc0.go:82 runtime.goexit() /usr/local/go/src/runtime/asm_amd64.s:2232 +0x1
1 回答
- 1 回答
- 0 关注
- 161 浏览
添加回答
举报
0/150
提交
取消