gen.alcazar
gen.alcazar

Reputation: 13

Populating a map of structs with incrementing function

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\n", 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?

Upvotes: 1

Views: 240

Answers (1)

Marc
Marc

Reputation: 21035

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.

Upvotes: 2

Related Questions