fork(1) download
  1. package main
  2.  
  3. import "bufio"
  4. import "fmt"
  5. import "os"
  6. import "strings"
  7.  
  8. func main() {
  9. table := make(map[string]string)
  10.  
  11. table["key1"] = "val1"
  12. table["key2"] = "val2"
  13. table["key3"] = "val3"
  14.  
  15. fmt.Println("Enter a key (followed by Return):")
  16. reader := bufio.NewReader(os.Stdin)
  17. input, err := reader.ReadString('\n')
  18. if err != nil {
  19. panic(err)
  20. }
  21.  
  22. val, ok := table[input]
  23. fmt.Println("val for key '" + input + "' = '" + val + "'(" + fmt.Sprintf("%v", ok) + ")")
  24. input = strings.TrimRight(input, "\n\r")
  25. val, ok = table[input]
  26. fmt.Println("val for key '" + input + "' = '" + val + "'(" + fmt.Sprintf("%v", ok) + ")")
  27. }
Success #stdin #stdout 0s 420480KB
stdin
key2
stdout
Enter a key (followed by Return):
val for key 'key2
' = ''(false)
val for key 'key2' = 'val2'(true)