2014-07-03 23:42:24 -07:00
# go-multiaddr
2016-08-15 11:15:17 -04:00
[](http://ipn.io)
[](http://github.com/multiformats/multiformats)
[](http://webchat.freenode.net/?channels=%23ipfs)
2014-07-03 23:42:24 -07:00
2016-08-15 11:15:17 -04:00
> multiaddr implementation in go
2014-07-03 23:42:24 -07:00
2016-08-15 11:15:17 -04:00
This is a [multiaddr ](https://github.com/multiformats/multiaddr ) implementation in Go.
## Table of Contents
- [Install ](#install )
- [Usage ](#usage )
- [Example ](#example )
- [Simple ](#simple )
- [Protocols ](#protocols )
- [En/decapsulate ](#endecapsulate )
- [Tunneling ](#tunneling )
- [Maintainers ](#maintainers )
- [Contribute ](#contribute )
- [License ](#license )
## Install
```sh
go get multiformats/multiaddr
```
## Usage
### Example
#### Simple
2014-07-03 23:42:24 -07:00
```go
2016-09-29 14:52:32 +02:00
import ma "github.com/multiformats/go-multiaddr"
2014-07-03 23:42:24 -07:00
2014-10-06 04:05:20 -07:00
// construct from a string (err signals parse failure)
m1, err := ma.NewMultiaddr("/ip4/127.0.0.1/udp/1234")
2014-07-03 23:42:24 -07:00
2014-10-06 04:05:20 -07:00
// 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)
2014-07-03 23:42:24 -07:00
```
2016-08-15 11:15:17 -04:00
#### Protocols
2014-07-03 23:42:24 -07:00
```go
// get the multiaddr protocol description objects
2014-07-04 00:12:05 -07:00
addr.Protocols()
2015-01-09 05:37:02 -08:00
// []Protocol{
// Protocol{ Code: 4, Name: 'ip4', Size: 32},
// Protocol{ Code: 17, Name: 'udp', Size: 16},
2014-07-03 23:42:24 -07:00
// }
```
2016-08-15 11:15:17 -04:00
#### En/decapsulate
2014-07-03 23:42:24 -07:00
```go
2014-10-06 04:05:20 -07:00
m.Encapsulate(ma.NewMultiaddr("/sctp/5678"))
2014-07-03 23:42:24 -07:00
// < Multiaddr / ip4 / 127 . 0 . 0 . 1 / udp / 1234 / sctp / 5678 >
2014-10-06 04:05:20 -07:00
m.Decapsulate(ma.NewMultiaddr("/udp")) // up to + inc last occurrence of subaddr
2014-07-03 23:42:24 -07:00
// < Multiaddr / ip4 / 127 . 0 . 0 . 1 >
```
2016-08-15 11:15:17 -04:00
#### Tunneling
2014-07-03 23:42:24 -07:00
Multiaddr allows expressing tunnels very nicely.
```js
2014-10-06 04:05:20 -07:00
printer, _ := ma.NewMultiaddr("/ip4/192.168.0.13/tcp/80")
proxy, _ := ma.NewMultiaddr("/ip4/10.20.30.40/tcp/443")
2014-07-03 23:42:24 -07:00
printerOverProxy := proxy.Encapsulate(printer)
2014-10-06 04:05:20 -07:00
// /ip4/10.20.30.40/tcp/443/ip4/192.168.0.13/tcp/80
2014-07-03 23:42:24 -07:00
2014-10-06 04:05:20 -07:00
proxyAgain := printerOverProxy.Decapsulate(printer)
// /ip4/10.20.30.40/tcp/443
2014-07-03 23:42:24 -07:00
```
2016-08-15 11:15:17 -04:00
## Maintainers
Captain: [@whyrusleeping ](https://github.com/whyrusleeping ).
## Contribute
Contributions welcome. Please check out [the issues ](https://github.com/multiformats/go-multiaddr/issues ).
Check out our [contributing document ](https://github.com/multiformats/multiformats/blob/master/contributing.md ) for more information on how we work, and about contributing in general. Please be aware that all interactions related to multiformats are subject to the IPFS [Code of Conduct ](https://github.com/ipfs/community/blob/master/code-of-conduct.md ).
Small note: If editing the Readme, please conform to the [standard-readme ](https://github.com/RichardLitt/standard-readme ) specification.
## License
[MIT ](LICENSE ) © Juan Batiz-Benet