title: Maximum length of a slice in Go
body: <p>I have the following code running in a 64-bit linux OS in a 4Gb machine:</p>
  
  <pre><code>package main
  
  import (
      "fmt"
      "math"
  )
  
  func main() {
      r := make([]bool, math.MaxInt32)
  
      fmt.Println("Size: ", len(r))
  }
  </code></pre>
  
  <p>When I run this I get:</p>
  
  <pre><code>Size: 2147483647
  </code></pre>
  
  <p>If I change the <code>math.MaxInt32</code> for <code>math.MaxUint32</code> I get:</p>
  
  <pre><code>fatal error: runtime: out of memory
  </code></pre>
  
  <p>With slice size of <code>math.MaxUint32</code> I ran out of memory, I was expecting that, but when I try using <code>math.MaxInt64</code> I get:</p>
  
  <pre><code>panic: runtime error: makeslice: len out of range
  </code></pre>
  
  <p>So aparently I cannot create a slice with a size of <code>math.MaxInt64</code>, which bring us to my question: If the memory is not an issue, what's the biggest slice I cant create in Go?</p>
  
  <p>I remember that, in Java, raw array indexes are managed with the type <code>int</code>, so the maximum size of a raw array is the max value of an <code>int</code>, if you try to do it with <code>long</code> it will raise an exception (as far as I remember), is it the same with Go? are slice index in Go bound to one specific type?</p>
  
  <p>EDIT:</p>
  
  <p>I ran the test using <code>struct{}</code> instead of <code>bool</code> and allocating <code>math.MaxInt64</code> elements. Everything went as expected, and prints:</p>
  
  <pre><code>Size: 9223372036854775807
  </code></pre>
  
  <p>So, another question, why there are two different error messages when it seems that the error is the same (not enough memory)?</p>
  
  <p>What are the conditions for each error to pop out?</p>