title: Comparing strings in Go
body: <p>I'm trying to find the begin of a named capturing groups in a string to create a simple parser (see <a href="https://stackoverflow.com/questions/13330274/regex-to-find-named-capturing-groups-with-go-programming-language">related question</a>). To do this the <code>extract</code> function remembers the last for characters in the <code>last4</code> variable. If the last 4 characters are equal to "(?P&lt;" it is the beginning of a capturing group: </p>
  
  <pre><code>package main
  
  import "fmt"
  
  const sample string = `/(?P&lt;country&gt;m((a|b).+)(x|y)n)/(?P&lt;city&gt;.+)`
  
  func main() {
      extract(sample)
  }
  
  func extract(regex string) {
      last4 := new([4]int32)
      for _, c := range regex {
          last4[0], last4[1], last4[2], last4[3] = last4[1], last4[2], last4[3], c
          last4String := fmt.Sprintf("%c%c%c%c\n", last4[0], last4[1], last4[2], last4[3])
          if last4String == "(?P&lt;" {
              fmt.Print("start of capturing group")
          }
      }
  }
  </code></pre>
  
  <p><a href="http://play.golang.org/p/pqA-wCuvux" rel="nofollow noreferrer">http://play.golang.org/p/pqA-wCuvux</a></p>
  
  <p>But this code prints nothing! <code>last4String == "(?P&lt;"</code> is never true, although this substrin appears in the output if I print <code>last4String</code> inside the loop. How to compare strings in Go then?</p>
  
  <p>And is there a more elegant way to convert an int32 array to a string than <code>fmt.Sprintf("%c%c%c%c\n", last4[0], last4[1], last4[2], last4[3])</code>?</p>
  
  <p>Anything else that could be better? My code looks somewhat inelegant to me.</p>