373e25552c927b3bea32511aac6ce150aac6b984
We allocate once via "make([]byte, len)",
and again when that buffer is converted to a string.
Thankfully, since Go 1.10 we have strings.Builder,
designed specifically for this use case.
In a downstream benchmark in go-car,
which needs to reconstruct many CID values,
we see small but nice gains:
name old time/op new time/op delta
ReadBlocks-16 1.09ms ± 4% 1.06ms ± 5% -3.33% (p=0.007 n=11+11)
name old speed new speed delta
ReadBlocks-16 478MB/s ± 4% 494MB/s ± 5% +3.46% (p=0.007 n=11+11)
name old alloc/op new alloc/op delta
ReadBlocks-16 1.30MB ± 0% 1.25MB ± 0% -3.86% (p=0.000 n=12+12)
name old allocs/op new allocs/op delta
ReadBlocks-16 9.50k ± 0% 8.45k ± 0% -11.05% (p=0.000 n=12+12)
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.
Lead Maintainer
Table of Contents
Install
go-cid is a standard Go module which can be installed with:
go get github.com/ipfs/go-cid
Usage
Running tests
Run tests with go test from the directory root
go test
Examples
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.")
}
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%