如何优雅地检查三个值的相等性?

说我有价值观abc。我想知道它们是否相等。如果我做

if a == b == c{...}

然后我得到一个编译错误

invalid operation: a == b == c (mismatched types bool and TypeOfABandC)

这很明显,因为它解析为:

(a == b) == c

并且(a == b)是一个布尔值。

我当然可以:

if a == b && a == c {...}

然而,这不是很好看,感觉混乱。还有其他方法吗?


汪汪一只猫
浏览 340回答 1
1回答

摇曳的蔷薇

事先说明:您最后提出的解决方案是比较 3 个值是否相等的最短、最清晰和最有效的方法:if a == b && a == c {&nbsp; &nbsp; fmt.Println("Clearest: all 3 are equal")}或者(根据您的喜好):if a == b && b == c {&nbsp; &nbsp; fmt.Println("Clearest: all 3 are equal")}这个答案的其余部分(接下来的内容)只是在玩弄语言规范和语言的功能,展示我觉得有趣和有创意的东西。他们并不试图提供更好的解决方案。在Go Playground上尝试以下所有示例。这些示例建立在Spec:Comparison operator中定义的术语和比较结果的基础上。一般说明:在下面的示例中,我使用的类型适用于interface{}您的值具有的任何类型(a和b)c,但是如果您知道它们是类型int,例如,您也可以使用该特定类型(这将提高效率并缩短示例的长度)。以一个map为一组if len(map[interface{}]int{a: 0, b: 0, c: 0}) == 1 {&nbsp; &nbsp; fmt.Println("Map set: all 3 are equal")}实际上,我们将所有可比较的值放入 amap作为键,如果都相等,则映射中将只有 1 对,因此映射的“长度”将为 1。映射的值类型不播放任何角色在这里,它可以是任何东西。我使用它int是因为这会产生最短的复合文字(定义map值)。这个解决方案很灵活,因为您可以使用任意数量的值来测试是否都相等,而不仅仅是 3。带数组数组是可比较的(不像切片):if [2]interface{}{a, b} == [2]interface{}{b, c} {&nbsp; &nbsp; fmt.Println("Arrays: all 3 are equal")}数组比较的结果将是trueifa == b和b == c(如果相应的元素相等)。请注意,您也可以将此方法应用于任意数量的值。对于 5 个值,它看起来像这样:if [4]interface{}{a, b, c, d} == [4]interface{}{b, c, d, e} {&nbsp; &nbsp; fmt.Println("Arrays: all 5 are equal")}带着诡计 mapif map[interface{}]bool{a: b == c}[b] {&nbsp; &nbsp; fmt.Println("Tricky map: all 3 are equal")}这个复合文字会将比较结果分配给b == ckey a。我们询问与 key 关联的值b。如果a == b,则索引表达式的结果将是 的结果b == c,即所有 3 个值是否相等。如果a != b,则值类型的零值将是结果,false在 的情况下bool,正确地说明所有 3 个值都不相等。带匿名struct的struct 值也具有可比性,因此:if struct{ a, b interface{} }{a, b} == struct{ a, b interface{} }{b, c} {&nbsp; &nbsp; fmt.Println("Anon structs: all 3 are equal")}与(命名)structs如果我们struct预先定义一个简单的:type P struct{ a, b interface{} }比较会更紧凑:if (P{a, b} == P{b, c}) {&nbsp; &nbsp; fmt.Println("Structs: all 3 are equal")}(请注意,if语句的表达式必须放在括号中以避免解析歧义- 否则它是编译时错误!)带切片切片是不可比较的,所以我们需要从以下方面借一些帮助reflect.DeepEqual():if reflect.DeepEqual([]interface{}{a, b}, []interface{}{b, c}) {&nbsp; &nbsp; fmt.Println("Slices: all 3 are equal")}带有辅助功能func AllEquals(v ...interface{}) bool {&nbsp; &nbsp; if len(v) > 1 {&nbsp; &nbsp; &nbsp; &nbsp; a := v[0]&nbsp; &nbsp; &nbsp; &nbsp; for _, s := range v {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if a != s {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return false&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; return true}并使用它:if AllEquals(a, b, c) {&nbsp; &nbsp; fmt.Println("Helper function: all 3 are equal")}此解决方案也很灵活,您可以AllEquals()使用任意数量的值进行调用。请注意,AllEquals()如果我们也愿意在reflect.DeepEqual()这里调用,我们可以使函数更紧凑:func AllEquals2(v ...interface{}) bool {&nbsp; &nbsp; if len(v) < 2 {&nbsp; &nbsp; &nbsp; &nbsp; return true&nbsp; &nbsp; }&nbsp; &nbsp; return reflect.DeepEqual(v[:len(v)-1], v[1:])}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Go