在运行go libp2p时遇到了未定义的ctx。

huangapple go评论86阅读模式
英文:

Undefined: ctx was encountered while running go libp2p

问题

以下是代码的中文翻译:

// 如果在命令行中传递了远程对等点,连接到它并发送5个ping消息,否则等待停止信号
if len(os.Args) > 1 {
    addr, err := multiaddr.NewMultiaddr(os.Args[1])
    if err != nil {
        panic(err)
    }
    peer, err := peerstore.AddrInfoFromP2pAddr(addr)
    if err != nil {
        panic(err)
    }
    if err := node.Connect(ctx, *peer); err != nil {
        panic(err)
    }
    fmt.Println("发送5个ping消息到", addr)
}

导入的包如下

import (
    "fmt"
    "os"
    "os/signal"
    "syscall"

    "github.com/libp2p/go-libp2p"
    peerstore "github.com/libp2p/go-libp2p-core/peer"
    "github.com/libp2p/go-libp2p/p2p/protocol/ping"
    multiaddr "github.com/multiformats/go-multiaddr"
)

希望对你有帮助!如果还有其他问题,请随时提问。

英文:

The code is following,it is the an offical demo of go-libp2p.And I didn't encounter any other references or undefined errors

	// if a remote peer has been passed on the command line, connect to it
	// and send it 5 ping messages, otherwise wait for a signal to stop
	if len(os.Args) > 1 {
		addr, err := multiaddr.NewMultiaddr(os.Args[1])
		if err != nil {
			panic(err)
		}
		peer, err := peerstore.AddrInfoFromP2pAddr(addr)
		if err != nil {
			panic(err)
		}
		if err := node.Connect(ctx, *peer); err != nil {
			panic(err)
		}
		fmt.Println("sending 5 ping messages to", addr)

The import is following:

import (
	"fmt"
	"os"
	"os/signal"
	"syscall"

	"github.com/libp2p/go-libp2p"
	peerstore "github.com/libp2p/go-libp2p-core/peer"
	"github.com/libp2p/go-libp2p/p2p/protocol/ping"
	multiaddr "github.com/multiformats/go-multiaddr"
)

答案1

得分: 1

看起来你正在按照"入门教程"进行操作。在你的问题代码块之前,你需要导入context并创建一个上下文:

// 创建上下文
ctx := context.Background()

// 如果在命令行中传递了远程对等点,请连接到它并发送5个ping消息,否则等待停止信号
if len(os.Args) > 1 {
    addr, err := multiaddr.NewMultiaddr(os.Args[1])
    if err != nil {
        panic(err)
    }
    peer, err := peerstore.AddrInfoFromP2pAddr(addr)
    if err != nil {
        panic(err)
    }
    if err := node.Connect(ctx, *peer); err != nil {
        panic(err)
    }
    fmt.Println("发送5个ping消息到", addr)
}
英文:

Looks like you are following the "Getting Started" tutorial.
You'll need to import context and prior to the code block in your question, you'll need to create a context:

// create context
ctx:=context.Background()

// if a remote peer has been passed on the command line, connect to it
// and send it 5 ping messages, otherwise wait for a signal to stop
if len(os.Args) > 1 {
    addr, err := multiaddr.NewMultiaddr(os.Args[1])
    if err != nil {
        panic(err)
    }
    peer, err := peerstore.AddrInfoFromP2pAddr(addr)
    if err != nil {
        panic(err)
    }
    if err := node.Connect(ctx, *peer); err != nil {
        panic(err)
    }
    fmt.Println("sending 5 ping messages to", addr)

huangapple
  • 本文由 发表于 2022年1月2日 19:33:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/70555599.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定