diff --git a/README.md b/README.md index eb05c1ed..813eee02 100644 --- a/README.md +++ b/README.md @@ -10,34 +10,6 @@ See `test_coverage.txt` for the current coverage (using gocov). Alternatively, if you are running a POSIX OS, you can run the cov_report.sh script for a real-time report. Package btcdb is licensed under the liberal ISC license. -## Sample Use - -```Go - // Import packages. - import ( - "github.com/conformal/btcdb" - _ "github.com/conformal/btcdb/ldb" - "github.com/conformal/btcnet" - "github.com/conformal/btcutil" - ) - - // Create a database and schedule it to be closed on exit. - dbName := "example" - db, err := btcdb.CreateDB("leveldb", dbName) - if err != nil { - // Log and handle the error - } - defer db.Close() - - - // Insert the main network genesis block. - genesis := btcutil.NewBlock(btcnet.MainNetParams.GenesisBlock) - newHeight, err := db.InsertBlock(genesis) - if err != nil { - // Log and handle the error - } -``` - ## Documentation [![GoDoc](https://godoc.org/github.com/conformal/btcdb?status.png)] @@ -57,6 +29,13 @@ http://localhost:6060/pkg/github.com/conformal/btcdb $ go get github.com/conformal/btcdb ``` +## Examples + +* [CreateDB Example] + (http://godoc.org/github.com/conformal/btcdb#example-CreateDB) + Demonstrates creating a new database and inserting the genesis + block into it. + ## TODO - Increase test coverage to 100% diff --git a/doc.go b/doc.go index 5a89f75d..559428ff 100644 --- a/doc.go +++ b/doc.go @@ -27,30 +27,6 @@ At the highest level, the use of this packages just requires that you import it, setup a database, insert some data into it, and optionally, query the data back. The first block inserted into the database will be treated as the genesis block. Every subsequent block insert requires the -referenced parent block to already exist. In a more concrete example: - - // Import packages. - import ( - "github.com/conformal/btcdb" - _ "github.com/conformal/btcdb/ldb" - "github.com/conformal/btcnet" - "github.com/conformal/btcutil" - ) - - // Create a database and schedule it to be closed on exit. - dbName := "example.db" - db, err := btcdb.CreateDB("leveldb", dbName) - if err != nil { - // Log and handle the error - } - defer db.Close() - - - // Insert the main network genesis block. - genesis := btcutil.NewBlock(btcnet.MainNetParams.GenesisBlock) - newHeight, err := db.InsertBlock(genesis) - if err != nil { - // Log and handle the error - } +referenced parent block to already exist. */ package btcdb diff --git a/example_test.go b/example_test.go new file mode 100644 index 00000000..eda1c0d1 --- /dev/null +++ b/example_test.go @@ -0,0 +1,42 @@ +// Copyright (c) 2013-2014 Conformal Systems LLC. +// Use of this source code is governed by an ISC +// license that can be found in the LICENSE file. + +package btcdb_test + +import ( + "fmt" + "github.com/conformal/btcdb" + _ "github.com/conformal/btcdb/memdb" + "github.com/conformal/btcnet" + "github.com/conformal/btcutil" +) + +// This example demonstrates creating a new database and inserting the genesis +// block into it. +func ExampleCreateDB() { + // Create a database and schedule it to be closed on exit. This example + // uses a memory-only database to avoid needing to write anything to + // the disk. Typically, you would specify a persistent database driver + // such as "leveldb" and give it a database name as the second + // parameter. + db, err := btcdb.CreateDB("memdb") + if err != nil { + fmt.Println(err) + return + } + defer db.Close() + + // Insert the main network genesis block. + genesis := btcutil.NewBlock(btcnet.MainNetParams.GenesisBlock) + newHeight, err := db.InsertBlock(genesis) + if err != nil { + fmt.Println(err) + return + } + + fmt.Println("New height:", newHeight) + + // Output: + // New height: 0 +}