Created
September 28, 2022 05:50
-
-
Save xin053/c6b99466db9394601c02baaaf0c33b10 to your computer and use it in GitHub Desktop.
[go net]
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 获取 cidr 所有 ip | |
package main | |
import ( | |
"net/netip" | |
) | |
func Hosts(cidr string) ([]netip.Addr, error) { | |
prefix, err := netip.ParsePrefix(cidr) | |
if err != nil { | |
panic(err) | |
} | |
var ips []netip.Addr | |
for addr := prefix.Addr(); prefix.Contains(addr); addr = addr.Next() { | |
ips = append(ips, addr) | |
} | |
if len(ips) < 2 { | |
return ips, nil | |
} | |
return ips[1 : len(ips)-1], nil | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment