I'm experiencing unexpected behavior in a unit test which uses reflect.DeepEqual to test for the equality of two parsed structures. The structures contain a Slice of net.IPNet
structs. The tests fail because reflect.DeepEqual
returns false.
While tracking this issue down I found the following unexpected behavior of DeepEqual. The most fascinating thing for me is that it works with IPv6 addresses and using the war parsed IP, not the masked one in the net.IPNet
struct.
Can anyone explain to me:
- Why are those IPv4 addresses not DeepEqual, despite their byte-representation seems to be?
- Why are they DeepEqual for IPv6 addresses?
- How to construct an net.IP instance that match the one generated by ParseCIDR with respect to DeepEqual?
Example program, runable with go run
:
package main
import (
"fmt"
"net"
"reflect"
)
func main() {
aip, a, _ := net.ParseCIDR("135.104.0.0/32")
//aip, a, _ := net.ParseCIDR("abcd:2345::/65")
bip := net.IPv4(135, 104, 0, 0)
//bip := net.ParseIP("abcd:2345::")
// IPa: 135.104.0.0 3133352e3130342e302e30
fmt.Printf("IPa: %s %x
", a.IP, a.IP)
// IPb: 135.104.0.0 3133352e3130342e302e30
fmt.Printf("IPb: %s %x
", bip, bip)
fmt.Println("eq?:", a.IP.Equal(bip)) // true
// I'd expect this to be true
fmt.Println("deep eq?:", reflect.DeepEqual(a.IP, bip)) // false
fmt.Println("deep eq w/o mask?:", reflect.DeepEqual(aip, bip)) // true
}