Commit 716761b8 authored by Rob Phoenix's avatar Rob Phoenix Committed by Brad Fitzpatrick

net: add examples for IPv4, ParseCIDR & IPv4Mask

Further examples to support the net package.

See issue #5757

Change-Id: I839fd97a468c8d9195e8f4a0ee886ba50ca3f382
Reviewed-on: https://go-review.googlesource.com/42912Reviewed-by: default avatarBrad Fitzpatrick <bradfitz@golang.org>
Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
parent bcf2d74c
......@@ -36,6 +36,35 @@ func ExampleListener() {
}
}
func ExampleIPv4() {
fmt.Println(net.IPv4(8, 8, 8, 8))
// Output:
// 8.8.8.8
}
func ExampleParseCIDR() {
ipv4Addr, ipv4Net, err := net.ParseCIDR("192.0.2.1/16")
if err != nil {
log.Fatal(err)
}
fmt.Println(ipv4Addr)
fmt.Println(ipv4Net)
ipv6Addr, ipv6Net, err := net.ParseCIDR("2001:db8:a0b:12f0::1/32")
if err != nil {
log.Fatal(err)
}
fmt.Println(ipv6Addr)
fmt.Println(ipv6Net)
// Output:
// 192.0.2.1
// 192.0.0.0/16
// 2001:db8:a0b:12f0::1
// 2001:db8::/32
}
func ExampleCIDRMask() {
// This mask corresponds to a /31 subnet for IPv4.
fmt.Println(net.CIDRMask(31, 32))
......@@ -47,3 +76,10 @@ func ExampleCIDRMask() {
// fffffffe
// ffffffffffffffff0000000000000000
}
func ExampleIPv4Mask() {
fmt.Println(net.IPv4Mask(255, 255, 255, 0))
// Output:
// ffffff00
}
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