Search code examples
gomemory-managementstructinstances

Return a new Struct instance in Golang


My goal is to return a new DDIAddress from EndRangeTest(); however, when I try to do this, subsequent calls to EndRangeTest seem to modify the same struct instance instead of creating a new object.

For instance, when I run the code below, I expect tRange to be 127.0.0.255 and sRange equal to 255.0.0.20. But what actually happens is that s.EndRangeTest() modifies tRange.

t := new(DDIAddress)
s := new(DDIAddress)

t.FromString("127.0.0.1")
t.cidr = 24

s.FromString("255.0.0.20")
s.cidr = 32

tRange := t.EndRangeTest()
fmt.Printf("T Result:%s\n", tRange.String())

sRange := s.EndRangeTest()

fmt.Printf("S Result:%s\n", sRange.String())
fmt.Printf("T Result:%s\n\n\n", tRange.String())

Output:

T Result:127.0.0.255
S Result:255.0.0.20
T Result:255.0.0.20

I am new to Go, and don't understand what I am doing wrong here.

My DDIAddress struct is implemented as followed:

type DDIAddress struct {
    net.IP
    cidr uint32
}

func (addr *DDIAddress) EndRangeTest() (DDIAddress) {
   var maskSize int
   var start int

   endAddr := DDIAddress{}

   if addr.isIPv4() == false {
      maskSize = 16
      start = 0
      endAddr.IP = net.IPv6zero
   }else{
      maskSize = 4
      start = 12
      endAddr.IP = net.IPv4zero
   }

   mask := net.CIDRMask(int(addr.cidr), 8*maskSize)

   for i :=0; i < maskSize; i++{
      endAddr.IP[start] = addr.IP[start] | (mask[i] ^ 0xff)
      start++
   }

   return endAddr
}

Thanks!


Solution

  • The values created by EndRangeTest share the backing arrays of net.IPv6zero or net.IPv4zero. A change to the backing array of one DDIAddress changes the backing array of other DDIAddress values of the same size.

    To fix the problem, allocate a new slice:

       if addr.isIPv4() == false {
          maskSize = 16
          start = 0
          endAddr.IP = make(net.IP, net.IPv6len)
       } else {
          maskSize = 4
          start = 12
          endAddr.IP = make(net.IP, net.IPv4len)
       }