英文:
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 (
"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("Error", ipList[i], err)
continue
}
fmt.Println(ipList[i], "-> ip:", ip, " net:", 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 (
"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)
}
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}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论