title: Go compare strings
body: <p>Given two strings <code>a</code> and <code>b</code>, sometimes I want to determine which one of the three statements: <code>a &lt; b</code>, <code>a == b</code> or <code>a &gt; b</code> is true.</p>
  
  <p>In a language like C or C++, I will get an int value <code>v</code> after one invoking of the corresponding function or method. Then I can determine which of the above statements is true by examining whether <code>v &lt; 0</code>, <code>v == 0</code> or <code>v &gt; 0</code>.</p>
  
  <p>But in Go I have to do at least two comparisons (e.g. first test <code>a &lt; b</code> then test <code>a == b</code>) to find out which one of the three statements is true.</p>
  
  <p>My question is that is there a way in Go so that I can just do a single comparison?</p>
  
  <p>It turns out that this feature is called <a href="https://en.wikipedia.org/wiki/Three-way_comparison" rel="nofollow noreferrer">three way comparison</a>.</p>