檢查三個值的等式優雅
[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