~/snippets/go-cidr-overlap
Published on

Go CIDR Overlap

195 words1 min read
package main

import (
	"fmt"
	"net"
	"os"
)

func main() {
	cidr1 := "27.53.68.54/32"
	cidr2 := "27.53.68.54/24"

	_, n1, err := net.ParseCIDR(cidr1)
	if err != nil {
		fmt.Println(err)
		os.Exit(1)
	}

	_, n2, err := net.ParseCIDR(cidr2)
	if err != nil {
		fmt.Println(err)
		os.Exit(1)
	}

	fmt.Println(n1, n1.Contains(n2.IP))
	fmt.Println(n2, n2.Contains(n1.IP))

}