title: How can I get "index out of range" when I am not indexing
body: <p>Long time programmer - total newbie in go.</p>
  
  <p>Here's the code. It is my first attempt at a generator. I am trying to generate an lfsr sequence. Essentially, every time around you shift right one. If you just shifted out a <code>1</code> bit, xor with the <code>tap</code> value.</p>
  
  <pre><code>package main
  
  import (
      "fmt"
      "math/big"
  )
  
  // lfsr returns an lfsr generator.
  func lfsr(tap, start big.Int) func() big.Int {
      // Keep track of next.
      next := &amp;start
      // The generator.
      return func() big.Int {
          // Remember where we are.
          ret := *next
          // Work out next.
          fmt.Println("next", next.String(), "bit(0)", next.Bit(0))
          // Is it currently odd?
          odd := next.Bit(0)
          // Shift right one.
          next = next.Rsh(next, 1)
  
          // If odd - tap!
          if odd != 0 {
              // Tap!
              next = next.Xor(next, &amp;tap)
              fmt.Printf("Tap!", next.String())
          }
  
          // Return where we were.
          return ret
      }
  }
  
  func main() {
      ten := new(big.Int)
      ten.SetString("10", 32)
      f := lfsr(*ten, *ten)
      for i := 0; i &lt; 10; i++ {
          n := f()
          fmt.Println("lfsr ", n.String())
      }
  }
  </code></pre>
  
  <p>The printout I am getting is:</p>
  
  <pre><code>next 32 bit(0) 0
  lfsr  16
  next 16 bit(0) 0
  lfsr  8
  next 8 bit(0) 0
  lfsr  4
  next 4 bit(0) 0
  lfsr  2
  next 2 bit(0) 0
  lfsr  1
  next 1 bit(0) 1
  Tap! 0
  panic: runtime error: index out of range
  </code></pre>
  
  <p>What am I doing wrong - and why does it look right?</p>
  
  <p><a href="http://play.golang.org/p/QzUuc2Ad-X" rel="nofollow">Play</a> - Interestingly - it outputs:</p>
  
  <pre><code>...
  next 1 bit(0) 1
  Tap!%!(EXTRA string=0)panic: runtime error: index out of range
  
  goroutine 1 [running]:
  math/big.nat.string(0xc010045150, 0x1, 0x5, 0x12b23d0, 0xa, ...)
      go/src/pkg/math/big/nat.go:819 +0x67f
  math/big.nat.decimalString(0xc010045150, 0x1, 0x5, 0x1, 0x1, ...)
      go/src/pkg/math/big/nat.go:731 +0x8f
  math/big.(*Int).String(0x7f851e0eff40, 0xc010045150, 0x1)
      go/src/pkg/math/big/int.go:331 +0xfe
  main.main()
      /tmpfs/gosandbox-94dce1ec_430947f1_6360662e_01c3d6ad_a7071d20/prog.go:41 +0x120
  </code></pre>
  
  <p>which is a) slightly different and b) suggesting that the <code>fmt.Println("lfsr ", n.String())</code> is what is failing but I am no nearer working out why.</p>
  
  <p><strong>Added</strong></p>
  
  <p>After experimentation (changing <code>ten.SetString("10", 32)</code> to <code>ten.SetString("10", 10)</code>) I now get:</p>
  
  <pre><code>lfsr  5
  next 5 bit(0) 1
  Tap!%!(EXTRA string=0)panic: runtime error: index out of range
  </code></pre>
  
  <p>Going to sleep now - hope someone can help.</p>