how to call github.com/ethereum/go-ethereum/p2p in outer proagram?

0

I write a golang program and call the ethereum package in order to use the ethereum function. The codes which called ethereum function is below (of course there is other codes in my program): package main

import (
    "fmt"
    "github.com/ethereum/go-ethereum/p2p"
)
func (cli *CLI) getPeerInfo() {
    var server = new(p2p.Server)
    server.Start()
    a:=server.NodeInfo()
    //return a
    fmt.Printf(a.Enode)
}

you can see that I just want to get the node infor using ethereum function, but after compilation and run ,it shows error below:

panic: runtime error: invalid memory address or nil pointer dereference
[signal 0xc0000005 code=0x0 addr=0x0 pc=0x4f429e]

goroutine 1 [running]:
github.com/ethereum/go-ethereum/p2p.(*Server).Self(0xc00019ca00, 0x0)
        C:/tools/src/github.com/ethereum/go-ethereum/p2p/server.go:386 +0xfe
github.com/ethereum/go-ethereum/p2p.(*Server).NodeInfo(0xc00019ca00, 0x0)
        C:/tools/src/github.com/ethereum/go-ethereum/p2p/server.go:1077 +0x5e
main.(*CLI).getPeerInfo(0xc00007df78)
        C:/tools/src/github.com/liangjian/blockchain_go/cli_getpeerinfo.go:10 +0x59
main.(*CLI).Run(0xc00007df78)
        C:/tools/src/github.com/liangjian/blockchain_go/cli.go:165 +0xbdd
main.main()
        C:/tools/src/github.com/liangjian/blockchain_go/main.go:5 +0x32

Debugger finished with exit code 0

How to solve it ? thank you very much.

p2p
go-ethereum
asked on Stack Overflow Apr 12, 2021 by jiebang

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0