import "github.com.ipfs.go-ipfs.godeps._workspace.src.github.com.jbenet.go-multiaddr" // create a new multiaddr for a TCP address addr, err := multiaddr.NewMultiaddr("/ip4/127.0.0.1/tcp/4001") if err != nil { panic(err) } // print the address in human-readable format fmt.Println(addr.String())
import "github.com.ipfs.go-ipfs.godeps._workspace.src.github.com.jbenet.go-multiaddr" // create a new multiaddr for a UDP address addr := multiaddr.NewMultiaddr("/ip4/127.0.0.1/udp/1234") // encode the address as a byte array bytes := addr.Bytes() // decode the byte array back into a multiaddr object decodedAddr, err := multiaddr.NewMultiaddrBytes(bytes) if err != nil { panic(err) } // print the original and decoded addresses fmt.Println(addr.String()) fmt.Println(decodedAddr.String())In this example, we’re using the `Bytes` method to encode the multiaddr object as a byte array. We’re then using the `NewMultiaddrBytes` method to decode the byte array back into a multiaddr object. We’re using the `String` method to print both the original and decoded addresses. Overall, the Multiaddr package is a useful library for working with network addresses in a protocol-agnostic way. It’s available as an open-source package on GitHub and can be installed using the `go get` command.