Skip to content
Content ID v1 implemented in go
Branch: master
Clone or download
Stebalien Merge pull request #86 from lidel/feat/libp2p-key
Add support for libp2p-key multicodec to go-cid
Latest commit 9bb7ea6 May 28, 2019


GoDoc Coverage Status Travis CI

A package to handle content IDs in Go.

This is an implementation in Go of the CID spec. It is used in go-ipfs and related packages to refer to a typed hunk of data.

Table of Contents


go-cid is a standard Go module which can be installed with:

go get

Note that go-cid is packaged with Gx, so it is recommended to use Gx to install and use it (see Usage section).


Using Gx and Gx-go

This module is packaged with Gx. In order to use it in your own project it is recommended that you:

go get -u
go get -u
cd <your-project-repository>
gx init
gx import
gx install --global
gx-go --rewrite

Please check Gx and Gx-go documentation for more information.

Running tests

Before running tests, please run:

make deps

This will make sure that dependencies are rewritten to known working versions.


Parsing string input from users

// Create a cid from a marshaled string
c, err := cid.Decode("bafzbeigai3eoy2ccc7ybwjfz5r3rdxqrinwi4rwytly24tdbh6yk7zslrm")
if err != nil {...}

fmt.Println("Got CID: ", c)

Creating a CID from scratch

// Create a cid manually by specifying the 'prefix' parameters
pref := cid.Prefix{
	Version: 1,
	Codec: cid.Raw,
	MhType: mh.SHA2_256,
	MhLength: -1, // default length

// And then feed it some data
c, err := pref.Sum([]byte("Hello World!"))
if err != nil {...}

fmt.Println("Created CID: ", c)

Check if two CIDs match

// To test if two cid's are equivalent, be sure to use the 'Equals' method:
if c1.Equals(c2) {
	fmt.Println("These two refer to the same exact data!")

Check if some data matches a given CID

// To check if some data matches a given cid, 
// Get your CIDs prefix, and use that to sum the data in question:
other, err := c.Prefix().Sum(mydata)
if err != nil {...}

if !c.Equals(other) {
	fmt.Println("This data is different.")


PRs are welcome!

Small note: If editing the Readme, please conform to the standard-readme specification.


MIT © Jeromy Johnson

You can’t perform that action at this time.