### Install go-multiaddr Source: https://github.com/multiformats/go-multiaddr/blob/master/README.md Use `go get` to install the go-multiaddr library. ```sh go get github.com/multiformats/go-multiaddr ``` -------------------------------- ### Get Multiaddr Protocols Source: https://github.com/multiformats/go-multiaddr/blob/master/README.md Retrieve descriptions of the protocols used within a multiaddr. Each protocol includes its code, name, and size. ```go // get the multiaddr protocol description objects m1.Protocols() // []Protocol{ // Protocol{ Code: 4, Name: 'ip4', Size: 32}, // Protocol{ Code: 17, Name: 'udp', Size: 16}, // } ``` -------------------------------- ### Create and Compare Multiaddr Source: https://github.com/multiformats/go-multiaddr/blob/master/README.md Construct multiaddrs from strings or bytes and compare them for equality. Ensure proper error handling for parsing failures. ```go import ma "github.com/multiformats/go-multiaddr" // construct from a string (err signals parse failure) m1, err := ma.NewMultiaddr("/ip4/127.0.0.1/udp/1234") // construct from bytes (err signals parse failure) m2, err := ma.NewMultiaddrBytes(m1.Bytes()) // true strings.Equal(m1.String(), "/ip4/127.0.0.1/udp/1234") strings.Equal(m1.String(), m2.String()) bytes.Equal(m1.Bytes(), m2.Bytes()) m1.Equal(m2) m2.Equal(m1) ``` -------------------------------- ### Express Tunneling with Multiaddr Source: https://github.com/multiformats/go-multiaddr/blob/master/README.md Demonstrates how multiaddr can represent tunneled addresses by encapsulating one address within another, useful for proxying. ```js printer, _ := ma.NewMultiaddr("/ip4/192.168.0.13/tcp/80") proxy, _ := ma.NewMultiaddr("/ip4/10.20.30.40/tcp/443") printerOverProxy := proxy.Encapsulate(printer) // /ip4/10.20.30.40/tcp/443/ip4/192.168.0.13/tcp/80 proxyAgain := printerOverProxy.Decapsulate(printer) // /ip4/10.20.30.40/tcp/443 ``` -------------------------------- ### Encapsulate and Decapsulate Multiaddrs Source: https://github.com/multiformats/go-multiaddr/blob/master/README.md Combine multiaddrs by encapsulating a sub-address within a parent address, or separate them by decapsulating a sub-address. ```go import ma "github.com/multiformats/go-multiaddr" m, err := ma.NewMultiaddr("/ip4/127.0.0.1/udp/1234") // sctpMA, err := ma.NewMultiaddr("/sctp/5678") m.Encapsulate(sctpMA) // udpMA, err := ma.NewMultiaddr("/udp/1234") m.Decapsulate(udpMA) // up to + inc last occurrence of subaddr // ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.