Created
August 19, 2024 16:13
-
-
Save mrizkimaulidan/30d3a30decf323dc2c5a6c22e98ba86c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
type InMemory struct { | |
// | |
} | |
var usersFromInMemory = []map[string]any{ | |
{ | |
"id": 1, | |
"name": "John Doe", | |
"age": 21, | |
}, | |
{ | |
"id": 2, | |
"name": "Elisa Doe", | |
"age": 35, | |
}, | |
} | |
func (r InMemory) GetAll() []map[string]any { | |
return usersFromInMemory | |
} | |
func (r InMemory) Find(id int) map[string]any { | |
for _, user := range usersFromInMemory { | |
if user["id"] == id { | |
return user | |
} | |
} | |
return nil | |
} | |
func NewInMemory() Storage { | |
return InMemory{} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import "fmt" | |
func main() { | |
// storage := NewRedis() | |
// storage := NewInMemory() | |
// storage := NewMongoDB() | |
storage := NewPostgreSQL() | |
fmt.Println(storage.GetAll()) | |
fmt.Println(storage.Find(1)) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
type MongoDB struct { | |
// | |
} | |
var usersFromMongoDB = []map[string]any{ | |
{ | |
"id": 1, | |
"name": "Mace Windu", | |
"age": 53, | |
}, | |
{ | |
"id": 2, | |
"name": "Jabba The Hut", | |
"age": 650, | |
}, | |
} | |
func (r MongoDB) GetAll() []map[string]any { | |
return usersFromMongoDB | |
} | |
func (r MongoDB) Find(id int) map[string]any { | |
for _, user := range usersFromMongoDB { | |
if user["id"] == id { | |
return user | |
} | |
} | |
return nil | |
} | |
func NewMongoDB() Storage { | |
return MongoDB{} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
type PostgreSQL struct { | |
// | |
} | |
var usersFromPostgreSQL = []map[string]any{ | |
// | |
} | |
func (r PostgreSQL) GetAll() []map[string]any { | |
return nil | |
} | |
func (r PostgreSQL) Find(id int) map[string]any { | |
return nil | |
} | |
func NewPostgreSQL() Storage { | |
return PostgreSQL{} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
type Redis struct { | |
// | |
} | |
var usersFromRedis = []map[string]any{ | |
{ | |
"id": 1, | |
"name": "John Doe", | |
"age": 21, | |
}, | |
{ | |
"id": 2, | |
"name": "Elisa Doe", | |
"age": 35, | |
}, | |
} | |
func (r Redis) GetAll() []map[string]any { | |
return usersFromRedis | |
} | |
func (r Redis) Find(id int) map[string]any { | |
for _, user := range usersFromRedis { | |
if user["id"] == id { | |
return user | |
} | |
} | |
return nil | |
} | |
func NewRedis() Storage { | |
return Redis{} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
type Storage interface { | |
GetAll() []map[string]any | |
Find(id int) map[string]any | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment