The append+make slice extension idiom works, but note that append uses
the slice's length as its base. We need to append the number of bytes
required for length to reach cidLength, not the capacity.
The added test case panicked before this change, and works now:
--- FAIL: TestReadCidsFromBuffer (0.00s)
panic: runtime error: slice bounds out of range [:73] with capacity 64 [recovered]
panic: runtime error: slice bounds out of range [:73] with capacity 64
goroutine 37 [running]:
testing.tRunner.func1.2({0x570d60, 0xc000016438})
testing/testing.go:1203 +0x24e
testing.tRunner.func1()
testing/testing.go:1206 +0x218
panic({0x570d60, 0xc000016438})
runtime/panic.go:1038 +0x215
github.com/ipfs/go-cid.CidFromReader({0x5b0e20, 0xc000010900})
github.com/ipfs/go-cid/cid.go:803 +0x75f
github.com/ipfs/go-cid.TestReadCidsFromBuffer(0xc00014ba00)
github.com/ipfs/go-cid/cid_test.go:710 +0x625
testing.tRunner(0xc00014ba00, 0x58af38)
testing/testing.go:1253 +0x102
created by testing.(*T).Run
testing/testing.go:1300 +0x35a
exit status 2
FAIL github.com/ipfs/go-cid 0.004s
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%