」工欲善其事,必先利其器。「—孔子《論語.錄靈公》
首頁 > 程式設計 > 如何檢查三個值是否相等?

如何檢查三個值是否相等?

發佈於2025-03-24
瀏覽:561

How Can I Check if Three Values are Equal in Go? 
檢查三個值的等式優雅

[g == b == b == b == c會導致語法錯誤,有其他方法可以確定三個值是否等於。方法This solution is straightforward and efficient, making comparisons on a per-pair basis.

Exploring Creative Solutions

Using 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