title: How do I handle nil return values from database?
body: <p>I am writing a basic program to read values from database table and print in table. The table was populated by an ancient program. Some of the fields in the row are optional and when I try to read them as string, I get the following error:</p>
  
  <pre><code>panic: sql: Scan error on column index 2: unsupported driver -&gt; Scan pair: &lt;nil&gt; -&gt; *string
  </code></pre>
  
  <p>After I read other questions for similar issues, I came up with following code to handle the nil values. The method works fine in practice. I get the values in plain text and empty string instead of the nil values.</p>
  
  <p>However, I have two concerns:</p>
  
  <ol>
  <li>This does not look efficient. I need to handle 25+ fields like this and that would mean I read each of them as bytes and convert to string. Too many function calls and conversions. Two structs to handle the data and so on...</li>
  <li>The code looks ugly. It is already looking convoluted with 2 fields and becomes unreadable as I go to 25+</li>
  </ol>
  
  <p>Am I doing it wrong? Is there a better/cleaner/efficient/idiomatic golang way to read values from database?</p>
  
  <p>I find it hard to believe that a modern language like Go would not handle the database returns gracefully.</p>
  
  <p>Thanks in advance!</p>
  
  <p><strong>Code snippet:</strong></p>
  
  <pre><code>// DB read format
  type udInfoBytes struct {
    id                     []byte
    state                  []byte
  }
  
  // output format
  type udInfo struct {
    id                     string
    state                  string
  }
  
  func CToGoString(c []byte) string {
    n := -1
    for i, b := range c {
      if b == 0 {
        break
      }
      n = i
    }
    return string(c[:n+1])
  }
  
  func dbBytesToString(in udInfoBytes) udInfo {
  
    var out udInfo
    var s string
    var t int
  
    out.id = CToGoString(in.id)
    out.state = stateName(in.state)
    return out
  }
  
  func GetInfo(ud string) udInfo {
  
    db := getFileHandle()
    q := fmt.Sprintf("SELECT id,state FROM Mytable WHERE id='%s' ", ud)
  
    rows, err := db.Query(q)
    if err != nil {
      log.Fatal(err)
    }
    defer rows.Close()
    ret := udInfo{}
    r := udInfoBytes{}
    for rows.Next() {
      err := rows.Scan(&amp;r.id, &amp;r.state)
  
      if err != nil {
        log.Println(err)
      }
      break
    }
    err = rows.Err()
    if err != nil {
      log.Fatal(err)
    }
  
    ret = dbBytesToString(r)
    defer db.Close()
    return ret
  }
  </code></pre>
  
  <p><strong>edit:</strong></p>
  
  <p>I want to have something like the following where I do no have to worry about handling NULL and automatically read them as empty string.</p>
  
  <pre><code>// output format
  type udInfo struct {
    id                     string
    state                  string
  }
  
  func GetInfo(ud string) udInfo {
  
    db := getFileHandle()
    q := fmt.Sprintf("SELECT id,state FROM Mytable WHERE id='%s' ", ud)
  
    rows, err := db.Query(q)
    if err != nil {
      log.Fatal(err)
    }
    defer rows.Close()
    r := udInfo{}
  
    for rows.Next() {
      err := rows.Scan(&amp;r.id, &amp;r.state)
  
      if err != nil {
        log.Println(err)
      }
      break
    }
    err = rows.Err()
    if err != nil {
      log.Fatal(err)
    }
  
    defer db.Close()
    return r
  }
  </code></pre>