Using user defined struct as a key in map

Hi All,

I am new to GoLang and trying to grasp the concept here.So i have a question like in java where you can use any user defined object as a key where equal() and hashCode() function is defined for that object.

Is there any provision similar to that in GoLang as well?

Thanks in advance.

Map keys may be of any type that is comparable. By comparable you have boolean, numeric, string, pointer, channel, interface types, and structs or arrays that contain only those types.

You can not use slices, maps, and functions; these types cannot be compared using ==, and may not be used as map keys.

Hi Yamil_bracho,

I think i did not put my question properly.My question was suppose i use any build in type as a key in GoLang then i can thing there is a built in support for the all the build in types like you mentioned boolean, string,int etc…but for the types like arrays ,struct and interfaces which can be user defined type there is a need from the programmer to define how two values of user defined type should be compared…so my question was around that in maps as the keys needs to be compared or in slices when the values needs to be compared before returning…so how is that done in the GoLang.


Hi. For the built in map type can you only use the built in types and structs as @Yamil_Bracho explained. You can not define any new type by implementing equal() and getHash() as you do in java

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.