Skip to content

Instantly share code, notes, and snippets.

@ravisantoshgudimetla
Last active December 12, 2017 05:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ravisantoshgudimetla/6cb8a08c5e88cbdfd9d4ea489790497b to your computer and use it in GitHub Desktop.
Save ravisantoshgudimetla/6cb8a08c5e88cbdfd9d4ea489790497b to your computer and use it in GitHub Desktop.
KVStore with map
package KVStore
type Item struct {
Key string
Val int
}
type Store map[string]int
// Checks if the item exists in the store. If the key exists it returns the value else it returns -1.
func (s Store) checkIfItemsExistsInStore(key string) int {
if value, ok := s[key]; !ok{
return -1
} else {
return value
}
}
// Get returns value associated with key, if the item exists or else returns -1.
func (s Store) Get(key string) int {
return s.checkIfItemsExistsInStore(key)
}
// Put inserts the given item in the store. On success, it returns a 0 and if the item already exists it returns a -1.
func (s Store) Put(item Item) int {
index := s.checkIfItemsExistsInStore(item.Key)
if index == -1 {
s[item.Key] = item.Val
return 0
}
return -1
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment