Skip to content

Instantly share code, notes, and snippets.

@maxwellgithinji
Created July 27, 2020 17:05
Show Gist options
  • Save maxwellgithinji/17d05f99650f7cbf4faf998c33b4e714 to your computer and use it in GitHub Desktop.
Save maxwellgithinji/17d05f99650f7cbf4faf998c33b4e714 to your computer and use it in GitHub Desktop.
//NOte you can easily swap the storage for your database without changing much of the code
package main
import (
"fmt"
)
type user struct {
first string
}
type mongo map[int]user
type hardrive map[int]user
func (m mongo) save(n int, u user) {
m[n] = u
}
func (m mongo) retrieve(n int) user {
return m[n]
}
func (hd hardrive) save(n int, u user) {
hd[n] = u
}
func (hd hardrive) retrieve(n int) user {
return hd[n]
}
type accessor interface {
save(n int, u user)
retrieve(n int) user
}
func put(a accessor, n int, u user) {
a.save(n , u)
}
func get ( a accessor, n int) user {
return a.retrieve(n)
}
func main() {
storage := mongo{}
p1 := user {
first: "Ula Britta Smita Frita",
}
p2 := user {
first: "Joergen Smorgen",
}
put(storage, 1, p1)
put(storage, 2, p2)
fmt.Println(get(storage, 1), "\n", get(storage, 2), "\n")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment