title: Different behavior of len() with const or non-const value
body: <p>The code below</p>
  <pre class="lang-golang prettyprint-override"><code>const s = &quot;golang.go&quot;
  
  var a byte = 1 &lt;&lt; len(s) / 128
  </code></pre>
  <p>The result of <code>a</code> is 4. However, after changing <code>const s</code> to <code>var s</code> as following</p>
  <pre class="lang-golang prettyprint-override"><code>var s = &quot;golang.go&quot;
  
  var a byte = 1 &lt;&lt; len(s) / 128
  </code></pre>
  <p>The result of <code>a</code> is 0 now.</p>
  <p>Also other test codes as below</p>
  <pre class="lang-golang prettyprint-override"><code>const s = &quot;golang.go&quot;
  
  var a byte = 1 &lt;&lt; len(s) / 128     // the result of a is 4
  var b byte = 1 &lt;&lt; len(s[:]) / 128  // the result of b is 0
  
  var ss = &quot;golang.go&quot;
  
  var aa byte = 1 &lt;&lt; len(ss) / 128    // the result of aa is 0
  var bb byte = 1 &lt;&lt; len(ss[:]) / 128 // the result of bb is 0
  </code></pre>
  <p>It is weird that <code>b</code> is 0 with evaluating the length of <code>s[:]</code></p>
  <p>I try to understand it per <a href="https://go.dev/ref/spec#Length_and_capacity" rel="noreferrer">golang spec</a></p>
  <blockquote>
  <p>The expression len(s) is constant if s is a string constant. The expressions len(s) and cap(s) are constants if the type of s is an array or pointer to an array and the expression s does not contain channel receives or (non-constant) function calls</p>
  </blockquote>
  <p>But I failed. Could someone explain it more clearly to me?</p>