Make sure your key don't include the delimiter '
' included by bytes.ReadString()
:
(and more generally, don't ignore the return values, especially the errors)
See this runnable(!) example on http://ideone.com/qgvsmF:
package main
import "bufio"
import "fmt"
import "os"
func main() {
table := make(map[string]string)
table["key1"] = "val1"
table["key2"] = "val2"
table["key3"] = "val3"
fmt.Println("Enter a key (followed by Return):")
reader := bufio.NewReader(os.Stdin)
input, err := reader.ReadString('
')
if err != nil {
panic(err)
}
val, ok := table[input]
fmt.Println("val for key '" + input + "' = '" + val + "'(" + fmt.Sprintf("%v", ok) + ")")
}
It will show a key equals to:
'key2
'
Remove the last character of your string, as in this runnable example:
input = strings.TrimRight(input, "
")
Output:
Enter a key (followed by Return):
val for key 'key2' = 'val2'(true)