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
Add a comment  | 

1 Answer 1

Reset to default 0

Basically 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