title: Golang Net.IP to IPv6 (from MySQL) as Decimal(39,0) Conversion?
body: <p>I have a database that stores IPv4 and IPv6 addresses as decimal(39,0). I need to convert a Golang Net.IP to this format. I have done it for IPv4 as follows:</p>
  
  <pre><code>func ipv4ToInt(IPv4Addr net.IP) int64 {
      bits := strings.Split(IPv4Addr.String(), ".")
  
      b0, _ := strconv.Atoi(bits[0])
      b1, _ := strconv.Atoi(bits[1])
      b2, _ := strconv.Atoi(bits[2])
      b3, _ := strconv.Atoi(bits[3])
  
      var sum int64
  
      sum += int64(b0) &lt;&lt; 24
      sum += int64(b1) &lt;&lt; 16
      sum += int64(b2) &lt;&lt; 8
      sum += int64(b3)
  
  return sum
  }
  </code></pre>
  
  <p>I am trying the same with IPv6:</p>
  
  <pre><code>func ipv6ToInt(IPv6Addr net.IP) Int128 {
      bits := strings.Split(IPv6Addr.String(), ":")
      var arr [4]int64
      var arr1 [4]uint64
  
      for i := 0; i &lt; 4; i++ {
          arr[i], _ = strconv.ParseInt(bits[i], 16, 64)
  
      }
  
      for i := 0; i &lt; 4; i++ {
      arr1[i], _ = strconv.ParseUint(bits[i], 16, 64)
      }
  
      int1 := arr[0]
  
      for i := 0; i &lt; 4; i++ {
          int1 = (int1 &lt;&lt; 16) + arr[i]
      }
  
      int2 := arr1[0]
      for i := 0; i &lt; 4; i++ {
          int2 = (int2 &lt;&lt; 16) + arr1[i]
      }
  
      var IPv6Int Int128
      IPv6Int.H = int1
      IPv6Int.L = int2
      return IPv6Int
  }
  </code></pre>
  
  <p>Where int128 is </p>
  
  <pre><code>type Int128 struct {
      H int64
      L uint64
  }
  </code></pre>
  
  <p>The result should look like:</p>
  
  <pre><code>42540578165168461141553663388954918914
  </code></pre>
  
  <p>from the IPv6 addr: </p>
  
  <pre><code>2001:470:0:76::2
  </code></pre>
  
  <p>Thanks!</p>
  
  <p>EDIT, ANSWER:
  Thanks to the people in #go-nuts, the answer is as follows:</p>
  
  <pre><code>func ipv6ToInt(IPv6Addr net.IP) *big.Int {
      IPv6Int := big.NewInt(0)
      IPv6Int.SetBytes(IPv6Addr)
      return IPv6Int
  } 
  </code></pre>
  
  <p>The same works for IPv6, just do IP.To4() first. </p>