比较两个不同类型(但基础类型相同)的值

时间:2019-03-27 20:00:26

标签: go types compare

如何比较两个类型不同但基础类型相同的变量的值。

type   SomeID    uint8
type   OtherID   uint8

func areEqual(sid SomeID, oid OtherID) bool {
   // error: mismatched types
   return sid == oid
}

2 个答案:

答案 0 :(得分:3)

如mkopriva所述,需要进行类型转换以比较这些值。

https://tour.golang.org/basics/13

Car

答案 1 :(得分:0)

您可能不太准确,不需要记住基础类型

consensus_name

而且您可以更通用

    group    col1  col2             name   consensus_name
0       1     dog    40          canidae          canidae
1       1     dog    40          canidae          canidae
2       1     dog    40          canidae          canidae
3       1     dog    40          canidae          canidae
4       1     dog    40              NaN          canidae
5       1     dog    40          canidae          canidae
6       1     dog    40          canidae          canidae
7       2    frog    85    dendrobatidae    dendrobatidae
8       2    frog    89  leptodactylidae  leptodactylidae
9       2    frog    89  leptodactylidae  leptodactylidae
10      2    frog    82  leptodactylidae  leptodactylidae
11      2    frog    89              NaN  leptodactylidae
12      2    frog    81              NaN  leptodactylidae
13      2    frog    89    dendrobatidae    dendrobatidae
14      3   horse    87         equidae1         equidae1
15      3  donkey    76         equidae2         equidae2
16      3   zebra    67         equidae3         equidae3
17      4    bird    54      psittacidae      psittacidae
18      4    bird    56              NaN      psittacidae
19      4    bird    34              NaN      psittacidae
20      5    bear    67              NaN              NaN
21      5    bear    54              NaN              NaN

Playground