Last active
December 12, 2017 05:01
-
-
Save ravisantoshgudimetla/6cb8a08c5e88cbdfd9d4ea489790497b to your computer and use it in GitHub Desktop.
KVStore with map
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 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