Stian OK
Stian OK

Reputation: 666

Unexpected value when getting value from a map

So I have a struct like this:

type Magni struct {
    ...
    Handlers map[string]func(*Message)
    ...
}

And I have a function to create a new instance of the struct:

func New(nick, user, real string) *Magni {
    return &Magni{
        ...
        Handlers: make(map[string]func(*Message)),
        ...
    }
}

But I can't get something from the Handlers map with the key "hey" when "hey" is in a variable, it only works if I type it myself. Here is a method of the struct Magni and m is a pointer to the struct Magni:

handler := m.Handlers[cmd[3][1:]] // cmd[3][1:] contains string "hey"
handler2 := m.Handlers["hey"]

For some reason, the value of handler is nil and the value of handler2 is 0x401310, basically I am not expecting handler to be nil.

Am I doing something wrong or is this the expected behavior?

Upvotes: 2

Views: 215

Answers (2)

icza
icza

Reputation: 417592

Getting the value based on the value of a variable works:

m := map[string]string{"hey": "found"}
fmt.Println(m["hey"]) // found

cmd := []string{"1", "2", "3", "hey"}
fmt.Println(m[cmd[3]]) // found

It even works if the variable is of string type and you slice its value, e.g.:

cmd = []string{"1", "2", "3", "Hhey"}
fmt.Println(m[cmd[3][1:]]) // found

You issue is most likely cmd[3] is the string "hey" itself, but if you slice it like cmd[3][1:], it will cut off the first character (or to be precise: the first byte from its UTF-8 encoding sequence, the memory representation of strings, but the characters of "hey" map to bytes one-to-one), so it will be "ey", for which you will not find any associated value in the map of course:

cmd = []string{"1", "2", "3", "hey"}
fmt.Println(m[cmd[3][1:]]) // NOT FOUND (empty string - zero value)

Try these on the Go Playground.

If cmd[3] is "hey", no need to slice it, just use it as a key.

Edit: You claim cmd[3] contains the string ":hey". If it would, it would also work:

cmd = []string{"1", "2", "3", ":hey"}
fmt.Println(m[cmd[3][1:]]) // found

So your cmd[3] is not what you think it is. It may contain 0 bytes or unprintable characters. Print its bytes to verify. For example bytes of the string ":hey" are: [58 104 101 121]

fmt.Println([]byte(":hey")) // Prints [58 104 101 121]

Print your cmd[3] to verify:

fmt.Println([]byte(cmd[3]))

You could also compare it to the strings you think it is, but that will only tell you whether they are equal (and won't tell you where the difference is):

fmt.Println(cmd[3] == ":hey", cmd[3][1:] == "hey")

Upvotes: 1

Stian OK
Stian OK

Reputation: 666

Trimming the string solves the problem.

strings.TrimSpace(cmd[3])

Upvotes: 0

Related Questions