Skip to content

Instantly share code, notes, and snippets.

@sekky0905
Last active March 19, 2018 00:41
Show Gist options
  • Save sekky0905/530aca5ca51b2932998b692ef05c2a34 to your computer and use it in GitHub Desktop.
Save sekky0905/530aca5ca51b2932998b692ef05c2a34 to your computer and use it in GitHub Desktop.
Goにおける等値と等価の考察(struct1==struct2と&struct1==&struct2とreflect.DeepEqual(struct1,struct2)とreflect.DeepEqual(&struct1,&struct2)) ref: https://qiita.com/Sekky0905/items/1ff4979d80b163e0aeb6
fmt.Println(bar)
p1 := Person{
Name: "太郎",
Age: 20,
}
p2 := Person{
Name: "太郎",
Age: 20,
}
fmt.Println("単純な構造体の比較")
fmt.Printf("p1 == p2 : 等価(メモリが別でも値が一緒だったらOK) : %t\n", p1 == p2)
fmt.Printf("&p1 == &p2 : 等値 : %t\n", &p1 == &p2)
fmt.Printf("reflect.DeepEqual(p1, p2) : 等価 : %t\n", reflect.DeepEqual(p1, p2))
fmt.Printf("reflect.DeepEqual(&p1, &p2) : %t\n", reflect.DeepEqual(&p1, &p2))
fmt.Println(bar)
fmt.Println("単純な構造体の比較2")
p3 := p1
fmt.Printf("p1 == p3 : 等価(メモリが別でも値が一緒だったらOK) : %t\n", p1 == p3)
fmt.Printf("&p1 == &p3 : 等値 : %t\n", &p1 == &p3)
fmt.Printf("reflect.DeepEqual(p1, p3) : 等価 : %t\n", reflect.DeepEqual(p1, p3))
fmt.Printf("reflect.DeepEqual(&p1, &p3) : %t\n", reflect.DeepEqual(&p1, &p3))
===========================================================
単純な構造体の比較
p1 == p2 : 等価(メモリが別でも値が一緒だったらOK) : true
&p1 == &p2 : 等値 : false
reflect.DeepEqual(p1, p2) : 等価 : true
reflect.DeepEqual(&p1, &p2) : true
===========================================================
単純な構造体の比較2
p1 == p3 : 等価(メモリが別でも値が一緒だったらOK) : true
&p1 == &p3 : 等値 : false
reflect.DeepEqual(p1, p3) : 等価 : true
reflect.DeepEqual(&p1, &p3) : true
===========================================================
fmt.Println(bar)
fmt.Println("構造体のポインタ型の比較(同一ポインタ)")
p1 := Person{
Name: "太郎",
Age: 20,
}
p2 := Person{
Name: "太郎",
Age: 20,
}
p3 := &p1
fmt.Printf("&p1 == p3 : 等値 : %t\n", &p1 == p3)
fmt.Printf("reflect.DeepEqual(&p1, p3) : 等価 : %t\n", reflect.DeepEqual(&p1, p3))
fmt.Println(bar)
fmt.Println("構造体のポインタ型の比較(別ポインタ)")
p4 := &p2
fmt.Printf("&p1 == p5 : 等値 : %t\n", &p1 == p4)
fmt.Printf("reflect.DeepEqual(&p1, p4) : 等価 : %t\n", reflect.DeepEqual(&p1, p4))
===========================================================
構造体のポインタ型の比較(同一ポインタ)
&p1 == p3 : 等値 : true
reflect.DeepEqual(&p1, p3) : 等価 : true
===========================================================
構造体のポインタ型の比較(別ポインタ)
&p1 == p5 : 等値 : false
reflect.DeepEqual(&p1, p4) : 等価 : true
===========================================================
fmt.Println(bar)
sa := []int{0, 1, 2, 3, 4, 5}
sb := []int{0, 1, 2, 3, 4, 5}
s1 := StructIncludingSlice{
Numbers: sa,
}
s2 := StructIncludingSlice{
Numbers: sb,
}
fmt.Println("フィールドにSliceが入っている構造体の比較")
// fmt.Printf("等価(メモリが別でも値が一緒だったらOK) : %t\n", s1 == s2) // struct containing []int cannot be compared
fmt.Println("s1 == s2 : 等価(エラー) : struct containing []int cannot be compared")
fmt.Printf("&s1 == &s2 : 等値 : %t\n", &s1 == &s2)
fmt.Printf("reflect.DeepEqual(s1, s2) : 等価 : %t\n", reflect.DeepEqual(s1, s2))
fmt.Printf("reflect.DeepEqual(&s1, &s2) : %t\n", reflect.DeepEqual(&s1, &s2))
fmt.Println(bar)
fmt.Println("フィールドにSliceが入っている構造体の比較(Sliceが同じ物)")
s3 := StructIncludingSlice{
Numbers: sa,
}
fmt.Println("s1 == s3 : 等価(エラー) : struct containing []int cannot be compared")
fmt.Printf("&s1 == &s3 : 等値 : %t\n", &s1 == &s3)
fmt.Printf("reflect.DeepEqual(s1, s3) : 等価 : %t\n", reflect.DeepEqual(s1, s3))
fmt.Printf("reflect.DeepEqual(&s1, &s3) : %t\n", reflect.DeepEqual(&s1, &s3))
fmt.Println(bar)
fmt.Println("フィールドにSliceが入っている構造体の比較(同じアドレスの場合)")
s4 := &s1
fmt.Println("s1 == s4 : 等価(エラー) : struct containing []int cannot be compared")
fmt.Printf("&s1 == s4 : 等値 : %t\n", &s1 == s4)
fmt.Printf("reflect.DeepEqual(s1, s4) : 等価 : %t\n", reflect.DeepEqual(s1, s4))
fmt.Printf("reflect.DeepEqual(&s1, s4) : %t\n", reflect.DeepEqual(&s1, s4))
===========================================================
フィールドにSliceが入っている構造体の比較
s1 == s2 : 等価(エラー) : struct containing []int cannot be compared
&s1 == &s2 : 等値 : false
reflect.DeepEqual(s1, s2) : 等価 : true
reflect.DeepEqual(&s1, &s2) : true
===========================================================
フィールドにSliceが入っている構造体の比較(Sliceが同じ物)
s1 == s3 : 等価(エラー) : struct containing []int cannot be compared
&s1 == &s3 : 等値 : false
reflect.DeepEqual(s1, s3) : 等価 : true
reflect.DeepEqual(&s1, &s3) : true
===========================================================
フィールドにSliceが入っている構造体の比較(同じアドレスの場合)
s1 == s4 : 等価(エラー) : struct containing []int cannot be compared
&s1 == s4 : 等値 : true
reflect.DeepEqual(s1, s4) : 等価 : false
reflect.DeepEqual(&s1, s4) : true
===========================================================
fmt.Println("StructIncludingPointerの実験")
num1 := 1
num2 := 1
s1 := StructIncludingPointer{
Pointer: &num1,
}
s2 := StructIncludingPointer{
Pointer: &num2,
}
// プロパティにポインタ(スライス含む)が入っていると動作
fmt.Println("フィールドに異なるアドレスを指す同じ数字が入っている構造体の比較")
fmt.Printf("s1 == s2 : この場合、構造体のフィールドの等値での比較になる : %t\n", s1 == s2)
fmt.Printf("&s1 == &s2 : %t\n", &s1 == &s2)
fmt.Printf("reflect.DeepEqual(s1, s2) : 等価 : %t\n", reflect.DeepEqual(s1, s2))
fmt.Printf("reflect.DeepEqual(&s1, &s2) : %t\n", reflect.DeepEqual(&s1, &s2))
===========================================================
StructIncludingPointerの実験
フィールドに異なるアドレスを指す同じ数字が入っている構造体の比較
s1 == s2 : この場合、構造体のフィールドの等値での比較になる : false
&s1 == &s2 : false
reflect.DeepEqual(s1, s2) : 等価 : true
reflect.DeepEqual(&s1, &s2) : true
===========================================================
fmt.Println(bar)
numX := 2
s3 := StructIncludingPointer{
Pointer: &numX,
}
s4 := StructIncludingPointer{
Pointer: &numX,
}
fmt.Println("フィールドに同一アドレスを指す同じ数字が入っている構造体の比較")
fmt.Printf("s3 == s4 : 構造体のフィールドの等値での比較になる : %t\n", s3 == s4)
fmt.Printf("&s3 == &s4 : %t\n", &s3 == &s4)
fmt.Printf("reflect.DeepEqual(s3, s4) : 等価 : %t\n", reflect.DeepEqual(s3, s4))
fmt.Printf("reflect.DeepEqual(&s3, &s4) : %t\n", reflect.DeepEqual(&s3, &s4))
fmt.Println(bar)
===========================================================
フィールドに同一アドレスを指す同じ数字が入っている構造体の比較
s3 == s4 : 構造体のフィールドの等値での比較になる : true
&s3 == &s4 : false
reflect.DeepEqual(s3, s4) : 等価 : true
reflect.DeepEqual(&s3, &s4) : true
===========================================================
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment