Wrap net.IP in a custom type and add methodes in golang

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

Wrap net.IP in a custom type and add methodes in golang

问题

我尝试为net.IP添加方法。因此,我创建了一个自定义类型IPAddr:

package main

import (
	"encoding/json"
	"net"
	"log"
)

func readNetworks(data []byte) (*[]Network, error) {
	var networks []Network

	if err := json.Unmarshal(data, &networks); err != nil {
		return &networks, err
	}

	return &networks, nil
}

type IPAddr net.IP

type Network struct {
	CIDR    string `json:"cidr"`
	Gateway IPAddr `json:"gateway"`
}

func (ip *IPAddr) copy() IPAddr {
	if x := ip.To4(); x != nil {
		ip = x
	}
	dup := make(IPAddr, len(ip))
	copy(dup, ip)
	return dup
}

func main() {
	_, err := readNetworks([]byte(`[{"cidr":"10.100.19.0/24","gateway":"10.100.19.1"}]`))
	if err != nil {
		log.Fatal(err)
	}
}

这里有一些不起作用的地方:

  • IPAddr类型不知道net.IP的To4()方法(第27行)
  • IPAddr类型不能使用len()方法,但net.IP类型可以(第30行)
  • 无法将字符串解组为IPAddr类型,但可以解组为net.IP类型(第12行)

我的类型定义显然是错误的...有什么提示吗?

请参考示例:
http://play.golang.org/p/BT7VFnbPZW

英文:

I try to add methodes to net.IP. Therefore I created a custom type IPAddr:

package main

import (
	"encoding/json"
	"net"
	"log"
)

func readNetworks(data []byte) (*[]Network, error) {
	var networks []Network

	if err := json.Unmarshal(data, &networks); err != nil {
		return &networks, err
	}

	return &networks, nil
}

type IPAddr net.IP

type Network struct {
	CIDR          string        `json:"cidr"`
	Gateway       IPAddr        `json:"gateway"`
}

func (ip *IPAddr) copy() IPAddr {
	if x := ip.To4(); x != nil {
		ip = x
	}
	dup := make(IPAddr, len(ip))
	copy(dup, ip)
	return dup
}

func main() {
	_, err := readNetworks([]byte("[{\"cidr\":\"10.100.19.0/24\",\"gateway\":\"10.100.19.1\"}]"))
	if err != nil {
		log.Fatal(err)
	}
}

Some things that do not work here:

  • Method To4() from net.IP is not known for IPAddr (line 27)
  • len() does not work for IPAddr but for net.IP (line 30)
  • string cannot be unmarshalled to IPAddr, this works for net.IP (line 12)

My type definition is obviously wrong... any hints?

See example at:
http://play.golang.org/p/BT7VFnbPZW

答案1

得分: 6

方法在类型之间不会被继承(Go语言根本没有继承)。

如果你想自动提升方法,你可以考虑嵌入该类型:

type IPAddr struct {
    net.IP
}
英文:

Methods are not inherited between types (Go does not have inheritance at all).

You may want to embed the type, if promoting the methods automatically is what you're after:

type IPAddr struct {
    net.IP
}

答案2

得分: 3

在调用方法之前,将其转换为net.IP,否则你将在包装器上调用方法:

func (ip *IPAddr) copy() IPAddr {
    if x := net.IP(*ip).To4(); x != nil {
        ip = x
    }
    dup := make(IPAddr, len(net.IP(*ip)))
    copy(dup, *ip)
    return dup
}

对于解组,实现json.Unmarshaler接口。

另一种方法是通过在结构体中嵌入net.IP,尽管这将继承net.IP的所有方法。

英文:

Convert to net.IP before trying to call methods, otherwise you'll call methods on the wrapper:

func (ip *IPAddr) copy() IPAddr {
    if x := net.IP(*ip).To4(); x != nil {
        ip = x
    }
    dup := make(IPAddr, len(net.IP(*ip)))
    copy(dup, *ip)
    return dup
}

For unmarshaling, implement the json.Unmarshaler interface.

Another way to do it is by embedding net.IP in a struct, although this will inherit all methods from net.IP.

huangapple
  • 本文由 发表于 2015年4月20日 23:05:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/29751577.html
匿名

发表评论

匿名网友

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

确定