1
0
mirror of https://github.com/golang/go synced 2024-11-23 07:20:06 -07:00

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/42912
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
This commit is contained in:
Rob Phoenix 2017-05-08 22:08:42 +01:00 committed by Brad Fitzpatrick
parent bcf2d74ce8
commit 716761b8b1

View File

@ -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
}