检查三个值的等式优雅
[g == b == b == b == c会导致语法错误,有其他方法可以确定三个值是否等于。方法This solution is straightforward and efficient, making comparisons on a per-pair basis.
Exploring Creative SolutionsUsing a Map as a Set:
if a == b && a == c {
fmt.Println("All 3 are equal")
}
With Arrays:
Arrays are comparable, allowing us to compare multiple elements at once:
if [2]interface{}{a, b} == [2]interface{}{b, c} { fmt.println(“所有3个都相等”) }
With Anonymous Structs:
if len(map[interface{}]int{a: 0, b: 0, c: 0}) == 1 {
fmt.Println("All 3 are equal")
}
if struct{ a, b interface{} }{a, b} == struct{ a, b interface{} } {b,c} { fmt.println(“所有3个都相等”) } ,带有slices:
if反思 fmt.println(“所有3个都相等”) }
使用助手函数:if [2]interface{}{a, b} == [2]interface{}{b, c} {
fmt.Println("All 3 are equal")
}
对于_,s:= range v {
如果a!= s {
返回false
}
}
}
返回true
} 免责声明: 提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发到邮箱:[email protected] 我们会第一时间内为您处理。
Copyright© 2022 湘ICP备2022001581号-3