Commit 4bf6c566 authored by Rob Phoenix's avatar Rob Phoenix Committed by Mikio Hara

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/43711Reviewed-by: default avatarMikio Hara <mikioh.mikioh@gmail.com>
parent 2d20ded5
......@@ -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))
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment