...

Source file src/go4.org/netipx/example_test.go

Documentation: go4.org/netipx

     1  // Copyright 2020 The Inet.Af AUTHORS. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package netipx_test
     6  
     7  import (
     8  	"fmt"
     9  	"net/netip"
    10  
    11  	"go4.org/netipx"
    12  )
    13  
    14  func ExampleIPSet() {
    15  	var b netipx.IPSetBuilder
    16  
    17  	b.AddPrefix(netip.MustParsePrefix("10.0.0.0/8"))
    18  	b.RemovePrefix(netip.MustParsePrefix("10.0.0.0/16"))
    19  
    20  	b.AddRange(netipx.IPRangeFrom(
    21  		netip.MustParseAddr("fed0::0400"),
    22  		netip.MustParseAddr("fed0::04ff"),
    23  	))
    24  
    25  	s, _ := b.IPSet()
    26  
    27  	fmt.Println("Ranges:")
    28  	for _, r := range s.Ranges() {
    29  		fmt.Printf("  %s - %s\n", r.From(), r.To())
    30  	}
    31  
    32  	fmt.Println("Prefixes:")
    33  	for _, p := range s.Prefixes() {
    34  		fmt.Printf("  %s\n", p)
    35  	}
    36  	// Output:
    37  	// Ranges:
    38  	//   10.1.0.0 - 10.255.255.255
    39  	//   fed0::400 - fed0::4ff
    40  	// Prefixes:
    41  	//   10.1.0.0/16
    42  	//   10.2.0.0/15
    43  	//   10.4.0.0/14
    44  	//   10.8.0.0/13
    45  	//   10.16.0.0/12
    46  	//   10.32.0.0/11
    47  	//   10.64.0.0/10
    48  	//   10.128.0.0/9
    49  	//   fed0::400/120
    50  }
    51  

View as plain text