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 chaquopy python 安卓
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 有没有帮写代码做实验仿真的
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥30 vmware exsi重置后登不上
  • ¥15 易盾点选的cb参数怎么解啊
  • ¥15 MATLAB运行显示错误,如何解决?
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容
  • ¥15 关于#stm32#的问题:CANOpen的PDO同步传输问题