golang cast a string to net.IPNet type

huangapple go评论103阅读模式
英文:

golang cast a string to net.IPNet type

问题

我可以帮你翻译这段内容。以下是翻译结果:

我有一组以CIDR表示的字符串片段。它们既是IPv4又是IPv6的,我需要将它们转换为net.IPNet类型。

在Go语言中,我该如何做到这一点?

示例字符串:

192.168.1.1/24
fd04:3e42:4a4e:3381::/64

英文:

I have a slice of strings that are in CIDR notation. They are both ipv4 and ipv6 and I need them cast into the type net.IPNet.

How would I do this in golang?

example strings:

192.168.1.1/24<br>
fd04:3e42:4a4e:3381::/64

答案1

得分: 7

根据cnicutar的建议,可以使用net.ParseCIDR函数来解析CIDR地址。以下是一个实际使用该函数的工作示例:

package main

import (
	"fmt"
	"net"
)

func main() {
	ipList := []string{"192.168.1.1/24", "fd04:3e42:4a4e:3381::/64"}
	for i := 0; i < len(ipList); i += 1 {
		ip, ipnet, err := net.ParseCIDR(ipList[i])
		if err != nil {
			fmt.Println("错误", ipList[i], err)
			continue
		}
		fmt.Println(ipList[i], "-> ip:", ip, " 网络:", ipnet)
	}
}

你可以在这个链接中查看这个示例的运行结果:http://play.golang.org/p/Wtqy56LS2Y

英文:

As cnicutar says use net.ParseCIDR.

This is a working example on how to actually use it.

http://play.golang.org/p/Wtqy56LS2Y

package main

import (
	&quot;fmt&quot;
	&quot;net&quot;
)

func main() {
	ipList := []string{&quot;192.168.1.1/24&quot;, &quot;fd04:3e42:4a4e:3381::/64&quot;}
	for i := 0; i &lt; len(ipList); i += 1 {
		ip, ipnet, err := net.ParseCIDR(ipList[i])
		if err != nil {
			fmt.Println(&quot;Error&quot;, ipList[i], err)
			continue
		}
		fmt.Println(ipList[i], &quot;-&gt; ip:&quot;, ip, &quot; net:&quot;, ipnet)
	}
}

答案2

得分: 5

我不认为你想要进行类型转换;相反,我认为你想要使用ParseCIDR函数。

func ParseCIDR(s string) (IP, *IPNet, error)
英文:

I don't think you want casting; instead I think you want ParseCIDR

func ParseCIDR(s string) (IP, *IPNet, error)

答案3

得分: 0

这是使用IPAddress Go库来完成的方法。CIDR子网、IP地址和掩码在IPAddress库中都使用相同的类型ipaddr.IPAddress,使得代码更简洁。免责声明:我是项目经理。

package main

import (
	"fmt"
	"github.com/seancfoley/ipaddress-go/ipaddr"
	"net"
)

func main() {
	parseAddr("192.168.1.1/24")
	parseAddr("fd04:3e42:4a4e:3381::/64")
}

func parseAddr(cidrStr string) {
	cidr := ipaddr.NewIPAddressString(cidrStr)
	subnet, addr := cidr.GetAddress().ToPrefixBlock(), cidr.GetHostAddress()
	var ipNet = net.IPNet{
		IP:   subnet.GetNetIP(),
		Mask: subnet.GetNetworkMask().Bytes(),
	}
	fmt.Printf("\nsubnet: %s\naddress: %s\nIPNet: %+v\n", subnet, addr, ipNet)
}

输出结果:

subnet: 192.168.1.0/24
address: 192.168.1.1
IPNet: {IP:192.168.1.0 Mask:ffffff00}

subnet: fd04:3e42:4a4e:3381::/64
address: fd04:3e42:4a4e:3381::
IPNet: {IP:fd04:3e42:4a4e:3381:: Mask:ffffffffffffffff0000000000000000}
英文:

This is how to do it using the IPAddress Go library. CIDR subnets, IP addresses and masks all use the same type in the IPAddress library, ipaddr.IPAddress, making code simpler. Disclaimer: I am the project manager.

package main

import (
	&quot;fmt&quot;
	&quot;github.com/seancfoley/ipaddress-go/ipaddr&quot;
	&quot;net&quot;
)

func main() {
	parseAddr(&quot;192.168.1.1/24&quot;)
	parseAddr(&quot;fd04:3e42:4a4e:3381::/64&quot;)
}

func parseAddr(cidrStr string) {
	cidr := ipaddr.NewIPAddressString(cidrStr)
	subnet, addr := cidr.GetAddress().ToPrefixBlock(), cidr.GetHostAddress()
	var ipNet = net.IPNet{
		IP:   subnet.GetNetIP(),
		Mask: subnet.GetNetworkMask().Bytes(),
	}
	fmt.Printf(&quot;\nsubnet: %s\naddress: %s\nIPNet: %+v\n&quot;, subnet, addr, ipNet)
}

Output:

subnet: 192.168.1.0/24
address: 192.168.1.1
IPNet: {IP:192.168.1.0 Mask:ffffff00}

subnet: fd04:3e42:4a4e:3381::/64
address: fd04:3e42:4a4e:3381::
IPNet: {IP:fd04:3e42:4a4e:3381:: Mask:ffffffffffffffff0000000000000000}

huangapple
  • 本文由 发表于 2014年6月12日 01:34:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/24169152.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定