title: How can I fetch a NULL boolean from Postgres in Go?
body: <p>Go's zero value for a bool type is <code>false</code>. Postgres supports an undefined BOOL type, represented as NULL.  This leads to problems when trying to fetch a BOOL value from Postgres in Go:</p>
  
  <pre><code>rows,err := db.Query("SELECT UNNEST('{TRUE,FALSE,NULL}'::BOOL[])");
  if err != nil {
      log.Fatal(err)
  }
  for rows.Next() {
      var value bool
      if err := rows.Scan(&amp;value); err != nil {
          log.Fatal(err)
      }
      fmt.Printf("Value: %t\n",value);
  }
  </code></pre>
  
  <p>Output:</p>
  
  <pre class="lang-none prettyprint-override"><code>Value: true  
  Value: false  
  2014/11/17 09:34:26 sql: Scan error on column index 0: sql/driver: couldn't convert &lt;nil&gt; (&lt;nil&gt;) into type bool
  </code></pre>
  
  <p>What's the most idiomatic way around this problem? The two solutions I have imagined are neither very attractive:</p>
  
  <ol>
  <li>Don't use Go's <code>bool</code> type. Instead I would probably use a string, and do my own conversion which accounts for <code>nil</code></li>
  <li>Always make sure BOOLs are either TRUE or FALSE in Postgres by using <code>COALESCE()</code> or some other means.</li>
  </ol>