Skip to content

Instantly share code, notes, and snippets.

@reetasingh
Last active July 4, 2023 22:07
Show Gist options
  • Save reetasingh/123d71aed0c79eff5d4326f4117244a9 to your computer and use it in GitHub Desktop.
Save reetasingh/123d71aed0c79eff5d4326f4117244a9 to your computer and use it in GitHub Desktop.
// You can edit this code!
// Click here and start typing.
package main
import "fmt"
type Person struct {
ID int
Name string
}
type personDB interface {
save(person Person) error
}
type LocalDatabase struct{}
type InMemoryDatabase struct {
m map[int]Person
}
func (db *LocalDatabase) save(person Person) error {
// Implementation for saving a person to the local database
fmt.Printf("Saving person in Local DB: %+v\n", person)
return nil
}
func (db *InMemoryDatabase) save(person Person) error {
// Implementation for saving a person to the map in meory
fmt.Printf("Saving person in memory DB: %+v\n", person)
mapd := db.m
if mapd == nil {
mapd = make(map[int]Person)
}
mapd[person.ID] = person
return nil
}
func main() {
db := new(LocalDatabase)
person := Person{
ID: 101,
Name: "John D",
}
db.save(person)
memorydb := new(InMemoryDatabase)
memorydb.save(person)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment