上級 › スライスやマップを含む構造体は == で比較できない › パターン3
スライスやマップを含む構造体は == で比較できない
スライスやマップ、関数値をフィールドに持つ構造体は比較不可能な型として扱われ、==演算子で比較しようとするとコンパイルエラーになります。
1 package main 2 3 import "fmt" 4 5 type Config struct { 6 Options map[string]bool 7 } 8 9 func main() { 10 a := Config{Options: map[string]bool{"x": true}} 11 b := Config{Options: map[string]bool{"x": true}} 12 fmt.Println(________________________________) ^ 13 }
./main.go:12:14: invalid operation: a == b (struct containing map[string]bool cannot be compared)
空欄を埋めて実行を通す
マップもスライスと同様に比較演算子を持たない型で、含む構造体全体の比較を不可能にします。
次の問題