package main import ( "fmt" "sync" ) // InMemoryDB represents a simple in-memory key-value database type InMemoryDB struct { data map[string]string mu sync.RWMutex } // NewDB creates a new instance of InMemoryDB func NewDB() *InMemoryDB { return &InMemoryDB{ data: make(map[string]string), } } // Set adds a key-value pair to the database func (db *InMemoryDB) Set(key, value string) { .Lock() defer .Unlock() db.data[key] = value } // Get retrieves the value associated with the given key func (db *InMemoryDB) Get(key string) (string, bool) { .RLock() defer .RUnlock() value, found := db.data[key] return value, found } // Delete removes the given key from the database func (db *InMemoryDB) Delete(key string) { .Lock() defer .Unlock() delete(db.data, key) } func main() { // Create a new in-memory database db := NewDB() // Add some data to the database db.Set("username", "john_doe") db.Set("email", "john.doe@example.com") // Retrieve data from the database if username, found := db.Get("username"); found { fmt.Println("Username:", username) } else { fmt.Println("Username not found") } // Delete data from the database db.Delete("email") // Try to retrieve deleted data if email, found := db.Get("email"); found { fmt.Println("Email:", email) } else { fmt.Println("Email not found") } }package main import ( "fmt" "sync" ) // InMemoryDB represents a simple in-memory key-value database type InMemoryDB struct { data map[string]string mu sync.RWMutex } // NewDB creates a new instance of InMemoryDB func NewDB() *InMemoryDB { return &InMemoryDB{ data: make(map[string]string), } } // Set adds a key-value pair to the database func (db *InMemoryDB) Set(key, value string) { .Lock() defer .Unlock() db.data[key] = value } // Get retrieves the value associated with the given key func (db *InMemoryDB) Get(key string) (string, bool) { .RLock() defer .RUnlock() value, found := db.data[key] return value, found } // Delete removes the given key from the database func (db *InMemoryDB) Delete(key string) { .Lock() defer .Unlock() delete(db.data, key) } func main() { // Create a new in-memory database db := NewDB() // Add some data to the database db.Set("username", "john_doe") db.Set("email", "john.doe@example.com") // Retrieve data from the database if username, found := db.Get("username"); found { fmt.Println("Username:", username) } else { fmt.Println("Username not found") } // Delete data from the database db.Delete("email") // Try to retrieve deleted data if email, found := db.Get("email"); found { fmt.Println("Email:", email) } else { fmt.Println("Email not found") } }