Last active
May 16, 2018 19:45
-
-
Save rogerwelin/3c988d131d420398c93742f51696d8bb to your computer and use it in GitHub Desktop.
key-value.go
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 ( | |
"bufio" | |
"encoding/gob" | |
"fmt" | |
"os" | |
"strings" | |
) | |
type myElement struct { | |
Name string | |
Surname string | |
Id string | |
} | |
var DATA = make(map[string]myElement) | |
var DATAFILE = "/tmp/dataFile.gob" | |
func save() error { | |
fmt.Println("Saving", DATAFILE) | |
err := os.Remove(DATAFILE) | |
if err != nil { | |
fmt.Println(err) | |
} | |
saveTo, err := os.Create(DATAFILE) | |
if err != nil { | |
fmt.Println("Cannot create", DATAFILE) | |
return err | |
} | |
defer saveTo.Close() | |
encoder := gob.NewEncoder(saveTo) | |
err = encoder.Encode(DATA) | |
if err != nil { | |
fmt.Println("Cannot save to", DATAFILE) | |
return err | |
} | |
return nil | |
} | |
func load() error { | |
fmt.Println("Loading", DATAFILE) | |
loadFrom, err := os.Open(DATAFILE) | |
defer loadFrom.Close() | |
if err != nil { | |
fmt.Println("Empty key/value store!") | |
return err | |
} | |
decoder := gob.NewDecoder(loadFrom) | |
decoder.Decode(&DATA) | |
return nil | |
} | |
func ADD(k string, n myElement) bool { | |
if k == "" { | |
return false | |
} | |
if LOOKUP(k) == nil { | |
DATA[k] = n | |
return true | |
} | |
return false | |
} | |
func DELETE(k string) bool { | |
if LOOKUP(k) != nil { | |
delete(DATA, k) | |
return true | |
} | |
return false | |
} | |
func LOOKUP(k string) *myElement { | |
_, ok := DATA[k] | |
if ok { | |
n := DATA[k] | |
return &n | |
} else { | |
return nil | |
} | |
} | |
func CHANGE(k string, n myElement) bool { | |
DATA[k] = n | |
return true | |
} | |
func PRINT() { | |
for k, d := range DATA { | |
fmt.Printf("key: %s value: %v\n", k, d) | |
} | |
} | |
func main() { | |
err := load() | |
if err != nil { | |
fmt.Println(err) | |
} | |
scanner := bufio.NewScanner(os.Stdin) | |
for scanner.Scan() { | |
text := scanner.Text() | |
text = strings.TrimSpace(text) | |
tokens := strings.Fields(text) | |
switch len(tokens) { | |
case 0: | |
continue | |
case 1: | |
tokens = append(tokens, "") | |
tokens = append(tokens, "") | |
tokens = append(tokens, "") | |
tokens = append(tokens, "") | |
case 2: | |
tokens = append(tokens, "") | |
tokens = append(tokens, "") | |
tokens = append(tokens, "") | |
case 3: | |
tokens = append(tokens, "") | |
tokens = append(tokens, "") | |
case 4: | |
tokens = append(tokens, "") | |
} | |
switch tokens[0] { | |
case "PRINT": | |
PRINT() | |
case "STOP": | |
err = save() | |
if err != nil { | |
fmt.Println(err) | |
} | |
return | |
case "DELETE": | |
if !DELETE(tokens[1]) { | |
fmt.Println("Delete operation failed!") | |
} | |
case "ADD": | |
n := myElement{tokens[2], tokens[3], tokens[4]} | |
if !ADD(tokens[1], n) { | |
fmt.Println("Add operation failed!") | |
} | |
case "LOOKUP": | |
n := LOOKUP(tokens[1]) | |
if n != nil { | |
fmt.Printf("%v\n", *n) | |
} | |
case "CHANGE": | |
n := myElement{tokens[2], tokens[3], tokens[4]} | |
if !CHANGE(tokens[1], n) { | |
fmt.Println("Update operation failed!") | |
} | |
default: | |
fmt.Println("Unknown command – please try again!") | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment