import ( "fmt" "github.com/ethereum/go-ethereum/ethdb" ) func main() { // Open a new database with LevelDB backend db, err := ethdb.NewLDBDatabase("my-database") if err != nil { fmt.Println("Error opening database:", err) return } // Store a new key-value pair key := []byte("hello") value := []byte("world") err = db.Put(key, value) if err != nil { fmt.Println("Error storing data:", err) } // Retrieve the value for the given key result, err := db.Get(key) if err != nil { fmt.Println("Error retrieving data:", err) return } fmt.Println(string(result)) // Output: world // Close the database err = db.Close() if err != nil { fmt.Println("Error closing database:", err) } }
import ( "fmt" "github.com/ethereum/go-ethereum/ethdb" ) func main() { // Open a new database with RocksDB backend db, err := ethdb.NewRocksDBDatabase("my-database") if err != nil { fmt.Println("Error opening database:", err) return } // Create a new WriteBatch batch := ethdb.NewBatch() // Add multiple key-value pairs to the WriteBatch batch.Put([]byte("key1"), []byte("value1")) batch.Put([]byte("key2"), []byte("value2")) batch.Put([]byte("key3"), []byte("value3")) // Write the batch to the database err = db.Write(batch) if err != nil { fmt.Println("Error writing batch:", err) } // Retrieve the values for the given keys result1, _ := db.Get([]byte("key1")) result2, _ := db.Get([]byte("key2")) result3, _ := db.Get([]byte("key3")) fmt.Println(string(result1)) // Output: value1 fmt.Println(string(result2)) // Output: value2 fmt.Println(string(result3)) // Output: value3 // Close the database err = db.Close() if err != nil { fmt.Println("Error closing database:", err) } }In both examples, the ethdb package is imported from the "github.com/ethereum/go-ethereum/ethdb" library.