/* net.IP, net.IPMask and net.IPNet are named types with METHODS, and the address predicates hold through them. `type IP []byte` carries the method set (`String`, `To4`, `To16`, `Equal`, `IsUnspecified`, `IsLoopback`, `IsPrivate`, `IsMulticast`, `IsInterfaceLocalMulticast`, `IsLinkLocalMulticast`, `IsLinkLocalUnicast`, `IsGlobalUnicast`, `DefaultMask`, `ApplyMask`, `AppendText`, `MarshalText`, `UnmarshalText`); `IPMask` carries `Size` and `String`; `IPNet` carries `Contains`, `Network` and `String`. This fixture used to call free functions (`net.IPString`, `net.IPEqual`, ...) from the intermediate alias form; those are gone, and a call to one is exactly what this file exists to catch. The fixture is end-to-end on purpose: the compiler checks the MAIN package's bodies thoroughly but tolerates an unresolved call in a dependency's body, so a dependency-only check would not prove the methods exist. This calls them and checks the values, including the 4-byte/16-byte form equivalence that Equal normalizes and IPNet.Contains' use of the mask. */ package main import ( "net" ) func main() { // Termination: a batch program must still be stoppable, so the entry // handles the channel the runtime closes on SIGINT/SIGTERM. select { case <-shutdown: return default: } ip := net.ParseIP("192.168.1.5") if ip == nil { panic("parse") } if ip.String() != "192.168.1.5" { panic("string: " | ip.String()) } if ip.IsUnspecified() { panic("unspecified") } if !ip.IsPrivate() { panic("private") } if ip.IsLoopback() { panic("loopback") } ip4 := ip.To4() if ip4 == nil || len(ip4) != 4 { panic("to4") } ip16 := ip.To16() if ip16 == nil || len(ip16) != 16 { panic("to16") } if !ip.Equal(ip16) { panic("equal across 4 and 16 byte forms") } if !net.ParseIP("127.0.0.1").IsLoopback() { panic("loopback 127.0.0.1") } m := net.CIDRMask(24, 32) ones, bits := m.Size() if ones != 24 || bits != 32 { panic("mask size") } n := &net.IPNet{IP: ip, Mask: m} if !n.Contains(net.ParseIP("192.168.1.99")) { panic("contains in-net") } if n.Contains(net.ParseIP("10.0.0.1")) { panic("contains out-of-net") } println("ip ok") }