title: Reference to string literals in Go
body: <p>In my application I will frequently pass references to a static string. I wish to avoid having Go allocate memory for each call, but I failed to get the address to my string literal.</p>
  
  <p>Why is it not possible to take the address of a string literal (see <code>test1()</code> in the example below)? Have I misunderstood the syntax, or is it a limitation due to the internal workings of Go?</p>
  
  <p>If not possible, what would be the best solution?</p>
  
  <p><code>test2()</code> works, but will it allocate memory for the <code>var hej</code> each time?<br>
  <code>test3()</code> will not allocate any new memory, but I wish to avoid clutter outside the function.  </p>
  
  <pre><code>package main
  
  import "fmt"
  
  var konnichiwa = `こんにちは世界`
  
  // Gives the compile error `cannot take the address of "Hello world"`
  func test1() (*string) { return &amp;`Hello world` }
  
  // Works fine
  func test2() (*string) {
      hej := `Hej världen`
      return &amp;hej
  }
  
  func test3() (*string) { return &amp;konnichiwa }
  
  func main(){
      fmt.Println(*test1())
      fmt.Println(*test2())
      fmt.Println(*test3())
  }
  </code></pre>
  
  <p>Thanks for help!</p>