c724ad0d221df41d1ea04c94d00b05bd6d128695
Added back in some of the parser methods. (These were previously named "Cast" and I think that's silly and wrong so I fixed it.) Functions are named overly-literally with their type (e.g. ParseCidString and ParseCidStruct rather than ParseCid or even just Parse) because for this research package I don't want to bother with many sub-packages. (Maybe I'll regret this, but at the moment it seems simpler to hold back on sub-packages.) Functions that produce Cids are literal with their return types, as well. Part of the purpose of this research package is going to be to concretely benchmark exactly how much performance overhead there is to using interfaces (which will likely cause a lot of boxing and unboxing in practice) -- since we want to explore where this boxing happens and how much it costs, it's important that none of our basic implementation functions do the boxing! The entire set of codec enums came along in this commit. Ah well; they would have eventually anyway, I guess. But it's interesting to note the only thing that dragged them along so far is the reference to 'DagProtobuf' when constructing v0 CIDs; otherwise, this enum is quite unused here.
go-cid
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
Install
go-cid is a standard Go module which can be installed with:
go get github.com/ipfs/go-cid
Note that go-cid is packaged with Gx, so it is recommended to use Gx to install and use it (see Usage section).
Usage
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 github.com/whyrusleeping/gx
go get -u github.com/whyrusleeping/gx-go
cd <your-project-repository>
gx init
gx import github.com/ipfs/go-cid
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.
Examples
Parsing string input from users
// Create a cid from a marshaled string
c, err := cid.Decode("zdvgqEMYmNeH5fKciougvQcfzMcNjF3Z1tPouJ8C7pc3pe63k")
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.")
}
Contribute
PRs are welcome!
Small note: If editing the Readme, please conform to the standard-readme specification.
License
MIT © Jeromy Johnson
Description
Languages
Go
99.9%
Makefile
0.1%