Repository: libp2p/go-libp2p
Pull request #2563 (2023-09-09 15:28:33)

examples: stop using deprecated peer.ID.Pretty
The peerID Pretty method is already deprecated, switch it to call String method.

Signed-off-by: Icarus9913 [icaruswu66@qq.com](mailto:icaruswu66@qq.com)


Patch:
diff --git a/examples/chat-with-mdns/main.go b/examples/chat-with-mdns/main.go
index 57b89c25..b45e4fd4 100644
--- a/examples/chat-with-mdns/main.go
+++ b/examples/chat-with-mdns/main.go
@@ -111,7 +111,7 @@ func main() {
 	// This function is called when a peer initiates a connection and starts a stream with this peer.
 	host.SetStreamHandler(protocol.ID(cfg.ProtocolID), handleStream)
 
-	fmt.Printf("\n[*] Your Multiaddress Is: /ip4/%s/tcp/%v/p2p/%s\n", cfg.listenHost, cfg.listenPort, host.ID().Pretty())
+	fmt.Printf("\n[*] Your Multiaddress Is: /ip4/%s/tcp/%v/p2p/%s\n", cfg.listenHost, cfg.listenPort, host.ID())
 
 	peerChan := initMDNS(host, cfg.RendezvousString)
 	for { // allows multiple peers to join
diff --git a/examples/chat/chat.go b/examples/chat/chat.go
index cd1c2872..d89ea3cd 100644
--- a/examples/chat/chat.go
+++ b/examples/chat/chat.go
@@ -187,7 +187,7 @@ func startPeer(ctx context.Context, h host.Host, streamHandler network.StreamHan
 		return
 	}
 
-	log.Printf("Run './chat -d /ip4/127.0.0.1/tcp/%v/p2p/%s' on another console.\n", port, h.ID().Pretty())
+	log.Printf("Run './chat -d /ip4/127.0.0.1/tcp/%v/p2p/%s' on another console.\n", port, h.ID())
 	log.Println("You can replace 127.0.0.1 with public IP as well.")
 	log.Println("Waiting for incoming connection")
 	log.Println()
diff --git a/examples/echo/main.go b/examples/echo/main.go
index 3bba8961..f34cb585 100644
--- a/examples/echo/main.go
+++ b/examples/echo/main.go
@@ -88,7 +88,7 @@ func makeBasicHost(listenPort int, insecure bool, randseed int64) (host.Host, er
 
 func getHostAddress(ha host.Host) string {
 	// Build host multiaddress
-	hostAddr, _ := ma.NewMultiaddr(fmt.Sprintf("/p2p/%s", ha.ID().Pretty()))
+	hostAddr, _ := ma.NewMultiaddr(fmt.Sprintf("/p2p/%s", ha.ID()))
 
 	// Now we can build a full multiaddress to reach this host
 	// by encapsulating both addresses:
diff --git a/examples/http-proxy/proxy.go b/examples/http-proxy/proxy.go
index 27d95978..f96d4b8b 100644
--- a/examples/http-proxy/proxy.go
+++ b/examples/http-proxy/proxy.go
@@ -144,7 +144,7 @@ func (p *ProxyService) Serve() {
 // Streams are multiplexed over single connections so, unlike connections
 // themselves, they are cheap to create and dispose of.
 func (p *ProxyService) ServeHTTP(w http.ResponseWriter, r *http.Request) {
-	fmt.Printf("proxying request for %s to peer %s\n", r.URL, p.dest.Pretty())
+	fmt.Printf("proxying request for %s to peer %s\n", r.URL, p.dest)
 	// We need to send the request to the remote libp2p peer, so
 	// we open a stream to it
 	stream, err := p.host.NewStream(context.Background(), p.dest, Protocol)
diff --git a/examples/pubsub/basic-chat-with-rendezvous/main.go b/examples/pubsub/basic-chat-with-rendezvous/main.go
index 57729ed1..46d8ca09 100644
--- a/examples/pubsub/basic-chat-with-rendezvous/main.go
+++ b/examples/pubsub/basic-chat-with-rendezvous/main.go
@@ -95,9 +95,9 @@ func discoverPeers(ctx context.Context, h host.Host) {
 			}
 			err := h.Connect(ctx, peer)
 			if err != nil {
-				fmt.Println("Failed connecting to ", peer.ID.Pretty(), ", error:", err)
+				fmt.Printf("Failed connecting to %s, error: %s\n", peer.ID, err)
 			} else {
-				fmt.Println("Connected to:", peer.ID.Pretty())
+				fmt.Println("Connected to:", peer.ID)
 				anyConnected = true
 			}
 		}
diff --git a/examples/pubsub/chat/README.md b/examples/pubsub/chat/README.md
index 2b99c121..a37af705 100644
--- a/examples/pubsub/chat/README.md
+++ b/examples/pubsub/chat/README.md
@@ -132,7 +132,7 @@ to the `pubsub.Topic`:
 func (cr *ChatRoom) Publish(message string) error {
 	m := ChatMessage{
 		Message:    message,
-		SenderID:   cr.self.Pretty(),
+		SenderID:   cr.self.String(),
 		SenderNick: cr.nick,
 	}
 	msgBytes, err := json.Marshal(m)
diff --git a/examples/pubsub/chat/chatroom.go b/examples/pubsub/chat/chatroom.go
index 452cc309..dc323d50 100644
--- a/examples/pubsub/chat/chatroom.go
+++ b/examples/pubsub/chat/chatroom.go
@@ -71,7 +71,7 @@ func JoinChatRoom(ctx context.Context, ps *pubsub.PubSub, selfID peer.ID, nickna
 func (cr *ChatRoom) Publish(message string) error {
 	m := ChatMessage{
 		Message:    message,
-		SenderID:   cr.self.Pretty(),
+		SenderID:   cr.self.String(),
 		SenderNick: cr.nick,
 	}
 	msgBytes, err := json.Marshal(m)
diff --git a/examples/pubsub/chat/main.go b/examples/pubsub/chat/main.go
index 45a103d0..593e41e2 100644
--- a/examples/pubsub/chat/main.go
+++ b/examples/pubsub/chat/main.go
@@ -80,7 +80,7 @@ func defaultNick(p peer.ID) string {
 
 // shortID returns the last 8 chars of a base58-encoded peer id.
 func shortID(p peer.ID) string {
-	pretty := p.Pretty()
+	pretty := p.String()
 	return pretty[len(pretty)-8:]
 }
 
@@ -93,10 +93,10 @@ type discoveryNotifee struct {
 // the PubSub system will automatically start interacting with them if they also
 // support PubSub.
 func (n *discoveryNotifee) HandlePeerFound(pi peer.AddrInfo) {
-	fmt.Printf("discovered new peer %s\n", pi.ID.Pretty())
+	fmt.Printf("discovered new peer %s\n", pi.ID)
 	err := n.h.Connect(context.Background(), pi)
 	if err != nil {
-		fmt.Printf("error connecting to peer %s: %s\n", pi.ID.Pretty(), err)
+		fmt.Printf("error connecting to peer %s: %s\n", pi.ID, err)
 	}
 }
 
diff --git a/examples/routed-echo/main.go b/examples/routed-echo/main.go
index 2f40549b..342661b6 100644
--- a/examples/routed-echo/main.go
+++ b/examples/routed-echo/main.go
@@ -83,7 +83,7 @@ func makeRoutedHost(listenPort int, randseed int64, bootstrapPeers []peer.AddrIn
 	}
 
 	// Build host multiaddress
-	hostAddr, _ := ma.NewMultiaddr(fmt.Sprintf("/ipfs/%s", routedHost.ID().Pretty()))
+	hostAddr, _ := ma.NewMultiaddr(fmt.Sprintf("/ipfs/%s", routedHost.ID()))
 
 	// Now we can build a full multiaddress to reach this host
 	// by encapsulating both addresses:
@@ -94,7 +94,7 @@ func makeRoutedHost(listenPort int, randseed int64, bootstrapPeers []peer.AddrIn
 		log.Println(addr.Encapsulate(hostAddr))
 	}
 
-	log.Printf("Now run \"./routed-echo -l %d -d %s%s\" on a different terminal\n", listenPort+1, routedHost.ID().Pretty(), globalFlag)
+	log.Printf("Now run \"./routed-echo -l %d -d %s%s\" on a different terminal\n", listenPort+1, routedHost.ID(), globalFlag)
 
 	return routedHost, nil
 }


Test patch:
diff --git a/examples/chat/chat_test.go b/examples/chat/chat_test.go
index 1a1cea3c..4240b477 100644
--- a/examples/chat/chat_test.go
+++ b/examples/chat/chat_test.go
@@ -49,7 +49,7 @@ func TestMain(t *testing.T) {
 			cancel() // end the test
 		})
 
-		dest := fmt.Sprintf("/ip4/127.0.0.1/tcp/%v/p2p/%s", port1, h1.ID().Pretty())
+		dest := fmt.Sprintf("/ip4/127.0.0.1/tcp/%v/p2p/%s", port1, h1.ID())
 
 		h2, err := makeHost(port2, rand.Reader)
 		if err != nil {