mirror of
https://github.com/golang/go
synced 2024-11-18 10:04:43 -07:00
net: add examples for ParseIP, IP.DefaultMask & IP.Mask
Further examples to support the net package. Updates #5757 Change-Id: I9b65521d211f6c404b9103c1eaf22b0772eb242e Reviewed-on: https://go-review.googlesource.com/43711 Reviewed-by: Mikio Hara <mikioh.mikioh@gmail.com>
This commit is contained in:
parent
2d20ded584
commit
4bf6c566d3
@ -65,6 +65,41 @@ func ExampleParseCIDR() {
|
||||
// 2001:db8::/32
|
||||
}
|
||||
|
||||
func ExampleParseIP() {
|
||||
fmt.Println(net.ParseIP("192.0.2.1"))
|
||||
fmt.Println(net.ParseIP("2001:db8::68"))
|
||||
fmt.Println(net.ParseIP("192.0.2"))
|
||||
|
||||
// Output:
|
||||
// 192.0.2.1
|
||||
// 2001:db8::68
|
||||
// <nil>
|
||||
}
|
||||
|
||||
func ExampleIP_DefaultMask() {
|
||||
ip := net.ParseIP("192.0.2.1")
|
||||
fmt.Println(ip.DefaultMask())
|
||||
|
||||
// Output:
|
||||
// ffffff00
|
||||
}
|
||||
|
||||
func ExampleIP_Mask() {
|
||||
ipv4Addr := net.ParseIP("192.0.2.1")
|
||||
// This mask corresponds to a /24 subnet for IPv4.
|
||||
ipv4Mask := net.CIDRMask(24, 32)
|
||||
fmt.Println(ipv4Addr.Mask(ipv4Mask))
|
||||
|
||||
ipv6Addr := net.ParseIP("2001:db8:a0b:12f0::1")
|
||||
// This mask corresponds to a /32 subnet for IPv6.
|
||||
ipv6Mask := net.CIDRMask(32, 128)
|
||||
fmt.Println(ipv6Addr.Mask(ipv6Mask))
|
||||
|
||||
// Output:
|
||||
// 192.0.2.0
|
||||
// 2001:db8::
|
||||
}
|
||||
|
||||
func ExampleCIDRMask() {
|
||||
// This mask corresponds to a /31 subnet for IPv4.
|
||||
fmt.Println(net.CIDRMask(31, 32))
|
||||
|
Loading…
Reference in New Issue
Block a user