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 想问一下树莓派接上显示屏后出现如图所示画面,是什么问题导致的
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号