From 716761b8b13926ef4a82dcb4ffc324066779239c Mon Sep 17 00:00:00 2001 From: Rob Phoenix Date: Mon, 8 May 2017 22:08:42 +0100 Subject: [PATCH] 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 Run-TryBot: Brad Fitzpatrick TryBot-Result: Gobot Gobot --- src/net/example_test.go | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/src/net/example_test.go b/src/net/example_test.go index 9dd17324545..bfab21c589f 100644 --- a/src/net/example_test.go +++ b/src/net/example_test.go @@ -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 +}