2015-04-19 15:53
浏览 120


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


What am I doing wrong?

CODE as requested:

package main

import (

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) {
        out = append(out, next) // and here (line 19)

    return out

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

func main() {
    r := Range{
        Start: net.ParseIP(""),
        End:   net.ParseIP(""),
    out := r.Expand()
  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

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.


    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, 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

    解决 无用
    打赏 举报

相关推荐 更多相似问题