github.com/cellofellow/gopkg@v0.0.0-20140722061823-eec0544a62ad/database/leveldb.chai2010/hello.go (about) 1 // Copyright 2013 <chaishushan{AT}gmail.com>. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 // +build ignore 6 7 package main 8 9 import ( 10 "fmt" 11 "log" 12 13 "chai2010.gopkg/database/leveldb.chai2010" 14 ) 15 16 func main() { 17 fmt.Printf("hello leveldb-%d.%d!\n", 18 leveldb.MajorVersion, 19 leveldb.MinorVersion, 20 ) 21 22 opt := &leveldb.Options{} 23 opt.SetFlag(leveldb.OFCreateIfMissing) 24 opt.SetBlockCacheCapacity(32 << 20) 25 26 db, err := leveldb.Open("testdb", opt) 27 if err != nil { 28 log.Fatal(err) // *leveldb.Error 29 } 30 defer db.Close() 31 32 db.Put([]byte("key1"), []byte("value1"), nil) 33 db.Put([]byte("key2"), []byte("value2"), nil) 34 db.Put([]byte("key3"), []byte("value3"), nil) 35 36 it := db.NewIterator(nil) 37 defer it.Release() 38 39 for it.SeekToFirst(); it.Valid(); it.Next() { 40 fmt.Printf("%s: %s\n", string(it.Key()), string(it.Value())) 41 } 42 if err := it.GetError(); err != nil { 43 fmt.Printf("err: %v\n", err) 44 } 45 }