Skip to content

Commit

Permalink
docs(readme): improved usage examples (#66)
Browse files Browse the repository at this point in the history
  • Loading branch information
lidel committed Apr 21, 2022
1 parent 9a49a9b commit 34aa63e
Showing 1 changed file with 27 additions and 1 deletion.
28 changes: 27 additions & 1 deletion README.md
Expand Up @@ -19,20 +19,46 @@

go get github.com/multiformats/go-multicodec


## Type

`Code` describes an integer reserved in the multicodec table, defined at [multiformats/multicodec/table.csv](https://github.com/multiformats/multicodec/blob/master/table.csv).

```go
type Code uint64
```

## Usage

### Importing Code constant

```go
package main

import "github.com/multiformats/go-multicodec"

func main() {
_ = multicodec.Sha2_256
code := multicodec.Sha2_256 // Code
name := multicodec.Sha2_256.String()
}
```

The corresponding `name` value for each codec from the [multicodecs table](https://raw.githubusercontent.com/multiformats/multicodec/master/table.csv) can be accessed via its `String` method. For example, `multicodec.Sha2_256.String()` will return `sha2-256`.

### Code from string

```go
var multicodec.Code code
err := code.Set("libp2p-key")
```


### Code from uint64

```go
rawCode := multicodec.Code(0x55)
```

## Generator

To generate the constants yourself:
Expand Down

0 comments on commit 34aa63e

Please sign in to comment.