title: Go lang string constants
body: <p>Is there a difference between an inline string and a constant string being used in Go, or is this being optimized entirely by the compiler?</p>
  
  <p>Example benchmark code with 100B iterations:</p>
  
  <pre><code>package main
  
  import "fmt"
  import "time"
  import "log"
  
  func main() {
      // String
      start := time.Now()
      for i := 0; i &lt; 100000000; i++ {
          x := "My String"
          if i % 1000000 == 0{
              fmt.Printf(x)
          }
      }
      elapsed := time.Since(start)
      log.Printf("\nTook %s", elapsed)
  
      // Constant
      start2 := time.Now()
      const MY_STRING = "My String 2"
      for i := 0; i &lt; 100000000; i++ {
          x := MY_STRING
          if i % 1000000 == 0 {
              fmt.Printf(x)
          }
      }
  
      elapsed2 := time.Since(start2)
      log.Printf("\nTook %s", elapsed2)
  
      // Proof that the timer does work :)
      start3 := time.Now()
      time.Sleep(100 * time.Millisecond)
      elapsed3 := time.Since(start3)
      log.Printf("\nTook %s", elapsed3)
  
  }
  </code></pre>
  
  <p>Output:</p>
  
  <pre><code>My StringMy StringMy StringMy StringMy StringMy StringMy StringMy StringMy StringMy StringMy StringMy StringMy StringMy StringMy StringMy StringMy StringMy StringMy StringMy StringMy StringMy StringMy StringMy StringMy StringMy StringMy StringMy StringMy StringMy StringMy StringMy StringMy StringMy StringMy StringMy StringMy StringMy StringMy StringMy StringMy StringMy StringMy StringMy StringMy StringMy StringMy StringMy StringMy StringMy StringMy StringMy StringMy StringMy StringMy StringMy StringMy StringMy StringMy StringMy StringMy StringMy StringMy StringMy StringMy StringMy StringMy StringMy StringMy StringMy StringMy StringMy StringMy StringMy StringMy StringMy StringMy StringMy StringMy StringMy StringMy StringMy StringMy StringMy StringMy StringMy StringMy StringMy StringMy StringMy StringMy StringMy StringMy StringMy StringMy StringMy StringMy StringMy StringMy StringMy String2009/11/10 23:00:00 
  Took 0
  My String 2My String 2My String 2My String 2My String 2My String 2My String 2My String 2My String 2My String 2My String 2My String 2My String 2My String 2My String 2My String 2My String 2My String 2My String 2My String 2My String 2My String 2My String 2My String 2My String 2My String 2My String 2My String 2My String 2My String 2My String 2My String 2My String 2My String 2My String 2My String 2My String 2My String 2My String 2My String 2My String 2My String 2My String 2My String 2My String 2My String 2My String 2My String 2My String 2My String 2My String 2My String 2My String 2My String 2My String 2My String 2My String 2My String 2My String 2My String 2My String 2My String 2My String 2My String 2My String 2My String 2My String 2My String 2My String 2My String 2My String 2My String 2My String 2My String 2My String 2My String 2My String 2My String 2My String 2My String 2My String 2My String 2My String 2My String 2My String 2My String 2My String 2My String 2My String 2My String 2My String 2My String 2My String 2My String 2My String 2My String 2My String 2My String 2My String 2My String 22009/11/10 23:00:00 
  Took 0
  2009/11/10 23:00:00 
  Took 100ms
  
  Program exited.
  </code></pre>