sontags
sontags

Reputation: 3241

Copy net.IP in Golang

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)
}

Upvotes: 3

Views: 5450

Answers (1)

Dave C
Dave C

Reputation: 7878

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

Upvotes: 6

Related Questions