I'm trying to make a wrapper in go for the map
type so that I can add some methods like contains()
(this almost makes me miss Java). However, I don't know if I can do anything like generics in Java.
While almost everything I've read says that Go doesn't have generic types, there must be a better way than writing a separate struct for every single possible combination of structs and values that I'm using.
Here's what I'm trying to do, even though the code doesn't work:
func newMap(key interface{}, val interface{}) {
keytype := key.(type)
valtype := val.(type)
return hashmap{map[keytype]valtype}
}
type hashmap struct {
hashmap map[]
}
Any help/explanation on how to do this would be appreciated.
EDIT: Contains
isn't the only thing I'm trying to replicate, hence the desire to make a "generic" hashmap. replace()
, isEmpty()
, all that fun stuff as well.
_, ok := m[key]
. Are there other methods that you want to add?len(m) == 0
.len(m) == 0
, replace:if _, ok := m[key]; ok { m[key] = newV }