duanniwu7730 2018-01-06 11:29
浏览 52
已采纳

使用增量功能填充结构图

I am trying to populate a map with string keys and pointer to struct values. Specifically, having issues populating one field, that needs to be incremented, by calling a function, over the range of another array. Problem snippet is here.

package main

import (
  "fmt"
  "net"
)

type bgp struct {
  tag net.IP
}

type nbrStruct struct {
  bgp
}

var mIPStr = "10.10.10.0"

func main() {
  m := make(map[string]*nbrStruct)
  s := []string{"string1", "string2"}
  mIP := net.ParseIP(mIPStr)
  mIP = mIP.To4()

  for _, v := range s {
    m[v] = &nbrStruct{
       bgp: bgp{
         tag: mIP,
       },
    }
    fmt.Println(mIP)
    mIP, _ = NextIP(mIP, 4)
  }
  for key, value := range m {
    fmt.Printf("key: %v ---> tag: %v
", key, value.bgp.tag)
  }
}

func NextIP(ip net.IP, addIP int) (net.IP, error) {
  var err error
  if ip = ip.To4(); ip != nil {
        // Case IPv4
    ip[3] = ip[3] + uint8(addIP)
    if ip[3] > 255 {
    ip[3] = ip[3] - 255
    ip[2]++
  }
    if ip[2] > 255 {
        ip[2] = ip[2] - 255
        ip[1]++
    }
    if ip[1] > 255 {
        err = fmt.Errorf("NextIP out of space in top IPv4 Octet %v", ip)
        return nil, err
    }
    // } else if ip = ip.To16(); ip != nil {
    //  // Case IPv6
} else {
    err = fmt.Errorf("NextIP cannot understand IP: %v", ip)
    return nil, err
}
return ip, err
}

The issue I have is with line #31

mIP, _ = NextIP(mIP, 4)

I'm expecting the two values to be:

m["string1"].bgp.tag == 10.10.10.0
m["string2"].bgp.tag == 10.10.10.4

...which the function NextIP is correctly returning. But on reading the map, the previous m["string1"].bgp.tag value seems to be overwritten by the new one? They're both 10.10.10.8; super confused about this. What am I doing wrong?

  • 写回答

1条回答 默认 最新

  • duanqian2278 2018-01-06 11:43
    关注

    net.IP is a byte slice.

    You're basically modifying mIP constantly and storing it. But due to the way slices work, you're really storing a pointer to the underlying data.

    A quick workaround to your problem is to store a copy of mIP. eg:

    for _, v := range s {
        tmpIP := make([]byte, len(mIP))
        copy(tmpIP, mIP)
        m[v] = &nbrStruct{
            bgp: bgp{
                tag: tmpIP,
            },
        }
    
        mIP, _ = NextIP(mIP, 4)
    }
    

    This results in the correct output:

    key: string1 ---> tag: 10.10.10.0
    key: string2 ---> tag: 10.10.10.4
    

    I would strongly suggest you change NextIP to return a new IP, not a modified version of the input.

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

报告相同问题?

悬赏问题

  • ¥15 BP神经网络控制倒立摆
  • ¥20 要这个数学建模编程的代码 并且能完整允许出来结果 完整的过程和数据的结果
  • ¥15 html5+css和javascript有人可以帮吗?图片要怎么插入代码里面啊
  • ¥30 Unity接入微信SDK 无法开启摄像头
  • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算