dpk20361 2015-04-19 15:53
浏览 143
已采纳

在Golang中复制net.IP

I try to copy the value of a net.IP instance (line 14 and 19) but obviously just pass around the pointer:

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

What am I doing wrong?

CODE as requested:

package main

import (
    "log"
    "net"
)

type Range struct {
    Start net.IP `json:"start"`
    End   net.IP `json:"end"`
}

func (r Range) Expand() []net.IP {
    next := r.Start // here (line 14)
    out := []net.IP{next}

    for !next.Equal(r.End) {
        incIP(next)
        out = append(out, next) // and here (line 19)
    }

    return out
}

func incIP(ip net.IP) {
    for j := len(ip) - 1; j >= 0; j-- {
        ip[j]++
        if ip[j] > 0 {
            break
        }
    }
}

func main() {
    r := Range{
        Start: net.ParseIP("192.100.10.4"),
        End:   net.ParseIP("192.100.13.1"),
    }
    out := r.Expand()
    log.Print(r)
    log.Print(out)
}
  • 写回答

1条回答 默认 最新

  • doupi8598 2015-04-19 16:21
    关注

    Look at the documentation for net.IP; it's just a []byte so you could use the copy builtin.

    E.g.:

    func dupIP(ip net.IP) net.IP {
        dup := make(net.IP, len(ip))
        copy(dup, ip)
        return dup
    }
    

    If you care about space, you can also note from the documentation that although all the net routines support a net.IP of length 4 (e.g. ip := net.IP([]byte{1, 2, 3, 4}) will work) all the ones they generate are 16 bytes. Since you make a whole bunch, if you care about a few bytes of space, or if you care what happens after 255.255.255.255, you can avoid this for IPv4 like so:

    func dupIP(ip net.IP) net.IP {
        // To save space, try and only use 4 bytes
        if x := ip.To4(); x != nil {
            ip = x
        }
        dup := make(net.IP, len(ip))
        copy(dup, ip)
        return dup
    }
    

    You may want to do something different for IPv6 addresses, I don't think it makes sense to "increment" an IPv6 the way you do, I could be wrong though.

    Modified version of your original: https://play.golang.org/p/GP9vASvUgh

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥15 stable diffusion
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误