Getting the value of sql.NullString with reflection
<p>I'd like to get the value of all the fields in a struct that aren't empty. This works for simple primitives (<code>string</code>, <code>int</code>, etc.), but not for struct types such as <code>sql.NullString</code>.</p>

<p>Very simple example:</p>

<pre><code>package main

import (
    "database/sql"
    "fmt"
    "reflect"
)

func main() {
    type fooT struct {
        NullS sql.NullString
    }

    values := reflect.ValueOf(fooT{})

    field := values.Field(0)
    v := reflect.ValueOf(field)

    iface := v.Interface().(sql.NullString)

    fmt.Println(iface)
}
</code></pre>

<p>This gives a panic:</p>

<pre><code>panic: interface conversion: interface is reflect.Value, not sql.NullString
</code></pre>

<p>I don't understand this, since the <code>Interface</code> method should return an <code>interface{}</code> (and not a <code>reflect.Value</code>) which I can then type assert (?)</p>

<p>First I thought I was maybe using type conversions only works for primitives, but a quick test script:</p>

<pre><code>package main

import (
    "database/sql"
    "fmt"
)

func main() {
    type fooT struct {
        NullS sql.NullString
    }

    foo := fooT{NullS: sql.NullString{"It's an Aardvark!", true}}

    var iface interface{}
    iface = foo.NullS

    fmt.Printf("%T -&gt; %#v\n", iface, iface)
    fmt.Printf("%T -&gt; %#v -&gt; %#v\n", iface.(sql.NullString), iface.(sql.NullString),
        iface.(sql.NullString).Valid)
}
</code></pre>

<p>Reveals that this should work?</p>

<hr>

<p>The full code I'm using:</p>

<pre><code>package main

import (
    "database/sql"
    "fmt"
    "reflect"
)

type fooT struct {
    ID    int64
    Foo   string
    NullS sql.NullString
    FooQ  sql.NullString
}

func main() {
    foo := fooT{
        ID:    42,
        NullS: sql.NullString{"Your mother was a hamster", true},
    }

    types := reflect.TypeOf(foo)
    values := reflect.ValueOf(foo)

    changed := ""
    for i := 0; i &lt; types.NumField(); i++ {
        fieldType := types.Field(i)
        field := values.Field(i)

        switch field.Type().Kind() {
        // Works
        case reflect.String:
            if field.String() != "" {
                changed += fmt.Sprintf("&lt;strong&gt;%s&lt;/strong&gt;: %v&lt;br&gt;\n",
                    fieldType.Name, field.String())
            }
        default:
            switch field.Type().String() {
            case "sql.NullString":
                v := reflect.ValueOf(field)

                // NullS: reflect.Value -&gt; sql.NullString{String:"Your mother was a hamster", Valid:true}
                iface := v.Interface()
                fmt.Printf("%s: %T -&gt; %#v\n",
                    fieldType.Name, iface, iface)

                // panic: interface conversion: interface is reflect.Value, not sql.NullString
                iface2 := v.Interface().(sql.NullString)
                fmt.Printf("%s: %T -&gt; %#v\n",
                    fieldType.Name, iface2, iface2)
            }
        }
    }

    fmt.Printf(changed)
}
</code></pre>