admin管理员组文章数量:1401914
In golang, I defined a custom struct
type Station struct {
Name string
StationLine []string
}
Station struct is not comparable because StationLine is []string which is not comparable. Hence, I cannot create map[Station]distance (error: invalid map key type) and I cannot use slices.Contains(stations, stn1). This is very inconvenient.
Actually, for comparing purposes, Station struct can just compare Station.Name and ignore StationLine. Station.Name will be unique.
I cannot use Station.Name as replacement of Station in map key, because Station contains metadata like StationLine, which will be used in function. And I don't want to create a reverse lookup function from string to Station, because it looks clumsy.
I remember in C++, we can define custom operation in class, so I am wondering if I can define comparition function for my custom struct so that it can be used as map[key]?
In golang, I defined a custom struct
type Station struct {
Name string
StationLine []string
}
Station struct is not comparable because StationLine is []string which is not comparable. Hence, I cannot create map[Station]distance (error: invalid map key type) and I cannot use slices.Contains(stations, stn1). This is very inconvenient.
Actually, for comparing purposes, Station struct can just compare Station.Name and ignore StationLine. Station.Name will be unique.
I cannot use Station.Name as replacement of Station in map key, because Station contains metadata like StationLine, which will be used in function. And I don't want to create a reverse lookup function from string to Station, because it looks clumsy.
I remember in C++, we can define custom operation in class, so I am wondering if I can define comparition function for my custom struct so that it can be used as map[key]?
Share Improve this question asked Mar 22 at 14:29 naive programmernaive programmer 91 silver badge5 bronze badges 5- In Go the type itself must be comparable. Just use whatever form you would use in a custom comparator as the map key directly. In almost every case, a string is sufficient. – Mr_Pink Commented Mar 22 at 14:49
- 1 "How to make custom struct comparable and be used as key to map" This is simply impossible, you cannot and must redesign. – Volker Commented Mar 22 at 14:49
- @Mr_Pink the reason to use string map key is that I need to access Station. StationLine. For example, in a function getDistance(Station1, Station2, PreviousStation) // I would need to access distances which is a map[station]distance and then check Station1. StationLine. If I have to use string, then I need to implement reverse lookup from string to Station. It is working, but I am exploring a better solution – naive programmer Commented Mar 23 at 2:45
- You can serialize your struct to a comparable type (such as string) and use the serialized value as key for your map. For example, JSON-encode your struct or create a hash function that converts your struct to an integer (which is also a comparable type). – Victor Ruiz Commented Mar 28 at 8:42
- Got it. Many thanks for the comments. I find it a bit weird and hard. In c++, as long as you define comparsion, then c++ allows you to use custom struct as map key. But I am now aware of language limitation – naive programmer Commented Mar 29 at 13:12
1 Answer
Reset to default 0Basically you can't unless all the types in the struct are comparable.
One way is to implement some kind of hash func and use that as keys for the map.
本文标签: dictionaryHow to make custom struct comparable and be used as key to mapStack Overflow
版权声明:本文标题:dictionary - How to make custom struct comparable and be used as key to map - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744311522a2600052.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论