In [1]:
type TrieMap struct {
    node  map[byte]*TrieMap
    empty bool
    value string
}
In [2]:
func (tm *TrieMap) add(key, value string) {
    cur := tm
    for _, c := range []byte(key) {
        if _, ok := cur.node[c]; ! ok {
            cur.node[c] = &TrieMap{make(map[byte]*TrieMap), true, ""}
        }
        cur = cur.node[c]
    }
    cur.empty = false
    cur.value = value
}
In [3]:
func NewTrieMap(keys, values []string) *TrieMap {
    tm := &TrieMap{make(map[byte]*TrieMap), true, ""}
    for i, key := range keys {
        tm.add(key, values[i])
    }
    return tm
}
In [4]:
func (tm *TrieMap) query(key string) (string, bool) {
    cur := tm
    for _, c := range []byte(key) {
        val, ok := cur.node[c]
        if !ok {
            return "", false
        }
        cur = val
    }
    return cur.value, !cur.empty
}

Unfortunately, this example's self-referential struct means it does not work in this Go notebook. It works fine as a typical go program, though -- try it!

In [ ]:
keys := []string{"hello", "there", "the"}
values := []string{"1", "2", "3"}
tm := NewTrieMap(keys, values)
In [ ]:
tm.query("hello")       // returns "1", true
tm.query("hello there") // returns "", false
tm.query("there")       // returns "2", true
tm.query("ther")        // returns "", false
tm.query("the")         // returns "3", true
tm.query("")            // erturns "", false